Perfect Brands for WooCommerce - Version 2.0.9

Version Description

  • Fix: Exclude coupons from brands
Download this release

Release Info

Developer quadlayers
Plugin Icon 128x128 Perfect Brands for WooCommerce
Version 2.0.9
Comparing to
See all releases

Code changes from version 2.0.8 to 2.0.9

classes/admin/class-pwb-coupon.php CHANGED
@@ -9,37 +9,50 @@ class PWB_Coupon {
9
  function __construct() {
10
  add_action( 'woocommerce_coupon_options_usage_restriction', array( $this, 'coupon_restriction' ) );
11
  add_action( 'woocommerce_coupon_options_save', array( $this, 'coupon_save' ) );
12
- add_filter( 'woocommerce_coupon_is_valid', array( $this, 'is_valid_coupon' ), 10, 2 );
13
- add_filter( 'woocommerce_coupon_is_valid_for_product', array( $this, 'is_valid_for_product_brand' ), 10, 4 );
 
 
14
  }
15
 
16
  public function coupon_restriction() {
17
  global $thepostid, $post;
 
18
  $thepostid = empty( $thepostid ) ? $post->get_ID() : $thepostid;
19
 
20
- $selected_brands = get_post_meta( $thepostid, '_pwb_coupon_restriction', true );
21
- if ( $selected_brands == '' ) {
22
- $selected_brands = array();
23
- }
24
 
25
  ob_start();
26
- ?>
27
- <p class="form-field"><label for="_pwb_coupon_restriction"><?php esc_html_e( 'Brands restriction', 'perfect-woocommerce-brands' ); ?></label>
28
- <select id="_pwb_coupon_restriction" name="_pwb_coupon_restriction[]" style="width: 50%;" class="wc-enhanced-select" multiple="multiple" data-placeholder="<?php esc_attr_e( 'Any brand', 'perfect-woocommerce-brands' ); ?>">
29
- <?php
30
- $categories = get_terms( 'pwb-brand', 'orderby=name&hide_empty=0' );
31
- if ( $categories ) {
32
- foreach ( $categories as $cat ) {
33
- echo '<option value="' . esc_attr( $cat->term_id ) . '"' . selected( in_array( $cat->term_id, $selected_brands ), true, false ) . '>' . esc_html( $cat->name ) . '</option>';
34
- }
35
  }
36
- ?>
37
- </select>
 
 
 
 
 
 
38
  <?php
39
- // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
40
- echo wc_help_tip( esc_html__( 'Coupon will be valid if there are at least one product of this brands in the cart', 'perfect-woocommerce-brands' ) );
 
 
 
 
41
  ?>
42
- </p>
 
 
43
  <?php
44
  // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
45
  echo ob_get_clean();
@@ -47,37 +60,121 @@ class PWB_Coupon {
47
  }
48
 
49
  public function coupon_save( $post_id ) {
50
- // phpcs:ignore WordPress.Security
51
- $_pwb_coupon_restriction = isset( $_POST['_pwb_coupon_restriction'] ) ? sanitize_text_field( wp_unslash( $_POST['_pwb_coupon_restriction'] ) ) : '';
52
- update_post_meta( $post_id, '_pwb_coupon_restriction', $_pwb_coupon_restriction );
 
 
 
 
 
 
 
 
 
53
 
54
  }
55
 
56
- public function is_valid_coupon( $availability, $coupon ) {
57
- $selected_brands = get_post_meta( $coupon->get_ID(), '_pwb_coupon_restriction', true );
58
- if ( ! empty( $selected_brands ) ) {
59
- global $woocommerce;
60
- $products = $woocommerce->cart->get_cart();
61
- foreach ( $products as $product ) {
62
- $product_brands = wp_get_post_terms( $product['product_id'], 'pwb-brand', array( 'fields' => 'ids' ) );
63
- $valid_brands = array_intersect( $selected_brands, $product_brands );
64
- if ( ! empty( $valid_brands ) ) {
65
- return true;
66
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  }
68
- return false;
69
  }
70
- return true;
 
71
  }
72
 
73
- public function is_valid_for_product_brand( $valid, $product, $coupon, $values ) {
 
74
  if ( ! $valid ) {
75
- return false;
76
  }
77
 
78
- $coupon_id = is_callable( array( $coupon, 'get_id' ) ) ? $coupon->get_id() : $coupon->id;
79
- $selected_brands = get_post_meta( $coupon_id, '_pwb_coupon_restriction', true );
80
- if ( empty( $selected_brands ) ) {
 
 
81
  return $valid;
82
  }
83
 
@@ -86,9 +183,16 @@ class PWB_Coupon {
86
  } else {
87
  $product_id = is_callable( array( $product, 'get_id' ) ) ? $product->get_id() : $product->id;
88
  }
 
89
  $product_brands = wp_get_post_terms( $product_id, 'pwb-brand', array( 'fields' => 'ids' ) );
90
- $valid_brands = array_intersect( $selected_brands, $product_brands );
91
- return ! empty( $valid_brands );
 
 
 
 
 
 
92
  }
93
 
94
  }
9
  function __construct() {
10
  add_action( 'woocommerce_coupon_options_usage_restriction', array( $this, 'coupon_restriction' ) );
11
  add_action( 'woocommerce_coupon_options_save', array( $this, 'coupon_save' ) );
12
+ add_filter( 'woocommerce_coupon_is_valid', array( $this, 'is_valid_for_brand' ), 10, 2 );
13
+ add_filter( 'woocommerce_coupon_is_valid_for_product', array( $this, 'is_valid_coupon_for_brand' ), 10, 4 );
14
+ add_filter( 'woocommerce_coupon_is_valid', array( $this, 'is_valid_for_exclude_brand' ), 10, 2 );
15
+ add_filter( 'woocommerce_coupon_is_valid_for_product', array( $this, 'is_valid_coupon_for_exclude_brand' ), 10, 4 );
16
  }
17
 
18
  public function coupon_restriction() {
19
  global $thepostid, $post;
20
+
21
  $thepostid = empty( $thepostid ) ? $post->get_ID() : $thepostid;
22
 
23
+ $product_brands_ids = (array) get_post_meta( $thepostid, '_pwb_coupon_restriction', true );
24
+ $exclude_brands_ids = (array) get_post_meta( $thepostid, '_pwb_coupon_exclude_brands', true );
 
 
25
 
26
  ob_start();
27
+ ?>
28
+ <p class="form-field">
29
+ <label for="_pwb_coupon_restriction"><?php _e( 'Product brands', 'perfect-woocommerce-brands' ); ?></label>
30
+ <select id="_pwb_coupon_restriction" name="_pwb_coupon_restriction[]" style="width: 50%;" class="wc-enhanced-select" multiple="multiple" data-placeholder="<?php esc_attr_e( 'Any brand', 'perfect-woocommerce-brands' ); ?>">
31
+ <?php
32
+ $categories = get_terms( 'pwb-brand', 'orderby=name&hide_empty=0' );
33
+ if ( $categories ) {
34
+ foreach ( $categories as $cat ) {
35
+ echo '<option value="' . esc_attr( $cat->term_id ) . '"' . wc_selected( $cat->term_id, $product_brands_ids ) . '>' . esc_html( $cat->name ) . '</option>';
36
  }
37
+ }
38
+ ?>
39
+ </select>
40
+ <?php echo wc_help_tip( esc_html__( 'Product brands that the coupon will be applied to, or that need to be in the cart in order for the "Fixed cart discount" to be applied.', 'perfect-woocommerce-brands' ) ); ?>
41
+ </p>
42
+ <p class="form-field">
43
+ <label for="_pwb_coupon_exclude_brands"><?php _e( 'Exclude brands', 'perfect-woocommerce-brands' ); ?></label>
44
+ <select id="_pwb_coupon_exclude_brands" name="_pwb_coupon_exclude_brands[]" style="width: 50%;" class="wc-enhanced-select" multiple="multiple" data-placeholder="<?php esc_attr_e( 'Any brand', 'perfect-woocommerce-brands' ); ?>">
45
  <?php
46
+ $categories = get_terms( 'pwb-brand', 'orderby=name&hide_empty=0' );
47
+ if ( $categories ) {
48
+ foreach ( $categories as $cat ) {
49
+ echo '<option value="' . esc_attr( $cat->term_id ) . '"' . wc_selected( $cat->term_id, $exclude_brands_ids ) . '>' . esc_html( $cat->name ) . '</option>';
50
+ }
51
+ }
52
  ?>
53
+ </select>
54
+ <?php echo wc_help_tip( esc_html__( 'Product brands that the coupon will not be applied to, or that cannot be in the cart in order for the "Fixed cart discount" to be applied.', 'perfect-woocommerce-brands' ) ); ?>
55
+ </p>
56
  <?php
57
  // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
58
  echo ob_get_clean();
60
  }
61
 
62
  public function coupon_save( $post_id ) {
63
+
64
+ if ( ! array_key_exists( '_pwb_coupon_restriction', $_POST ) ) {// phpcs:ignore WordPress.Security
65
+ delete_post_meta( $post_id, '_pwb_coupon_restriction' );
66
+ } else {
67
+ update_post_meta( $post_id, '_pwb_coupon_restriction', array_filter( array_map( 'intval', $_POST['_pwb_coupon_restriction'] ) ) );// phpcs:ignore WordPress.Security
68
+ }
69
+
70
+ if ( ! array_key_exists( '_pwb_coupon_exclude_brands', $_POST ) ) {// phpcs:ignore WordPress.Security
71
+ delete_post_meta( $post_id, '_pwb_coupon_exclude_brands' );
72
+ } else {
73
+ update_post_meta( $post_id, '_pwb_coupon_exclude_brands', array_filter( array_map( 'intval', $_POST['_pwb_coupon_exclude_brands'] ) ) );// phpcs:ignore WordPress.Security
74
+ }
75
 
76
  }
77
 
78
+ public function is_valid_for_brand( $availability, $coupon ) {
79
+
80
+ $valid = true;
81
+
82
+ $product_brands_ids = get_post_meta( $coupon->get_ID(), '_pwb_coupon_restriction', true );
83
+
84
+ if ( empty( $product_brands_ids ) ) {
85
+ return $valid;
86
+ }
87
+
88
+ global $woocommerce;
89
+
90
+ $products = $woocommerce->cart->get_cart();
91
+
92
+ $valid = false;
93
+
94
+ foreach ( $products as $product ) {
95
+
96
+ $product_brands = wp_get_post_terms( $product['product_id'], 'pwb-brand', array( 'fields' => 'ids' ) );
97
+ // If we find an item with a brand in our allowed cat list, the coupon is valid.
98
+ if ( count( array_intersect( $product_brands_ids, $product_brands ) ) ) {
99
+ $valid = true;
100
+ break;
101
+ }
102
+ }
103
+
104
+ return $valid;
105
+ }
106
+
107
+ public function is_valid_coupon_for_brand( $valid, $product, $coupon, $values ) {
108
+
109
+ if ( ! $valid ) {
110
+ return $valid;
111
+ }
112
+
113
+ $coupon_id = is_callable( array( $coupon, 'get_id' ) ) ? $coupon->get_id() : $coupon->id;
114
+
115
+ $product_brands_ids = get_post_meta( $coupon_id, '_pwb_coupon_restriction', true );
116
+
117
+ if ( empty( $product_brands_ids ) ) {
118
+ return $valid;
119
+ }
120
+
121
+ if ( $product->is_type( 'variation' ) ) {
122
+ $product_id = $product->get_parent_id();
123
+ } else {
124
+ $product_id = is_callable( array( $product, 'get_id' ) ) ? $product->get_id() : $product->id;
125
+ }
126
+
127
+ $product_brands = wp_get_post_terms( $product_id, 'pwb-brand', array( 'fields' => 'ids' ) );
128
+
129
+ $valid = false;
130
+
131
+ if ( count( array_intersect( $product_brands_ids, $product_brands ) ) ) {
132
+ $valid = true;
133
+ }
134
+
135
+ return $valid;
136
+ }
137
+
138
+ public function is_valid_for_exclude_brand( $availability, $coupon ) {
139
+
140
+ $valid = true;
141
+
142
+ $exclude_brands_ids = get_post_meta( $coupon->get_ID(), '_pwb_coupon_exclude_brands', true );
143
+
144
+ if ( empty( $exclude_brands_ids ) ) {
145
+ return $valid;
146
+ }
147
+
148
+ global $woocommerce;
149
+
150
+ $products = $woocommerce->cart->get_cart();
151
+
152
+ $valid = false;
153
+
154
+ foreach ( $products as $product ) {
155
+
156
+ $product_brands = wp_get_post_terms( $product['product_id'], 'pwb-brand', array( 'fields' => 'ids' ) );
157
+ // If we find an item with a brand in our allowed cat list, the coupon is invalid.
158
+ if ( ! count( array_intersect( $exclude_brands_ids, $product_brands ) ) ) {
159
+ $valid = true;
160
+ break;
161
  }
 
162
  }
163
+
164
+ return $valid;
165
  }
166
 
167
+ public function is_valid_coupon_for_exclude_brand( $valid, $product, $coupon, $values ) {
168
+
169
  if ( ! $valid ) {
170
+ return $valid;
171
  }
172
 
173
+ $coupon_id = is_callable( array( $coupon, 'get_id' ) ) ? $coupon->get_id() : $coupon->id;
174
+
175
+ $exclude_brands_ids = get_post_meta( $coupon_id, '_pwb_coupon_exclude_brands', true );
176
+
177
+ if ( empty( $exclude_brands_ids ) ) {
178
  return $valid;
179
  }
180
 
183
  } else {
184
  $product_id = is_callable( array( $product, 'get_id' ) ) ? $product->get_id() : $product->id;
185
  }
186
+
187
  $product_brands = wp_get_post_terms( $product_id, 'pwb-brand', array( 'fields' => 'ids' ) );
188
+
189
+ $valid = false;
190
+
191
+ if ( ! count( array_intersect( $exclude_brands_ids, $product_brands ) ) ) {
192
+ $valid = true;
193
+ }
194
+
195
+ return $valid;
196
  }
197
 
198
  }
