Jump to content

Not managing to remove product from cart


joe_g
 Share

Recommended Posts

I'm trying to make a minimal setup without ajax, so far I've got:

            <form method="post" class="padloper-cart-add-product flex ml-auto" action="<?= $config->urls->root ?>padloper/add/">
                <?=$session->CSRF->renderInput()?>
                <input type='hidden' name='product_id' value='<?=$product->id?>'/>
                <input type="submit" value="Add">
            </form>
            <form method="post" class="padloper-cart-remove-product flex ml-auto" action="<?= $config->urls->root ?>padloper/remove/">
                <?=$session->CSRF->renderInput()?>
                <input type='hidden' name='product_id' value='<?=$product->id?>'/>
                <input type="submit" value="Remove"> <?=$product->id?>
            </form>

Add works, remove not - how come?

Remove returns a redirect to https://domain.com/padloper/remove/?removedProduct=XXX but the cart isn't updated

(Then, after that, lots of redirects)

tx, J

Link to comment
Share on other sites

Hey @joe_g,

Removing a product requires the ID of the item in the cart. Here's an example:

<?php

namespace ProcessWire;
// ==================
// Just an example; you might be getting your product using a different approach
$product = $padloper->get("template=product,id=1190");
bd($product, __METHOD__ . ': $product at line #' . __LINE__);
/** @var array $cartItems */
$cartItems = $padloper->getCart();
// ALL CART ITEMS
bd($cartItems, __METHOD__ . ': $cartItems at line #' . __LINE__);
// FILTER CART ITEMS TO get the one product you want
// PHP 8.X
/** @var array $productInCart */
$productInCart = array_filter($cartItems, fn ($item) => $item->product_id === $product->id);
bd($productInCart, __METHOD__ . ': $productInCart - at line #' . __LINE__);
if (!empty($productInCart)) {
	// get first matched item FROM ARRAY
	/** @var stdClass $productInCart */
	$productInCart = reset($productInCart);
	bd($productInCart, __METHOD__ . ': $productInCart - RESET - at line #' . __LINE__);
} else {
	// HANDLE THIS PRODUCT NOT FOUND IN CART
}


?>

<form method="post" class="padloper-cart-add-product flex ml-auto" action="<?= $config->urls->root ?>padloper/add/">
	<?= $session->CSRF->renderInput() ?>
	<input type='hidden' name='product_id' value='<?= $product->id ?>' />
	<input type="submit" value="Add">
</form>
<!-- @NOTE: we need the ID of the item in the cart -->
<form method="post" class="padloper-cart-remove-product flex ml-auto" action="<?= $config->urls->root ?>padloper/remove/">
	<?= $session->CSRF->renderInput() ?>
	<input type='hidden' name='product_id' value='<?= $productInCart->id ?>' />
	<input type="submit" value="Remove"> <?= $productInCart->id ?>
</form>

Please let me know if you need further assistance with this.

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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