Skip to content
This repository has been archived by the owner on Mar 17, 2022. It is now read-only.

Commit

Permalink
fixes #268 setup coupon translations only when needed
Browse files Browse the repository at this point in the history
fixes #268 setup coupon translations only when needed
  • Loading branch information
Jon007 committed Nov 25, 2017
1 parent e0c51ba commit d192347
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions src/Hyyan/WPI/Coupon.php
Expand Up @@ -32,7 +32,7 @@ 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'));
add_action('wp_loaded', array($this, 'adminRegisterCouponStrings'));

//apply label filter with higher priority than woocommerce-auto-added-coupons
add_filter('woocommerce_cart_totals_coupon_label',
Expand Down Expand Up @@ -82,6 +82,7 @@ public function getFreeProductsInLanguage($productIds, $coupon)
*/
public function translateLabel($value, $coupon)
{
$this->registerCouponStringsForTranslation();
return sprintf(esc_html__('Coupon: %s', 'woocommerce'),
pll__(\get_post($coupon->get_id())->post_title));
}
Expand All @@ -95,6 +96,7 @@ public function translateLabel($value, $coupon)
*/
public function translateDescription($value, $coupon)
{
$this->registerCouponStringsForTranslation();
return pll__($value);
}
/**
Expand All @@ -107,35 +109,46 @@ public function translateDescription($value, $coupon)
*/
public function translateMessage($value, $coupon)
{
$this->registerCouponStringsForTranslation();
return pll__($value);
}

public function adminRegisterCouponStrings()
{
if (is_admin() && (!is_ajax())){
$this->registerCouponStringsForTranslation();
}
}
/**
* Register coupon titles adn descriptions in Polylang's Strings translations table.
*/
public function registerCouponStringsForTranslation()
{
if (function_exists('pll_register_string')) {
$coupons = $this->getCoupons();
static $coupons_loaded;
if (! $coupons_loaded){
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);
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);
$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);
}
}
}
$coupons_loaded = true;
}
}

Expand Down

0 comments on commit d192347

Please sign in to comment.