Ecommerce Shopify WordPress Discussion

Add or remove WooCommerce product from cart if another product is added or removed

I found this here: Remove product A from cart if product B is removed from cart on WooCoomerce which works to add Product B when Product A is added to the cart. I'm using WPCode Snippet. add_action( 'woocommerce_add_cart_item_data', function( $cart_item_data, $product_id ) { //If added product ID is 67 then also add product with an ID of 15 if ( 67 == $product_id ) { WC()->cart->add_to_cart( 15 ); } }, 10, 2 ); However, the answer given doesn't work when both codes are active. I have them a two separate code snippets. When I add Product A, it's the only item in the cart. It doesn't add Product B now. Do both codes need to be in one snippet (possibly)? Or simply doesn't work. Also, I need to use ID's because I've created an eLearning site which is not WooCommerce (Product A) but Product B is a WooCommerce product. It uses the WooCommerce cart to checkout. add_action( 'woocommerce_remove_cart_item', 'jhall_remove_product_from_cart', 10, 2 ); /** * Remove Product ID 15 when Product ID 67 is removed from the cart * * @param $item_key * @param $cart * @return $item_key */ function jhall_remove_product_from_cart( $item_key, $cart ){ //Get the ID of the product removed $removed_id = $cart->cart_contents[$item_key]['product_id']; //If the Product ID is 67 if( $removed_id === 67 ){ //You need a cart key for the product you want to remove $remove_from_cart_product_id = $cart->generate_cart_id( 15 ); //Remove Product ID 15 from the cart $cart->remove_cart_item( $remove_from_cart_product_id ); }
Try the following code replacement: add_action('woocommerce_before_calculate_totals', 'action_before_cart_calculate_totals'); function action_before_cart_calculate_totals( $cart ) { if ((is_admin() && !defined('DOING_AJAX'))) return; $targeted_product_id = 67; // Define the targeted product ID $linked_product_id = 15; // Define the linked product ID that should be added (complementary) $linked_item_key = $targeted_found = false; // Initialized variables // Check cart items foreach ( $cart->get_cart() as $item_key => $item ) { // Check if our targeted product is in cart if ( $item['product_id'] == $targeted_product_id ) { $targeted_found = true; } // Check if the complementary product is in cart elseif ( $item['product_id'] == $linked_product_id ) { $linked_item_key = $item_key; } } if ( $targeted_found && ! $linked_item_key ) { $cart->add_to_cart( $linked_product_id ); // Add to cart the complementary product } elseif ( ! $targeted_found && $linked_item_key ) { $cart->remove_cart_item($linked_item_key); // Remove from cart the complementary product } } Code goes in functions.php file of your child theme (or in a plugin). Tested and works.

January 7, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.