Use below Code to or Automatic Addition of Product along with Other. if you added one product and wanted to add one more with this product use following code for this.
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’];
if ( $_product->id == $product_id ){
$found = true;
}
//prasa
/* for conditional
$addon=95; $addon2=93;
if ( $_product->id ==$addon || $_product->id==$addon2)
{
$woocommerce->cart->add_to_cart( $product_id);
} */
//prasa
}
// if product not found, add it
if ( $found )
$woocommerce->cart->add_to_cart( $free_product_id );
}
}
}