Programmatically add a product to the cart and set price to zero when buying from certain category

Question

WordPress/Woocommerce related question.

I want to programmatically add a ‘free gift’ to the cart once an item from a certain category has been added. This free gift is actually a product they can buy from the store separately as well, but when combining it with an item from said category, will act as a free gift.

So far I have a working code that adds the extra product once an item from the specific category has been added. But I can’t figure out how to set the price of this item to zero in the cart.

/*
* Automatically adding the product to the cart.
*/

function aaptc_add_product_to_cart( $item_key, $product_id ) {

    $product_category_id    = 92; // set category id

    $product_cats_ids   = wc_get_product_term_ids( $product_id, 'product_cat' );

    if ( ! is_admin() && in_array( $product_category_id, $product_cats_ids ) ) {
        $free_product_id = 7506;  // Product Id of the free product which will get added to cart
        $found      = false;

        //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() == $free_product_id )
                    $found = true;
                
            }
            // if product not found, add it
            if ( ! $found )
                WC()->cart->add_to_cart( $free_product_id );
                        function return_custom_price($price, $product) {
                            $myPrice = 0;

                            global $current_user;
                            $price = $myPrice;
                            $post_id = $post->$free_product_id;
                            return $price;
                        }
                        add_filter('woocommerce_get_price', 'return_custom_price', 10, 2);
                        
        } else {
            // if no products in cart, add it
            WC()->cart->add_to_cart( $free_product_id );
                        function return_custom_price($price, $product) {
                            $myPrice = 0;

                            global $current_user;
                            $price = $myPrice;
                            $post_id = $post->$free_product_id;
                            return $price;
                        }
                        add_filter('woocommerce_get_price', 'return_custom_price', 10, 2);
        }        
    }    
}

add_action( 'woocommerce_add_to_cart', 'aaptc_add_product_to_cart', 10, 2 );

I’m pretty sure the following part of the snippet is programmatically wrong, but I am unsure how to achieve a zero price of the free added product.

function return_custom_price($price, $product) {
                            $myPrice = 0;

                            global $current_user;
                            $price = $myPrice;
                            $post_id = $post->$free_product_id;
                            return $price;
                        }
add_filter('woocommerce_get_price', 'return_custom_price', 10, 2);
0
Jordy Steyaert 2 months 0 Answers 9 views 0

Leave an answer