添加到购物车多个产品变异woocommerce

Goal : add to cart multiple variable products in a single button click.

I tried put this code in the loop but it did not work.

add_to_cart( $product_id, $quantity = 1, $variation_id = '', $variation = '', $cart_item_data = array() )

I found that we can add to cart by posting this link

product url/?variation_id=(variation id)8&attribute_pa_(attribute slug)=(attribute value slug)&quantity=(quantity)&add-to-cart=(product id)

But that method could not solve the problem as I need to add to cart several product variation in a single button click.

All suggestions will be appreciated. Thanks.

you can put add_to_cart( $product_id, $quantity = 1, $variation_id = '', $variation = '', $cart_item_data = array() ) in a loop, and instead of just passing product id you should also pass variation id in the second parameter.