Jump to content

Recommended Posts

Posted

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

  • Like 1
Posted

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.

Posted
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:

 

Posted

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

Posted

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

Posted
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);
}

 

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...