abstracts
1 month ago
admin
1 week ago
blocks
11 months ago
cli
1 month ago
customizer
4 months ago
data-stores
1 month ago
emails
1 week ago
export
1 year ago
gateways
1 month ago
import
3 months ago
integrations
2 months ago
interfaces
4 months ago
legacy
4 months ago
libraries
1 month ago
log-handlers
1 year ago
payment-tokens
5 years ago
product-usage
1 year ago
queue
4 months ago
react-admin
1 month ago
rest-api
1 month ago
shipping
3 months ago
shortcodes
1 month ago
theme-support
2 years ago
tracks
1 month ago
traits
5 years ago
walkers
5 years ago
wccom-site
1 month ago
widgets
2 months ago
class-wc-ajax.php
1 month ago
class-wc-auth.php
1 year ago
class-wc-autoloader.php
9 months ago
class-wc-background-emailer.php
2 months ago
class-wc-background-updater.php
5 years ago
class-wc-brands-brand-settings-manager.php
1 year ago
class-wc-brands-coupons.php
1 year ago
class-wc-brands.php
1 month ago
class-wc-breadcrumb.php
4 months ago
class-wc-cache-helper.php
1 month ago
class-wc-cart-fees.php
2 years ago
class-wc-cart-session.php
3 months ago
class-wc-cart-totals.php
11 months ago
class-wc-cart.php
1 month ago
class-wc-checkout.php
1 month ago
class-wc-cli.php
10 months ago
class-wc-comments.php
4 months ago
class-wc-countries.php
2 months ago
class-wc-coupon.php
2 months ago
class-wc-customer-download-log.php
5 years ago
class-wc-customer-download.php
1 year ago
class-wc-customer.php
5 days ago
class-wc-data-exception.php
8 years ago
class-wc-data-store.php
3 years ago
class-wc-datetime.php
4 years ago
class-wc-deprecated-action-hooks.php
2 years ago
class-wc-deprecated-filter-hooks.php
3 months ago
class-wc-discounts.php
11 months ago
class-wc-download-handler.php
1 month ago
class-wc-emails.php
1 month ago
class-wc-embed.php
1 year ago
class-wc-form-handler.php
4 months ago
class-wc-frontend-scripts.php
2 months ago
class-wc-geo-ip.php
9 months ago
class-wc-geolite-integration.php
6 years ago
class-wc-geolocation.php
2 months ago
class-wc-https.php
3 years ago
class-wc-install.php
1 week ago
class-wc-integrations.php
5 years ago
class-wc-log-levels.php
2 years ago
class-wc-logger.php
4 months ago
class-wc-meta-data.php
1 month ago
class-wc-order-factory.php
2 months ago
class-wc-order-item-coupon.php
4 years ago
class-wc-order-item-fee.php
5 months ago
class-wc-order-item-meta.php
4 years ago
class-wc-order-item-product.php
1 month ago
class-wc-order-item-shipping.php
5 months ago
class-wc-order-item-tax.php
1 month ago
class-wc-order-item.php
1 month ago
class-wc-order-query.php
1 month ago
class-wc-order-refund.php
1 year ago
class-wc-order.php
1 month ago
class-wc-payment-gateways.php
1 month ago
class-wc-payment-tokens.php
3 years ago
class-wc-post-data.php
2 months ago
class-wc-post-types.php
1 month ago
class-wc-privacy-background-process.php
1 year ago
class-wc-privacy-erasers.php
10 months ago
class-wc-privacy-exporters.php
4 years ago
class-wc-privacy.php
1 year ago
class-wc-product-attribute.php
4 months ago
class-wc-product-download.php
4 months ago
class-wc-product-external.php
1 year ago
class-wc-product-factory.php
3 months ago
class-wc-product-grouped.php
3 months ago
class-wc-product-query.php
4 months ago
class-wc-product-simple.php
11 months ago
class-wc-product-variable.php
1 month ago
class-wc-product-variation.php
1 month ago
class-wc-query.php
1 month ago
class-wc-rate-limiter.php
4 years ago
class-wc-regenerate-images-request.php
3 years ago
class-wc-regenerate-images.php
2 years ago
class-wc-register-wp-admin-settings.php
5 years ago
class-wc-rest-authentication.php
2 years ago
class-wc-rest-exception.php
5 years ago
class-wc-session-handler.php
3 months ago
class-wc-shipping-rate.php
1 year ago
class-wc-shipping-zone.php
5 years ago
class-wc-shipping-zones.php
7 months ago
class-wc-shipping.php
2 months ago
class-wc-shortcodes.php
1 year ago
class-wc-structured-data.php
1 month ago
class-wc-tax.php
2 months ago
class-wc-template-loader.php
8 months ago
class-wc-tracker.php
1 month ago
class-wc-validation.php
2 years ago
class-wc-webhook.php
1 month ago
class-woocommerce.php
5 days ago
wc-account-functions.php
1 month ago
wc-attribute-functions.php
1 month ago
wc-brands-functions.php
1 year ago
wc-cart-functions.php
1 month ago
wc-conditional-functions.php
11 months ago
wc-core-functions.php
2 months ago
wc-coupon-functions.php
5 months ago
wc-deprecated-functions.php
1 month ago
wc-formatting-functions.php
1 month ago
wc-interactivity-api-functions.php
2 months ago
wc-notice-functions.php
5 months ago
wc-order-functions.php
2 months ago
wc-order-item-functions.php
3 years ago
wc-order-step-logger-functions.php
4 months ago
wc-page-functions.php
2 months ago
wc-product-functions.php
2 months ago
wc-rest-functions.php
7 months ago
wc-stock-functions.php
7 months ago
wc-template-functions.php
1 month ago
wc-template-hooks.php
10 months ago
wc-term-functions.php
2 months ago
wc-update-functions.php
1 week ago
wc-user-functions.php
1 month ago
wc-webhook-functions.php
1 month ago
wc-widget-functions.php
5 years ago
class-wc-brands-coupons.php
192 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1); |
| 4 | |
| 5 | use Automattic\WooCommerce\Enums\ProductType; |
| 6 | |
| 7 | /** |
| 8 | * WC_Brands_Coupons class. |
| 9 | * |
| 10 | * Important: For internal use only by the Automattic\WooCommerce\Internal\Brands package. |
| 11 | * |
| 12 | * @version 9.4.0 |
| 13 | */ |
| 14 | class WC_Brands_Coupons { |
| 15 | |
| 16 | const E_WC_COUPON_EXCLUDED_BRANDS = 301; |
| 17 | |
| 18 | /** |
| 19 | * Constructor |
| 20 | */ |
| 21 | public function __construct() { |
| 22 | // Coupon validation and error handling. |
| 23 | add_filter( 'woocommerce_coupon_is_valid', array( $this, 'is_coupon_valid' ), 10, 3 ); |
| 24 | add_filter( 'woocommerce_coupon_is_valid_for_product', array( $this, 'is_valid_for_product' ), 10, 3 ); |
| 25 | add_filter( 'woocommerce_coupon_error', array( $this, 'brand_exclusion_error' ), 10, 2 ); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Validate the coupon based on included and/or excluded product brands. |
| 30 | * |
| 31 | * If one of the following conditions are met, an exception will be thrown and |
| 32 | * displayed as an error notice on the cart page: |
| 33 | * |
| 34 | * 1) Coupon has a brand requirement but no products in the cart have the brand. |
| 35 | * 2) All products in the cart match the brand exclusion rule. |
| 36 | * 3) For a cart discount, there is at least one product in cart that matches exclusion rule. |
| 37 | * |
| 38 | * @throws Exception Throws Exception for invalid coupons. |
| 39 | * @param bool $valid Whether the coupon is valid. |
| 40 | * @param WC_Coupon $coupon Coupon object. |
| 41 | * @param WC_Discounts $discounts Discounts object. |
| 42 | * @return bool $valid True if coupon is valid, otherwise Exception will be thrown. |
| 43 | */ |
| 44 | public function is_coupon_valid( $valid, $coupon, $discounts = null ) { |
| 45 | $this->set_brand_settings_on_coupon( $coupon ); |
| 46 | |
| 47 | // Only check if coupon has brand restrictions on it. |
| 48 | $brand_coupon_settings = WC_Brands_Brand_Settings_Manager::get_brand_settings_on_coupon( $coupon ); |
| 49 | |
| 50 | $brand_restrictions = ! empty( $brand_coupon_settings['included_brands'] ) || ! empty( $brand_coupon_settings['excluded_brands'] ); |
| 51 | if ( ! $brand_restrictions ) { |
| 52 | return $valid; |
| 53 | } |
| 54 | |
| 55 | $included_brands_match = false; |
| 56 | $excluded_brands_matches = 0; |
| 57 | |
| 58 | $items = $discounts->get_items(); |
| 59 | |
| 60 | foreach ( $items as $item ) { |
| 61 | $product_brands = $this->get_product_brands( $this->get_product_id( $item->product ) ); |
| 62 | |
| 63 | if ( ! empty( array_intersect( $product_brands, $brand_coupon_settings['included_brands'] ) ) ) { |
| 64 | $included_brands_match = true; |
| 65 | } |
| 66 | |
| 67 | if ( ! empty( array_intersect( $product_brands, $brand_coupon_settings['excluded_brands'] ) ) ) { |
| 68 | ++$excluded_brands_matches; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // 1) Coupon has a brand requirement but no products in the cart have the brand. |
| 73 | if ( ! $included_brands_match && ! empty( $brand_coupon_settings['included_brands'] ) ) { |
| 74 | throw new Exception( $coupon->get_coupon_error( WC_Coupon::E_WC_COUPON_NOT_APPLICABLE ), WC_Coupon::E_WC_COUPON_NOT_APPLICABLE ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 75 | } |
| 76 | |
| 77 | // 2) All products in the cart match brand exclusion rule. |
| 78 | if ( count( $items ) === $excluded_brands_matches ) { |
| 79 | throw new Exception( __( 'Sorry, this coupon is not applicable to the brands of selected products.', 'woocommerce' ), self::E_WC_COUPON_EXCLUDED_BRANDS ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 80 | } |
| 81 | |
| 82 | // 3) For a cart discount, there is at least one product in cart that matches exclusion rule. |
| 83 | if ( $coupon->is_type( 'fixed_cart' ) && $excluded_brands_matches > 0 ) { |
| 84 | throw new Exception( __( 'Sorry, this coupon is not applicable to the brands of selected products.', 'woocommerce' ), self::E_WC_COUPON_EXCLUDED_BRANDS ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 85 | } |
| 86 | |
| 87 | return $valid; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Check if a coupon is valid for a product. |
| 92 | * |
| 93 | * This allows percentage and product discounts to apply to only |
| 94 | * the correct products in the cart. |
| 95 | * |
| 96 | * @param bool $valid Whether the product should get the coupon's discounts. |
| 97 | * @param WC_Product $product WC Product Object. |
| 98 | * @param WC_Coupon $coupon Coupon object. |
| 99 | * @return bool $valid |
| 100 | */ |
| 101 | public function is_valid_for_product( $valid, $product, $coupon ) { |
| 102 | |
| 103 | if ( ! is_a( $product, 'WC_Product' ) ) { |
| 104 | return $valid; |
| 105 | } |
| 106 | $this->set_brand_settings_on_coupon( $coupon ); |
| 107 | |
| 108 | $product_id = $this->get_product_id( $product ); |
| 109 | $product_brands = $this->get_product_brands( $product_id ); |
| 110 | |
| 111 | // Check if coupon has a brand requirement and if this product has that brand attached. |
| 112 | $brand_coupon_settings = WC_Brands_Brand_Settings_Manager::get_brand_settings_on_coupon( $coupon ); |
| 113 | if ( ! empty( $brand_coupon_settings['included_brands'] ) && empty( array_intersect( $product_brands, $brand_coupon_settings['included_brands'] ) ) ) { |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | // Check if coupon has a brand exclusion and if this product has that brand attached. |
| 118 | if ( ! empty( $brand_coupon_settings['excluded_brands'] ) && ! empty( array_intersect( $product_brands, $brand_coupon_settings['excluded_brands'] ) ) ) { |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | return $valid; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Display a custom error message when a cart discount coupon does not validate |
| 127 | * because an excluded brand was found in the cart. |
| 128 | * |
| 129 | * @param string $err The error message. |
| 130 | * @param string $err_code The error code. |
| 131 | * @return string |
| 132 | */ |
| 133 | public function brand_exclusion_error( $err, $err_code ) { |
| 134 | if ( self::E_WC_COUPON_EXCLUDED_BRANDS !== $err_code ) { |
| 135 | return $err; |
| 136 | } |
| 137 | |
| 138 | return __( 'Sorry, this coupon is not applicable to the brands of selected products.', 'woocommerce' ); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Get a list of brands that are assigned to a specific product |
| 143 | * |
| 144 | * @param int $product_id Product id. |
| 145 | * @return array brands |
| 146 | */ |
| 147 | private function get_product_brands( $product_id ) { |
| 148 | return wp_get_post_terms( $product_id, 'product_brand', array( 'fields' => 'ids' ) ); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Set brand settings as properties on coupon object. These properties are |
| 153 | * lists of included product brand IDs and list of excluded brand IDs. |
| 154 | * |
| 155 | * @param WC_Coupon $coupon Coupon object. |
| 156 | * |
| 157 | * @return void |
| 158 | */ |
| 159 | private function set_brand_settings_on_coupon( $coupon ) { |
| 160 | $brand_coupon_settings = WC_Brands_Brand_Settings_Manager::get_brand_settings_on_coupon( $coupon ); |
| 161 | |
| 162 | if ( ! empty( $brand_coupon_settings['included_brands'] ) && ! empty( $brand_coupon_settings['excluded_brands'] ) ) { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | $included_brands = get_post_meta( $coupon->get_id(), 'product_brands', true ); |
| 167 | if ( empty( $included_brands ) ) { |
| 168 | $included_brands = array(); |
| 169 | } |
| 170 | |
| 171 | $excluded_brands = get_post_meta( $coupon->get_id(), 'exclude_product_brands', true ); |
| 172 | if ( empty( $excluded_brands ) ) { |
| 173 | $excluded_brands = array(); |
| 174 | } |
| 175 | |
| 176 | // Store these for later to avoid multiple look-ups. |
| 177 | WC_Brands_Brand_Settings_Manager::set_brand_settings_on_coupon( $coupon ); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Returns the product (or variant) ID. |
| 182 | * |
| 183 | * @param WC_Product $product WC Product Object. |
| 184 | * @return int Product ID |
| 185 | */ |
| 186 | private function get_product_id( $product ) { |
| 187 | return $product->is_type( ProductType::VARIATION ) ? $product->get_parent_id() : $product->get_id(); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | new WC_Brands_Coupons(); |
| 192 |