From 5ab1c13961a041c7c987c7949f9e30937011e07a Mon Sep 17 00:00:00 2001 From: Jon007 Date: Thu, 20 Jul 2017 00:23:08 +0800 Subject: [PATCH] Fixes #215 Coupon string translation, Fixes #217 updated BACS function * Fixes #215 add string translations for Coupons (includes WooCommerce Extended Coupon Features if installed) * addresses #168 with a utility function get_translated_variation to help get translated products or variations * Fixes #217 BACS bank_details() update for woocommerce3 --- CHANGELOG.md | 3 + readme.txt | 5 +- src/Hyyan/WPI/Coupon.php | 164 +++++++++++++++++++------ src/Hyyan/WPI/Gateways/GatewayBACS.php | 131 +++++++++++--------- src/Hyyan/WPI/Utilities.php | 57 ++++++++- 5 files changed, 260 insertions(+), 100 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f79899e..09c0743 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,7 @@ ### 1.0.2 +* Fixes #215 add string translations for Coupons (includes WooCommerce Extended Coupon Features if installed) +* addresses #168 with a utility function get_translated_variation to help get translated products or variations +* Fixes #217 BACS bank_details() update for woocommerce3 * partially implements #208 WooCommerce 3.1 CSV Import/Export by adding support for synchronising Product Meta and Product Attributes to translated products * Fixes #213 copy children for Grouped Product * Fixes #207 suppresses login customization to allow "Pay for Order" links to work when customer is not logged in diff --git a/readme.txt b/readme.txt index 8d1b55b..b4ac7cb 100644 --- a/readme.txt +++ b/readme.txt @@ -122,8 +122,11 @@ Just make sure to setup your permalinks , and every thing will be fine , I promi == Changelog == == 1.0.2 == -* partially implements #208 WooCommerce 3.1 CSV Import/Export by adding support for synchronising Product Meta and Product Attributes to translated products +* Fixes #215 add string translations for Coupons (includes WooCommerce Extended Coupon Features if installed) +* addresses #168 with a utility function get_translated_variation to help get translated products or variations +* Fixes #217 BACS bank_details() update for woocommerce3 * Fixes #213 copy children for Grouped Product +* partially implements #208 WooCommerce 3.1 CSV Import/Export by adding support for synchronising Product Meta and Product Attributes to translated products * Fixes #207 suppresses login customization to allow "Pay for Order" links to work when customer is not logged in (after login continue to payment page instead of my account home) * Fixes #212 update deleteRelatedVariation for woocommerce3 warnings diff --git a/src/Hyyan/WPI/Coupon.php b/src/Hyyan/WPI/Coupon.php index 73d11ae..00121b3 100644 --- a/src/Hyyan/WPI/Coupon.php +++ b/src/Hyyan/WPI/Coupon.php @@ -31,9 +31,134 @@ public function __construct() { if ('on' === Settings::getOption('coupons', Features::getID(), 'on')) { add_action('woocommerce_coupon_loaded', array($this, 'couponLoaded')); + + add_action('wp_loaded', array($this, 'registerCouponStringsForTranslation')); + + //apply label filter with higher priority than woocommerce-auto-added-coupons + add_filter('woocommerce_cart_totals_coupon_label', + array($this, 'translateLabel'), 20, 2); + add_filter('woocommerce_coupon_get_description', + array($this, 'translateDescription'), 10, 2); + + /* additional fields for WooCommerce Extended Coupon Features */ + add_filter('woocommerce_coupon_get__wjecf_enqueue_message', + array($this, 'translateMessage'), 10, 2); + add_filter('woocommerce_coupon_get__wjecf_select_free_product_message', + array($this, 'translateMessage'), 10, 2); + add_filter('woocommerce_coupon_get__wjecf_free_product_ids', + array($this, 'getFreeProductsInLanguage'), 10, 2); } } + /** + * filter product ids + * + * @param string $product_ids list + * @param WC_Coupon $coupon current coupon + * + * @return array filtered result + */ + public function getFreeProductsInLanguage($productIds, $coupon) + { + if (is_admin()){return $productIds;} + $productLang = pll_current_language(); + $productIds = explode(',', $productIds); + $mappedIds = array(); + foreach ($productIds as $productId) { + $mappedIds[] = Utilities::get_translated_variation($productId, $productLang); + } + return $mappedIds; + } + + /** + * translate coupon code. + * + * @param string $value + * @param \WC_Coupon $coupon + * + * @return string + */ + public function translateLabel($value, $coupon) + { + return sprintf( esc_html__( 'Coupon: %s', 'woocommerce' ), + pll__( \get_post( $coupon->get_id() )->post_title ) ); + } + /** + * translate coupon description. + * + * @param string $value + * @param \WC_Coupon $coupon + * + * @return string + */ + public function translateDescription($value, $coupon) + { + return pll__($value); + } + /** + * translate coupon message. + * + * @param string $value + * @param \WC_Coupon $coupon + * + * @return string + */ + public function translateMessage($value, $coupon) + { + return pll__($value); + } + + /** + * Register coupon titles adn descriptions in Polylang's Strings translations table. + */ + public function registerCouponStringsForTranslation() + { + if (function_exists('pll_register_string')) { + $coupons = $this->getCoupons(); + + foreach ($coupons as $coupon) { + //$code = wc_format_coupon_code($coupon->post_title); + pll_register_string($coupon->post_name, $coupon->post_title, + __('Woocommerce Coupon Names', 'woo-poly-integration')); + pll_register_string($coupon->post_name . '_description', $coupon->post_excerpt, + __('Woocommerce Coupon Names', 'woo-poly-integration'), true); + + $coupon_message = get_post_meta($coupon->ID, '_wjecf_enqueue_message', true); + if ($coupon_message) { + pll_register_string($coupon->post_name . '_message', $coupon_message, + __('Woocommerce Coupon Names', 'woo-poly-integration'), true); + } + $freeproduct_message = get_post_meta($coupon->ID, '_wjecf_select_free_product_message', true); + if ($freeproduct_message) { + pll_register_string($coupon->post_name . '_freeproductmessage', $coupon_message, + __('Woocommerce Coupon Names', 'woo-poly-integration'), true); + } + + } + } + } + + /** + * Helper function - Gets the coupons enabled in the shop. + * + * @return array $coupons Coupons settings including post_type, post_excerpt and post_title + */ + private function getCoupons(){ + global $woocommerce; + + $args = array( + 'posts_per_page' => -1, + 'orderby' => 'title', + 'order' => 'asc', + 'post_type' => 'shop_coupon', + 'post_status' => 'publish', + ); + + $coupons = get_posts( $args ); + return $coupons; + } + + /** * Extend the coupon to include porducts translations. * @@ -79,45 +204,6 @@ public function couponLoaded(\WC_Coupon $coupon) return $coupon; } - /** - * Extend the coupon to include porducts translations. - * - * @param \WC_Coupon $coupon - * - * @return \WC_Coupon - */ - public function couponLoadedOld(\WC_Coupon $coupon) - { - $productIDS = array(); - $excludeProductIDS = array(); - $productCategoriesIDS = array(); - $excludeProductCategoriesIDS = array(); - foreach ($coupon->product_ids as $id) { - foreach ($this->getProductPostTranslationIDS($id) as $_id) { - $productIDS[] = $_id; - } - } - foreach ($coupon->exclude_product_ids as $id) { - foreach ($this->getProductPostTranslationIDS($id) as $_id) { - $excludeProductIDS[] = $_id; - } - } - foreach ($coupon->product_categories as $id) { - foreach ($this->getProductTermTranslationIDS($id) as $_id) { - $productCategoriesIDS[] = $_id; - } - } - foreach ($coupon->exclude_product_categories as $id) { - foreach ($this->getProductTermTranslationIDS($id) as $_id) { - $excludeProductCategoriesIDS[] = $_id; - } - } - $coupon->product_ids = $productIDS; - $coupon->exclude_product_ids = $excludeProductIDS; - $coupon->product_categories = $productCategoriesIDS; - $coupon->exclude_product_categories = $excludeProductCategoriesIDS; - return $coupon; - } /** * Get array of product translations IDS. diff --git a/src/Hyyan/WPI/Gateways/GatewayBACS.php b/src/Hyyan/WPI/Gateways/GatewayBACS.php index d9745e3..080b643 100644 --- a/src/Hyyan/WPI/Gateways/GatewayBACS.php +++ b/src/Hyyan/WPI/Gateways/GatewayBACS.php @@ -59,70 +59,83 @@ public function email_instructions($order, $sent_to_admin, $plain_text = false) /** * Get bank details and place into a list format. + * Updated from wooCommerce 3.1 * * Note: Since this is declared as a private function in WC_Gateway_BACS, it needs * to be copied here 1:1 * * @param int $order_id */ - private function bank_details($order_id = '') - { - if (empty($this->account_details)) { - return; - } + private function bank_details( $order_id = '' ) { + + if ( empty( $this->account_details ) ) { + return; + } + + // Get order and store in $order + $order = wc_get_order( $order_id ); + + // Get the order country and country $locale + $country = $order->get_billing_country(); + $locale = $this->get_country_locale(); + + // Get sortcode label in the $locale array and use appropriate one + $sortcode = isset( $locale[ $country ]['sortcode']['label'] ) ? $locale[ $country ]['sortcode']['label'] : __( 'Sort code', 'woocommerce' ); + + $bacs_accounts = apply_filters( 'woocommerce_bacs_accounts', $this->account_details ); + + if ( ! empty( $bacs_accounts ) ) { + $account_html = ''; + $has_details = false; + + foreach ( $bacs_accounts as $bacs_account ) { + $bacs_account = (object) $bacs_account; + + if ( $bacs_account->account_name ) { + $account_html .= '

