Skip Navigation

[Closed] Need to modify quantity of the product that is being added via commerce form

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 2 replies, has 2 voices.

Last updated by Nigel 6 months, 3 weeks ago.

Author
Posts
#2696308

Tell us what you are trying to do?
Want to modify quantity of the product being added to cart while filling up the form

Is there any documentation that you are following?
https://toolset.com/documentation/programmer-reference/cred-commerce-api/

Is there a similar example that we can see?

What is the link to your site?

#2696354

Hi there,

i checked the 'cred-commerce' files. I understand there is addtocart function (referenced in 4 files) which adds '1' is default quantity.

Will it possible for you to modify api hook: 'cred_commerce_add_product_to_cart' so that we can modify quantity?

Further, there will be a field in cred form which will hold the quantity. It will be great if you can help me identify how to get that field in the above hook and use it update the quantity.

#2696524

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

There isn't a filter available within the Toolset API to modify the quantity added to the cart, it is hard-coded as 1.

Modifying that involves custom code, which is outside the scope of support, but let me see if I can give you some pointers about how to tackle this.

I checked our code base, and ultimately we are relying on WooCommerce's own API to add the product to the cart, using

global $woocommerce;
$woocommerce->cart->add_to_cart( $product_id, 1, '', '', array( 'cred_meta' => $extra_data ) );

And looking into WooCommerce's API, it looks like you can filter the quantity to override the value passed to the add_to_cart method.

See hidden link

So you could use our cred_commerce_add_product_to_cart hook to intervene, setting up a callback for the woocommerce_add_to_cart_quantity filter: the $product is available in your callback for that filter, and you need $product->ID to pass to the woocommerce_add_to_cart_quantity filter to identify the product being added to the cart to know which one to change the quantity for.

As for retrieving the quantity from a field that is submitted with the form, I suggest you check the $_POST object where you should find the field and its value available.

The topic ‘[Closed] Need to modify quantity of the product that is being added via commerce form’ is closed to new replies.