perfect-woocommerce-brands.php CHANGED
@@ -4,7 +4,7 @@
4
  * Plugin Name: Perfect Brands for WooCommerce
5
  * Plugin URI: https://quadlayers.com/portfolio/perfect-woocommerce-brands/
6
  * Description: Perfect WooCommerce Brands allows you to show product brands in your WooCommerce based store.
7
- * Version: 2.0.8
8
  * Author: QuadLayers
9
  * Author URI: https://quadlayers.com
10
  * Text Domain: perfect-woocommerce-brands
@@ -24,7 +24,7 @@
24
  * along with Perfect WooCommerce Brands. If not, see <http://www.gnu.org/licenses/>.
25
  *
26
  * WC requires at least: 3.1.0
27
- * WC tested up to: 6.4
28
  */
29
 
30
  namespace Perfect_Woocommerce_Brands;
@@ -36,7 +36,7 @@ define( 'PWB_PLUGIN_FILE', __FILE__ );
36
  define( 'PWB_PLUGIN_URL', plugins_url( '', __FILE__ ) );
37
  define( 'PWB_PLUGIN_DIR', __DIR__ . DIRECTORY_SEPARATOR );
38
  define( 'PWB_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
39
- define( 'PWB_PLUGIN_VERSION', '2.0.8' );
40
  define( 'PWB_PLUGIN_NAME', 'Perfect WooCommerce Brands' );
41
  define( 'PWB_PREFIX', 'pwb' );
42
  define( 'PWB_REVIEW_URL', 'https://wordpress.org/support/plugin/perfect-woocommerce-brands/reviews/?filter=5#new-post' );
4
  * Plugin Name: Perfect Brands for WooCommerce
5
  * Plugin URI: https://quadlayers.com/portfolio/perfect-woocommerce-brands/
6
  * Description: Perfect WooCommerce Brands allows you to show product brands in your WooCommerce based store.
7
+ * Version: 2.0.9
8
  * Author: QuadLayers
9
  * Author URI: https://quadlayers.com
10
  * Text Domain: perfect-woocommerce-brands
24
  * along with Perfect WooCommerce Brands. If not, see <http://www.gnu.org/licenses/>.
25
  *
26
  * WC requires at least: 3.1.0
27
+ * WC tested up to: 6.5
28
  */
29
 
30
  namespace Perfect_Woocommerce_Brands;
36
  define( 'PWB_PLUGIN_URL', plugins_url( '', __FILE__ ) );
37
  define( 'PWB_PLUGIN_DIR', __DIR__ . DIRECTORY_SEPARATOR );
38
  define( 'PWB_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
39
+ define( 'PWB_PLUGIN_VERSION', '2.0.9' );
40
  define( 'PWB_PLUGIN_NAME', 'Perfect WooCommerce Brands' );
41
  define( 'PWB_PREFIX', 'pwb' );
42
  define( 'PWB_REVIEW_URL', 'https://wordpress.org/support/plugin/perfect-woocommerce-brands/reviews/?filter=5#new-post' );
readme.txt CHANGED
@@ -3,11 +3,11 @@ Contributors: quadlayers, titodevera
3
  Donate link: https://quadlayers.com
4
  Tags: woocommerce, woocommerce brands, woocommerce product, woocommerce manufacturer, woocommerce supplier, e-commerce
5
  Requires at least: 4.7
6
- Tested up to: 5.9
7
  Requires PHP: 5.6
8
- Stable tag: 2.0.8
9
  WC requires at least: 3.0
10
- WC tested up to: 6.4
11
  License: GPLv3
12
  License URI: https://www.gnu.org/licenses/gpl-3.0.html
13
 
@@ -90,6 +90,8 @@ You can also contribute [translating the plugin](https://translate.wordpress.org
90
 
91
 
92
  == Changelog ==
 
 
93
  = 2.0.7 =
94
  * Fix: Brand image output sanitization
95
  = 2.0.6 =
3
  Donate link: https://quadlayers.com
4
  Tags: woocommerce, woocommerce brands, woocommerce product, woocommerce manufacturer, woocommerce supplier, e-commerce
5
  Requires at least: 4.7
6
+ Tested up to: 6.0
7
  Requires PHP: 5.6
8
+ Stable tag: 2.0.9
9
  WC requires at least: 3.0
10
+ WC tested up to: 6.5
11
  License: GPLv3
12
  License URI: https://www.gnu.org/licenses/gpl-3.0.html
13
 
90
 
91
 
92
  == Changelog ==
93
+ = 2.0.9 =
94
+ * Fix: Exclude coupons from brands
95
  = 2.0.7 =
96
  * Fix: Brand image output sanitization
97
  = 2.0.6 =