Repository › Forums › Support › WooCommerce Extended Coupon Features PRO › composite products count
- This topic has 9 replies, 2 voices, and was last updated 9 months, 2 weeks ago by almostretired.
-
AuthorPosts
-
December 8, 2022 at 4:25 pm #19423almostretiredParticipant
Hi, we are using WooCommerce Extended Coupon Features PRO with WooCommerce Composite Products
Can Coupons based on the cart quantity exclude the component quantity of composite products while including them for a percentage discount?
The problem comes up when we have a discount for a specific quantity. When someone chooses a composite product, it is considered 2 or more products in the cart even tho it is only one product.
We would like to apply a quantity discount. Can you help us do this?
Thanks.
December 8, 2022 at 5:41 pm #19425Soft79KeymasterCan you send us a screenshot of a cart with composite products?
December 8, 2022 at 5:59 pm #19426almostretiredParticipantYes, here’s the screenshot. https://capture.dropbox.com/5aRbS53GGbovkDH3
In this image, the product “Amateur Radio Coax Builder” is one product. That product is made up of 1 wire with a choice of the number of feet, and up to two connectors. The cart is applying a 3+ discount since it is counting each foot of wire and each connector.
December 8, 2022 at 10:43 pm #19427Soft79KeymasterI’m not sure if this will work, this snippet will not count products of which the price is 0. Replace ‘my_coupon_code’ with the actual coupon code. If this snippet doesn’t do anything please send the snippet to the author of the Composite Product plugin and ask how we can detect that it’s a ‘child’-product of a composite product. Maybe the $values array contains an item that tells us the item is part of a composite product.
add_filter( 'woocommerce_coupon_is_valid_for_product', function ( $valid, $product, $coupon, $values ) { if ( $coupon->get_code() === 'my_coupon_code' && $product->get_price() == 0 ) { $valid = false; } return $valid; }, 10, 4 );
December 9, 2022 at 1:32 pm #19430almostretiredParticipantWe’re using the official WooCommerce Composite Products plugin. https://woocommerce.com/products/composite-products/
The ‘composite_parent’ parent property should do what we need. For our purposes we don’t want to check specific coupon codes, since I doubt the client will ever include components in the quantity.
Page with info: https://woocommerce.com/document/composite-products-data-structures-storage/
We haven’t found the right combination of hooks and functions at the moment.
Excluding by price doesn’t work because our component items alter the price. We would end up with a free product and a discount.
December 9, 2022 at 3:09 pm #19431Soft79KeymasterGreat info. Then this snippet should work. Make sure to replace my_coupon_code. Please note that the snippet is untested.
add_filter( 'woocommerce_coupon_is_valid_for_product', function ( $valid, $product, $coupon, $values ) { if ( $coupon->get_code() === 'my_coupon_code' && array_key_exists( 'composite_parent', $values ) ) { $valid = false; } return $valid; }, 10, 4 );
December 9, 2022 at 8:40 pm #19433almostretiredParticipantThis gives the same result as excluding based on the category. The count matches, but the percentage discount is based on the two included items only. Toggling Allow discount on the cart with excluded items does not change this behavior.
December 10, 2022 at 1:01 pm #19437Soft79KeymasterThat’s too bad. Our plugin can only discount products that are also considered for the total quantity of items. We could introduce a filter in the next version that can be used to alter the quantity; same as above snippet, but it will not change the validity of the coupon for the product.
I will see if we can fit this in next week, maybe not an official release, but a preliminary release that you can already start using.
December 10, 2022 at 2:17 pm #19440Soft79KeymasterOk, why wait? I’ve published 3.3.3-dev.1 which you can download on your account page https://www.soft79.nl/my-account/ .
It should work with this updated snippet:
add_filter( 'wjecf_coupon_item_quantity', function ( $item_quantity, $coupon, $item, $wc_discounts ) { if ( $coupon->get_code() === 'my_coupon_code' && array_key_exists( 'composite_parent', $item->object ) ) { $item_quantity = 0; } return $item_quantity; }, 10, 4 );
December 15, 2022 at 8:21 pm #19452almostretiredParticipantSorry for the delay on this. Thanks so much for working this out. We did change one line of the code for our purposes.
if ( array_key_exists( ‘composite_parent’, $item->object ) ) {
I don’t think our client will ever make a sale on quantities that includes the components, so this version should be easier than checking for specific codes.
This works! Thanks again for all of your help!
-
AuthorPosts
- You must be logged in to reply to this topic.