How to add a paid product as free in WooCommerce without plugin?

Photo of author
Written By geekerhub

Lorem ipsum dolor sit amet consectetur pulvinar ligula augue quis venenatis. 

Let take an example such that When user add a specific product to the cart, then there must be product that will automatically added to cart and it must be free.

IT MUST BE FREE means that 100% coupon code must apply using the snippet. To make this working, add below snippet to your theme’s functions.php

function geekerhub_add_free_product_callback( $data, $product_id, $quantity ) {
global $woocommerce;
$product = wc_get_product( $product_id );

/* Fetch metadata of the product which is added in a cart to check free product status*/
$add_free_product = get_post_meta($product_id, 'add_free_product_to_cart', true);
/* SKU of the free product */
$freeproductsku = FREEPRODUCTSKU;
/*100% coupon to apply when free product is added*/
$coupon_code = COUPONCODE; 

if ( $add_free_product == 'yes' ) {
   WC()->cart->add_to_cart( wc_get_product_id_by_sku($freeproductsku) );
   wc_add_notice( __( 'Hey, Free product is added to the cart on purchase of product', 'woocommerce' ), 'success' );
   $woocommerce->cart->add_discount( $coupon_code );
return $data;

add_filter( 'woocommerce_add_to_cart_validation', 'geekerhub_add_free_product_callback', 10, 3 );

Leave a Comment