joe_g Posted December 21, 2022 Share Posted December 21, 2022 Hi there, I think what would help is a very minimal setup, a demo without ajax/htmx, templating etc. In the absence of that, here is a super basic question: 1. How do I add and remove a product to the cart purely via the API? This is for me to understand the basics. I'm imagining it would be something like $cart = $padloper->getCart(); if(!$cart->addProduct(product_id)) { echo 'problems adding'; } if(!$cart->removeProduct(product_id)) { echo 'problems removing'; } tx J 1 Link to comment Share on other sites More sharing options...
kongondo Posted December 21, 2022 Share Posted December 21, 2022 Hi J, 2 hours ago, joe_g said: I think what would help is a very minimal setup, a demo without ajax/htmx, templating etc. This is a very good suggestion. Please check your PM. Thanks. Link to comment Share on other sites More sharing options...
kongondo Posted December 21, 2022 Share Posted December 21, 2022 2 hours ago, joe_g said: How do I add and remove a product to the cart purely via the API? Supplementary to our conversation via PM, please also see this topic: Link to comment Share on other sites More sharing options...
joe_g Posted December 21, 2022 Author Share Posted December 21, 2022 Thanks, I'm mostly looking for a way to remove a product from the cart, I can't seem to find thatin the link you attached JJ Link to comment Share on other sites More sharing options...
joe_g Posted December 21, 2022 Author Share Posted December 21, 2022 After testing I can say that echo $padloper->cart->addProduct(1043); works fine, but however echo $padloper->cart->removeProduct(1043); does not work Why would that be?J Link to comment Share on other sites More sharing options...
kongondo Posted December 21, 2022 Share Posted December 21, 2022 1 minute ago, joe_g said: Thanks, I'm mostly looking for a way to remove a product from the cart, I can't seem to find thatin the link you attached JJ Yes, that link is only about adding. 27 minutes ago, joe_g said: $padloper->cart->removeProduct(1043); The argument you pass here should be the ID of the cart item, not the product ID, i.e., <?php namespace ProcessWire; // ================== /** @var array $cartItems */ $cartItems = $padloper->getCart(); bd($cartItems, __METHOD__ . ': $cartItems at line #' . __LINE__); foreach ($cartItems as $cartID => $cartItem) { /** @var stdClass $cartItem */ echo "The ID of the cart item is: {$cartID}<br>"; // OR // echo "The ID of the cart item is: {$cart->id}<br>"; // REMOVE CART ITEM $padloper->cart->removeProduct($cart->id); // OR // $padloper->cart->removeProduct($cartID); } Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now