how to add custom product accessory to product detail page in wordpress(woocommerce)?

enter image description here

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.

Read More

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.

Related posts

Leave a Reply

2 comments