Add duplicate product item to cart with modified price
-
I’m trying to implement a buy one get one, bogo, deal on a product category by adding a duplicate of the item to the cart when the product is added or the cart is modified. I have this working with synchronized quantities, but I’m stuck on how to make the free items free while the originals maintain their normal price.
An example of a cart with this working (the bogo is applied to Category B):
Product 5, Category A, quantity 1, $9.99
Product 2, Category A, quantity 3, $6.50
Product 16, Category B, quantity 8, $2.99
Product 12, Category B, quantity 1, $10.99
Product 16, Category B, quantity 8, $0.00
Product 12, Category B, quantity 1, $0.00When I use
$cart_item->set_price()
, where$cart_item
is an item inWC()->cart->get_cart()
orWC()->cart->cart_contents
, the price appears to be set for all products in the cart with the same product/variation ID. I’ve tried lots of ways to modify only the free ones, for which I set meta data to help differentiate, but they either don’t work or run into this problem of setting all of the items belonging to the same product.
I’ve considered manipulating$cart_item['data']['changes']['price']
more directly and with different ways of indexing, butset_prop()
is a protected function of the product itself and I’m not sure how to get around that, if it would even work.
I’ll need to continue learning more about PHP and WooCommerce, but this problem keeps getting more complicated the deeper I go into it, so I’m asking for help.
- The topic ‘Add duplicate product item to cart with modified price’ is closed to new replies.