WCML_Coupons

The WooCommerce Multilingual WCML Coupons class.

Defined (1)

The class is defined in the following location(s).

/inc/class-wcml-coupons.php  
  1. class WCML_Coupons{ 
  2.  
  3. private $woocommerce_wpml; 
  4. private $sitepress; 
  5.  
  6. public function __construct( &$woocommerce_wpml, &$sitepress ) { 
  7. $this->woocommerce_wpml = $woocommerce_wpml; 
  8. $this->sitepress = $sitepress; 
  9.  
  10. add_action( 'woocommerce_coupon_loaded', array( $this, 'wcml_coupon_loaded' ) ); 
  11. add_action( 'admin_init', array( $this, 'icl_adjust_terms_filtering' ) ); 
  12.  
  13. public function wcml_coupon_loaded( $coupons_data ) { 
  14.  
  15. $wc_27_coupons = method_exists( 'WC_Coupon', 'get_amount' ); 
  16.  
  17. $coupon_product_ids = $wc_27_coupons ? $coupons_data->get_product_ids() : $coupons_data->product_ids; 
  18. $coupon_excluded_product_ids = $wc_27_coupons ? $coupons_data->get_excluded_product_ids() : $coupons_data->exclude_product_ids; 
  19. $coupon_product_categories = $wc_27_coupons ? $coupons_data->get_product_categories() : $coupons_data->product_categories; 
  20. $coupon_excluded_product_categories = $wc_27_coupons ? $coupons_data->get_excluded_product_categories() : $coupons_data->exclude_product_categories; 
  21.  
  22. $product_ids = array(); 
  23. $exclude_product_ids = array(); 
  24. $product_categories_ids = array(); 
  25. $exclude_product_categories_ids = array(); 
  26.  
  27. foreach( $coupon_product_ids as $prod_id ) { 
  28. $post_type = get_post_field( 'post_type', $prod_id ); 
  29. $trid = $this->sitepress->get_element_trid( $prod_id, 'post_' . $post_type ); 
  30. $translations = $this->sitepress->get_element_translations( $trid, 'post_' . $post_type ); 
  31. foreach( $translations as $translation ) { 
  32. $product_ids[] = $translation->element_id; 
  33. foreach( $coupon_excluded_product_ids as $prod_id ) { 
  34. $post_type = get_post_field( 'post_type', $prod_id ); 
  35. $trid = $this->sitepress->get_element_trid( $prod_id, 'post_' . $post_type ); 
  36. $translations = $this->sitepress->get_element_translations( $trid, 'post_' . $post_type ); 
  37. foreach( $translations as $translation ) { 
  38. $exclude_product_ids[] = $translation->element_id; 
  39.  
  40. foreach( $coupon_product_categories as $cat_id ) { 
  41. $term = $this->woocommerce_wpml->terms->wcml_get_term_by_id( $cat_id, 'product_cat' ); 
  42. $trid = $this->sitepress->get_element_trid( $term->term_taxonomy_id, 'tax_product_cat' ); 
  43. $translations = $this->sitepress->get_element_translations( $trid, 'tax_product_cat' ); 
  44.  
  45. foreach( $translations as $translation ) { 
  46. $product_categories_ids[] = $translation->term_id; 
  47.  
  48. foreach( $coupon_excluded_product_categories as $cat_id ) { 
  49. $term = $this->woocommerce_wpml->terms->wcml_get_term_by_id( $cat_id, 'product_cat' ); 
  50. $trid = $this->sitepress->get_element_trid( $term->term_taxonomy_id, 'tax_product_cat' ); 
  51. $translations = $this->sitepress->get_element_translations( $trid, 'tax_product_cat' ); 
  52. foreach( $translations as $translation ) { 
  53. $exclude_product_categories_ids[] = $translation->term_id; 
  54.  
  55. if( $wc_27_coupons ) { 
  56. $coupons_data->set_product_ids( $product_ids ); 
  57. $coupons_data->set_excluded_product_ids( $exclude_product_ids ); 
  58. $coupons_data->set_product_categories( $product_categories_ids ); 
  59. $coupons_data->set_excluded_product_categories( $exclude_product_categories_ids ); 
  60. } else { 
  61. $coupons_data->product_ids = $product_ids; 
  62. $coupons_data->exclude_product_ids = $exclude_product_ids; 
  63. $coupons_data->product_categories = $product_categories_ids; 
  64. $coupons_data->exclude_product_categories = $exclude_product_categories_ids; 
  65.  
  66. return $coupons_data; 
  67.  
  68. public function icl_adjust_terms_filtering() { 
  69. if( is_admin() && isset( $_GET[ 'action' ] ) && $_GET['action'] == 'woocommerce_json_search_products_and_variations' ) { 
  70. global $icl_adjust_id_url_filter_off; 
  71. $icl_adjust_id_url_filter_off = true; 
  72.