' . wp_kses_post( wp_unslash( $bacs_account->account_name ) ) . ':

' . PHP_EOL; + } + + $account_html .= ''; + } + + if ( $has_details ) { + echo '

' . __( 'Our bank details', 'woocommerce' ) . '

' . PHP_EOL . $account_html . '
'; + } + } + + } - // Get order and store in $order - $order = wc_get_order($order_id); - - // Get the order country and country $locale - $country = Utilities::get_billing_country($order); - $locale = $this->get_country_locale(); - - // Get sortcode label in the $locale array and use appropriate one - $sortcode = isset($locale[ $country ]['sortcode']['label']) ? $locale[ $country ]['sortcode']['label'] : __('Sort Code', 'woocommerce'); - - $bacs_accounts = apply_filters('woocommerce_bacs_accounts', $this->account_details); - - if (!empty($bacs_accounts)) { - echo '

'.__('Our Bank Details', 'woocommerce').'

'.PHP_EOL; - - foreach ($bacs_accounts as $bacs_account) { - $bacs_account = (object) $bacs_account; - - if ($bacs_account->account_name || $bacs_account->bank_name) { - echo '

'.wp_unslash(implode(' - ', array_filter(array($bacs_account->account_name, $bacs_account->bank_name)))).'

'.PHP_EOL; - } - - echo ''; - } - } - } } diff --git a/src/Hyyan/WPI/Utilities.php b/src/Hyyan/WPI/Utilities.php index b00e8e5..f619963 100644 --- a/src/Hyyan/WPI/Utilities.php +++ b/src/Hyyan/WPI/Utilities.php @@ -438,5 +438,60 @@ public static function get_variation_parentid($variation) return null; } } - + /* + * get the translated product, including if it is a variation product, get the translated variation + * if there is no translation, return the original product + * + * @param int $product_id Product + * + * @return int translated product or variation (or original if no translation) + * + */ + public static function get_translated_variation($product_id, $lang) + { + if (! $lang){ + $lang = pll_current_language(); + } + //if input is already in correct language just return it + $sourcelang = pll_get_post_language($product_id); + if ($sourcelang == $lang){ + return $product_id; + } + //if a translated item is found, return it + $translated_id = pll_get_post( $product_id, $lang); + if ( ( $translated_id ) && ( $translated_id != $product_id ) ){ + return $translated_id; + } + //ok no linked Polylang translation so maybe it's a variation + $product = wc_get_product($product_id); + if ($product && 'variation' === $product->get_type()) { + //it's a variation, so let's get the parent and translate that + $parent_id = $product->get_parent_id(); + $translated_id = pll_get_post( $parent_id, $lang); + //if no translation return the original product variation id + if ((! $translated_id) || ($translated_id == $parent_id) ) { + return $product_id; + } + //ok, it's a variation and the parent product is translated, so here's what to do: + //find the master link for this variation using the Hyyan '_point_to_variation' key + $variationmaster = get_post_meta($product_id, '_point_to_variation'); + if (! $variationmaster){ + return $product_id; + } + //and now the related variation for the translation + $posts = get_posts(array( + 'meta_key' => '_point_to_variation', + 'meta_value' => $variationmaster, + 'post_type' => 'product_variation', + 'post_parent' => $translated_id, + )); + //return the related variation if there is one + if ( count($posts) ){ + return $posts[0]->ID; + } else { + //if this variation hasn't been translated, return the original + return $product_id; + } + } + } }