Forum Replies Created
-
AuthorPosts
-
May 7, 2020 at 8:55 am in reply to: Don’t apply other coupon discounts to a BOGO/Free product #15562anthonyceoParticipant
Very weird. I’m 100% sure it doesn’t have this on my end. Checked $values many times and doesn’t ever contain _wjecf_free_product_coupon. That code doesn’t work on my end with free products in cart.
However, I did create code that works. A lot more complex than I anticipated. And not very efficient. It was annoying. But hey, it works.
add_filter('woocommerce_coupon_is_valid_for_product', 'coupon_is_valid_for_free_product', 10, 4); function coupon_is_valid_for_free_product($bool, $product, $coupon, $values) { // if coupon is a free product coupon, apply it. if ($coupon->get_meta('_wjecf_must_select_free_product') === 'yes') { return $bool; } // Get parent of product $parent_id = $product->get_parent_id(); if ( $parent_id > 0 ) { $parent_product = wc_get_product( $parent_id ); } else { $parent_product = $product; } // Get children ids of product $children = $parent_product->get_children(); if (empty($children)) { $children[] = $product->id; } // get cart coupons $all_coupons = WC()->cart->get_coupons(); // loop through cart coupons and don't apply non-free product coupons to free products foreach ($all_coupons as $c) { $product_ids = explode(',', $c->get_meta('_wjecf_free_product_ids')); foreach ($children as $id) { // If the current product id is in this free product cart coupon, don't apply coupon if (in_array($id, $product_ids)) { return false; } } } return $bool; }
May 7, 2020 at 4:33 am in reply to: Don’t apply other coupon discounts to a BOGO/Free product #15560anthonyceoParticipantI assume you meant the $coupon object? I don’t see that meta in the $values object.
The logical problem is that I need to know if the product has a free product coupon associated with it when it’s looping through other (non-free product) coupons. I want all other coupons to apply to everything else but the product with a free product. if I rely on _wjecf_free_product_coupon, no coupons other than the free product coupons will apply.
I can’t think of a straightforward way to do this except adding custom meta data like above. There must be another way.
May 6, 2020 at 7:47 am in reply to: Don’t apply other coupon discounts to a BOGO/Free product #15556anthonyceoParticipantThank you. It seems to work.
However, I’m determining what products have free products through custom meta data. In this case, the meta data for free products is called hide_sub.
I don’t want to do it this way. It also won’t work for buy 2 get 1 free, etc. I want to be able to determine programmatically if any product has an auto-applied free product coupon. Is this possible? Thanks.
add_filter('woocommerce_coupon_is_valid_for_product', 'free_product_discount_coupon', 10, 4); function free_product_discount_coupon($bool, $product, $coupon, $values) { // Get parent product id for variable products. // Required to retrieve custom meta data. $parent_id = $product->get_parent_id(); // Create a parent product from the parent id or // set $parent_product = $product if the product is the parent product. if ( $parent_id > 0 ) { $parent_product = wc_get_product( $parent_id ); } else { $parent_product = $product; } // if coupon is a free product coupon, apply it. if ($coupon->get_meta('_wjecf_must_select_free_product') === 'yes') { return $bool; } else if ($parent_product->get_meta('hide_sub') == 1) { // Don't allow any other coupons to be applied // to products with free product coupons (hide_sub = 1). return false; } return $bool; }
May 5, 2020 at 8:25 pm in reply to: Don’t apply other coupon discounts to a BOGO/Free product #15554anthonyceoParticipantHmmm, the $coupon object isn’t in that filter. Just $bool and $product. Scratching my head.
anthonyceoParticipantThank you for your help. 🙂
Here’s the simple code for others trying to do the same thing.
Unfortunately, the coupon object doesn’t declare whether the coupon has a free item.However, all of our coupons for Free Products have a discount amount of 0.add_filter('woocommerce_apply_with_individual_use_coupon', 'force_bogo_coupon', 10, 2); function force_bogo_coupon($bool, $coupon) { $amount = $coupon->amount; $bool = ($amount == 0) ? true : $bool; return $bool; }
Edit:
this code is more specific to a free product:
add_filter('woocommerce_apply_with_individual_use_coupon', 'force_bogo_coupon', 10, 2); function force_bogo_coupon($bool, $coupon) { $meta = $coupon->get_meta('_wjecf_must_select_free_product'); $bool = ($meta === 'yes') ? true : $bool; return $bool; }
anthonyceoParticipantThank you.
filter_woocommerce_apply_individual_use_coupon( $array, $the_coupon, $this_applied_coupons )
What is $array for?
Is info whether or not a coupon has free products stored in $the_coupon?
(e.g. $the_coupon->free_products or something like that) -
AuthorPosts