wagnerwagner / merx

Merx is a plugin to create online shops with Kirby.
https://merx.wagnerwagner.de
102 stars 10 forks source link

Cart()->remove() not removing product - Issue with UUID's #66

Closed Heave-be closed 1 year ago

Heave-be commented 1 year ago

Hi,

Love the plugin and the flexibility. I've implemented it into a project, but seem to be having a very strange issue.

The following piece of code should normally work based on the docs:

$id = get('id');

try {

  merx()->cart()->remove($id);

} catch (Exception $ex) {

    $outputMessage    = $ex->getMessage();

}

For some reason this doesn't actually remove the product, but it doesn't trigger the catch block either. ID variable is correct for the product i'm trying to remove.

If I simply display the cart on a page like this:

<?= merx()->cart(); ?>

I get a number of UUID's from products (which are all correct). If I then use the following piece of code to manually remove one of the products:

<?php merx()->cart()->remove('page://uvpcafez4cjwkoiz '); ?>

The product doesn't get removed from the cart either.

Is there something I'm missing here?

—————

EDIT:

After a bit of research and scratching my head, this is what I found out.

When a product is added as follows:

<input type="hidden" name="id" value="<?= $page->id() ?>">
<button>add</button>

Everything works as intended. Product can be removed from the cart by using the id. However, when adding a product to the cart with a UUID, it causes the bug as explained above:

<input type="hidden" name="id" value="<?= $page->uuid() ?>">
<button>add</button>

Is this intended behaviour or a bug?

tobiasfabian commented 1 year ago

Hi @Heave-be,

thanks for your detailed comment.

What you described is somehow the intended behavior. For $cart->add(), you have to pass the page’s id (not uuid). Internally the id is renamed to key – for several reasons. You can remove the cart item with this specific key using the $cart->remove() method.

I tried to use the Page’s uuid instead of id. Theoretically this should work. Practically the key is transformed to lowercase. Page uuids are case sensitive. So they are stored with a lowercase key. When $cart->remove() is called, the method wants to remove the case sensitive uuid. E.g. item is stored as page://yewqbkkpvrwbqfbw, $cart->remove('page://yEWQBkKPvrwBQfBw') does not work.

Long story short, I modified ProductList::__set() (which is called when using $cart->add()) and and removed the conversion to lowercase (e56f9b1ec7c0574634f6ff977dbf135c13f7a106). You can download the develop branch and see if it works for you.

Heave-be commented 1 year ago

Hi @tobiasfabian,

Cheers for the response!

I tested your develop branch with the Merx plainkit (changed to add products to cart by UUID) seems to be working fine now with the removal of the conversion to lowercase.

Thanks for the "fix"!