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?
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.
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 enlace oculto
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.