Hello friends. i was testing to add the code for attached products.
Ex Product A if added automatically Products B will add
and Ex: If Product C is adding then it will check Product A is added or Not.
Here is simple script i have written to use in wordpress wocommerce.
add_action( ‘init’, ‘add_product_to_cart’ );
function add_product_to_cart()
{
if ( ! is_admin() )
{
global $woocommerce;
$product_id = 187; //Your product ID here
$free_product_id = 190; //Free product ID here
$found = false;
//check if product already in cart
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 )
{
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values )
{
$_product = $values[‘data’];
$found=true;
}
if($found)
{
if ( $_product->id == $product_id )
{
//$found = true;
$woocommerce->cart->add_to_cart( $free_product_id );}
else
{
$addon=95; $addon2=93;
if ( $_product->id ==$addon || $_product->id==$addon2)
{
$woocommerce->cart->add_to_cart( $product_id);
}
}
}
}}
}