I am trying to change the cart content based on the quantity of a product in the cart. I only have 5 products in the shop:
- Product_1 => 1 panel,
- Product_2 => 12 panels,
- Product_3 => 18 panels,
- Product_4 => 30 panels,
- Product_5 => 60 panels,
They are configured as different products, so no bundles, kits or anything. More than one panel products are obviously cheaper than adding a number of single panels separately.
Then I have also a custom product that calculates the number of panels needed based on floor measures given by the customer and then adds to cart the necessary quantity of single panels.
I Would like to change dynamically the content of the cart when it's reviewed.
So for example, if the floor configurator calculate 54 single panels and added them to the cart, I would like to change the cart item to:
- 1 Product_4 (30 panels)
- 1 Product_2 (12 panels)
- 2 Product_1 (1 panel)
- and print a message stating the change.
I've checked different solutions but none of them offer this kind of feature:
- How to bundle products with WooCommerce
- WooCommerce Product Bundles - Bulk Discounts
- WooCommerce Dynamic Pricing & Discounts
So, based on this answer thread, I think I need to use hook
woocommerce_before_calculate_totals and implement the logic, but need some help since I am a wordpress dev noob.
Note that the add to cart button is called with Ajax in the floor configurator with:
jQuery("button.ajax_add_to_cart").attr("data-quantity", parseInt(numLozasrealW * numLozasrealH));
Any help is appreciated.