One of our clients want to provide a free product ( Price: $0 ) to their customers if they have some products already in their cart. Custom code looked prominent solution for that use case. We have searched in google and found some logic with code and we have altered it as per our use case
We have implemented as below
1) Add that particular free product to user’s cart once user add any product to their cart
2) Check and remove that particular free product if user removed all products
from their cart ( It prevents user to order only that free product without any other products in the cart )
Step 1: Adding Free product to cart once user add any product to their cart:
The below code will added that free product to cart once user add any product to their cart. If that free product was added already, then it won’t add that free product once again.
function woocustom_add_product_to_cart() { if( ! is_admin() ) { $product_id = 12345; // Product Id of the free product which will get added to cart $is_present = false; $cart = WC()->cart->get_cart(); //check if product already in cart if ( sizeof( WC()->cart->get_cart() ) > 0 ) { foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) { $_product = $values['data']; if ( $_product->get_id() == $product_id ){ $is_present = true; } } // if free product not found, add it if ( ! $is_present ){ WC()->cart->add_to_cart( $product_id ); } } } } add_action( 'woocommerce_add_to_cart', 'woocustom_add_product_to_cart', 10, 2 );
Step 2: Check and remove that particular free product if user removed all products from their cart
function remove_free_product_from_cart() { // Run only in the Cart or Checkout Page if( is_cart() || is_checkout() ) { $prod_to_remove = 12345; // Product ID of Free Product $no_any_product_check = false; $cart = WC()->cart->get_cart(); if ( sizeof( $cart ) == 1 ) { // Check in the cart if any other product is present or not foreach ( WC()->cart->cart_contents as $prod_in_cart ) { // Get the Variation or Product ID $prod_id = ( isset( $prod_in_cart['variation_id'] ) && $prod_in_cart['variation_id'] != 0 ) ? $prod_in_cart['variation_id'] : $prod_in_cart['product_id']; if ( $prod_to_remove == $prod_id ) { $free_pro_cart_id = WC()->cart->generate_cart_id( $prod_id ); $no_any_product_check = true; } } // If any product is not present then remove the free product. if ( $no_any_product_check ) { unset( WC()->cart->cart_contents[ $free_pro_cart_id ] ); } } } } add_action( 'template_redirect', 'remove_free_product_from_cart' );
The above code will check the user cart when they visit cart page or checkout page. If they have other product apart from free product in their cart, then it will allow to proceed on checkout otherwise, it will remove that free product from the cart and cart will be empty.
If you like to use these code, change that value of variables ‘$product_id’ and ‘$prod_to_remove’ to your free product id and paste these codes in your theme functions.php.
3 comments
Tremoço
Hello, is it possible to have the same function but instead of getting the free product just for adding another one to the cart, getting it when reaching a certain amount, like let’s say 60€?
Many thanks, cheers!
Pau
Where do you put this code?
eryu456usr
Hi, you can add it on your theme functions.php file.