Search code examples
woocommerceproductcomposite

WooCommerce composite product plugin add to cart hook with quantity


We are working with add to cart hooks a lot in WooCommerce so we are able to add products to our carts via our own plugins without messing around in the functions.php all too much. But this time I think we came to our limits.

What we use is the WooCommerce Composite Products plugin and we have added multiple products into one composite product which ID is 123.

By creating a link like this, we can add the composite product into our cart and select the components as we want:

https://www.example.com/cart/?add-to-cart=123&wccp_component_selection[1593767685]=479&wccp_component_selection[1593767245]=289&wccp_component_selection[1593766620]=288&wccp_component_selection[1593436878]=284&wccp_component_selection[1593436879]=283

The challenge now is that we want to add a different quantity for each component:

  • component 1593767685 should be added 20 times
  • component 1593767245 should be added 15 times etc

Working with the quantity attribute didn't get us far here because it would only add the whole composite product into the cart x-times but not each of the components:

https://www.example.com/cart/?add-to-cart=123&wccp_component_selection[1593767685]=479&quantity=20...

So we're out of ideas now. How can we add a composite product into the cart 1 time but control the quantity of each component as we want?


Solution

  • After dealing with the customer support of the plugin, the solution is simple as this:

    https://www.example.com/cart/?wccps_c0=289&wccps_c1=288&wccpq_c0=34&wccpq_c1=67&quantity=1&wccpm0=1593767245&wccpm1=1593766620&wccpl=2&add-to-cart=448

    is the right link. while wccps_cX is the product ID and wccpq_cX is the quantity. wccpmX is the component ID and wccpl=X marks the number of components you want to add.