I want to add this accessories drop-down in product detail page. if user add any accessory then accessory price should also add to cart.
Here is my code:
add_action('woocommerce_add_to_cart', 'custome_add_to_cart');
$cnt=2
function custome_add_to_cart() {
global $woocommerce;
$cnt = $cnt + 1;
echo $cnt."X";
echo $p_id=$_POST['assessories'];
$woocommerce->cart->add_to_cart($p_id, 1);
}
Output: It is adding the same drop-down item many times into the cart, but I want only 1 quantity to add to cart. It seems that add_to_cart function runs many times. What should I do or how to add filter with passing second drop-down product as parameter to add to cart function? So I can add this product also to the cart.
Unfortunately, there’s currently no free plugin that can do this… however, you can buy WooCommerce Product Add-ons to have this kind of functionality.
You can use plugin WooCommerce Extra Product Options
It’s best solution to add custom field on Woocommerce product page