1

I am using WC()->cart->add_to_cart($new_post_id); to add a product ,but this function does not adding for the first time when cart is empty.

If I add product using default add to cart button, Then after the above function works properly so can you please point out the issue .

1 Answer 1

0

Quantity is missing of the product try to use

WC()->cart->add_to_cart( $new_post_id , $qty );

where $qty is a number of items you want to add in the cart for a particular product. hope this will help you.
Thank you.

1
  • 1
    The default qty is 1 as in the method signature: add_to_cart( integer $product_id = 0, integer $quantity = 1, integer $variation_id = 0, array $variation = array(), array $cart_item_data = array() ) (docs.woocommerce.com/wc-apidocs/class-WC_Cart.html)
    – TungstenX
    Commented Dec 3, 2018 at 6:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.