automatically a free product to the cart depending on a particular product id?
-
I am trying add a free product depending on some products added in the cart or not by the product ids. I found some code on depending on category and tried to change it in product id but I am not able to save it getting 403 Forbidden Access to this resource on the server is denied! error.
`/**
* Automatically adding the product to the cart.
*/
function aaptc_add_product_to_cart( $item_key, $product_id ) {$product_ids = array( 5237, 5236 ); //
$product_id_check = false;if ( ! WC()->cart->is_empty() ) {
// Loop though cart items
foreach(WC()->cart->get_cart() as $cart_item ) {
// Handling also variable products and their products variations
$cart_item_ids = array($cart_item[‘product_id’], $cart_item[‘variation_id’]);// Handle a simple product Id (int or string) or an array of product Ids
if( ( is_array($product_ids) && array_intersect($product_ids, cart_item_ids) )
|| ( !is_array($product_ids) && in_array($product_ids, $cart_item_ids) ) )
$product_id_check = true;
}
}if ( ! is_admin() && $product_id_check ) { // if front end and product is added to cart having category A or B or C.
$free_product_id = 5553; // Product Id of the free product which will get added to cartWC()->cart->add_to_cart( $free_product_id,2 );
}}
add_action( ‘woocommerce_add_to_cart’, ‘aaptc_add_product_to_cart’, 10, 2 );Not able to save this code and run further, plz help.
- The topic ‘automatically a free product to the cart depending on a particular product id?’ is closed to new replies.