cocart-headless / cocart-core

This is the core of CoCart that provides support for managing the user session and decouples WooCommerce.
Other
3 stars 1 forks source link

Warning: Undefined array key "cart_cached" #9

Open Mohamad-Jaallouk opened 1 year ago

Mohamad-Jaallouk commented 1 year ago

Prerequisites

Describe the bug

When using "restore item from cart" using Woocommerce Checkout I get:

Warning: Undefined array key "cart_cached" in C:\Users\user\Local Sites\test2\app\public\wp-content\plugins\cart-rest-api-for-woocommerce\includes\class-cocart-session.php on line 219

The product however is removed.

Expected behavior

...

Actual behavior

...

Steps to reproduce

.

WordPress Environment

WP: 6.3.2 PHP: 8.1.23

Isolating the problem

seb86 commented 1 year ago

Can you provide a video recording @Mohamad-Jaallouk ?

I ask because your report is a little short.

What are the steps to reproduce?

Mohamad-Jaallouk commented 1 year ago

In review-order.php I use Alpine JS to delete an item:

<button x-data="{}" @click="deleteItem($event)" data-key="<?php echo $cart_item_key; ?>" type="button">DELETE</button>
<script type="text/javascript">
function getParameterByName(name, url) {
  if (!url) url = window.location.href;
  name = name.replace(/[\[\]]/g, "\\$&");
  var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
    results = regex.exec(url);
  if (!results) return null;
  if (!results[2]) return '';
  return decodeURIComponent(results[2].replace(/\+/g, " "));
}

// Get the cart_key from the URL
var cartKey = getParameterByName('cocart-load-cart');

function deleteItem(event) {
  const itemKey = event.target.getAttribute('data-key');

  fetch("https://test2.local/wp-json/cocart/v2/cart/item/" + itemKey +
      "?cart_key=" + cartKey, {
        method: "DELETE",
        headers: {
          "Content-Type": "application/json; charset=utf-8"
        }
      })
    .then(response => response.json())
    .then(data => {
      window.location.reload();
    })
    .catch(error => {
      console.error('Error:', error);
    });
}
</script>

The item has been successfully removed, but this warning appears. If I delete all items and start to add new ones, the warning disappears. Once I click the delete button again, it appears once more. I would be happy to share a video if needed after sharing the code.

seb86 commented 12 months ago

Committed: https://github.com/co-cart/cocart-core/commit/f8f44a45678a3c86280cd25a9897a72bbf1427ea