0

i am creating a custom button to add products to cart using add_to_cart($product_id, $quantity) function. It's working well for the single property but when I change product_id with another product id then it shows error.

$cart = $woocommerce->cart->add_to_cart($product_id, $quantity);

when I try same with different product_ids it stops working. any solution?

4
  • what is the error? Commented Sep 14, 2017 at 9:31
  • @AkshayShah it return false when I change product id Commented Sep 14, 2017 at 9:54
  • What is the value of new $product_id that you change to? Does it really exist? Commented Sep 14, 2017 at 9:56
  • @NerijusMasikonis first time i used it with $product_id = 85 i work properly. Now when i change $product_id = 93 it return false. but for $product_id = 85 still working well and increase its quantity in cart. and yes both product_ids exist . Commented Sep 14, 2017 at 9:59

1 Answer 1

0

Okay, thanks for the help. Finally, the problem is solved. Actually, 93 product_id product is out of stock that's why it returns false. add_to_cart() function does not work with a product out of stock.

Sign up to request clarification or add additional context in comments.

2 Comments

If you enable stock management at product level (option in inventory tab) and you "allow backorders" (option), add_to_cart() function will work…
@LoicTheAztec Yeah I checked all the options. Now it working well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.