1

I am working on a custom module for our payment system using Drupal 8 and Commerce 2. When a user removes and item for the cart or updates the quantity, I need to sync the two systems (our internal and Drupal commerce's cart). Our site has one store and all products are associated with that store.

I need to be able to load (and loop) over all the items still in the cart after the CartOrderItemRemoveEvent and the CartOrderItemUpdateEvent events. I tried using the following code, but the order is an empty array, despite the cart still having other items in it.

$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id()); //Which is 0 for anonymous
$order = \Drupal::entityTypeManager()->getStorage('commerce_order')->loadByProperties(['uid' => $user->get('uid')->value, 'cart' => '0']);

Is there a way to get the anonymous user's cart and a list of items still in that cart?

//updated

 public function cartItemRemoved(CartOrderItemRemoveEvent $event) {
      $cart_provider = \Drupal::service('commerce_cart.cart_provider');
      $cart = $cart_provider->getCart('default');
      $order_items = $cart->order_items->referencedEntities();
    \Drupal::logger('current cart items')->info('<pre><code>' . print_r(json_encode($order_items), TRUE) . '</code></pre>');

}

1 Answer 1

1

Get the current anonymous user's cart

  $cart_provider = \Drupal::service('commerce_cart.cart_provider');
  $cart = $cart_provider->getCart('default');

Get a list of items still in that user's cart

$order_items = $cart->order_items->referencedEntities();

Bonus

$cart_manager = \Drupal::service('commerce_cart.cart_manager');

Update order item

$cart_manager->updateOrderItem($cart, $order_item);

Remove order item

$cart_manager->removeOrderItem($cart, $order_item);

Empty cart

$cart_manager->emptyCart($cart);

Create order item

$order_item = $cart_manager->createOrderItem($entity, $quantity);

Add order item

$cart_manager->addOrderItem($order_item);
9
  • When I try to log the value of the $cart or the $order_items, it comes up empty. I updated my post to show the code I am using Commented Jul 8, 2020 at 16:10
  • $cart is an object, $order_items is an array containing order_item objects. If you want to inspect these, I suggest you use Devel's Kint module and do kint($cart); for example. You'll need to give anonymous user permission to use Kint
    – No Sssweat
    Commented Jul 8, 2020 at 16:40
  • Although, if your cartItemRemoved is triggered with ajax, you wont see the kint dump. You could tempotsrily move the code inside thst funtion into your theme's preprocess_page function just to see that it's working.
    – No Sssweat
    Commented Jul 8, 2020 at 16:50
  • Thank you! I tried to dump the $order_items also and it came up as an empty array. The remove item is not in ajax. Can you tell me the expected keys for each array order items so I can loop over them and see? I'll add kint also but I am using Drupal's logger and also writing the results to a file. Commented Jul 8, 2020 at 17:42
  • 1
    Your can access the order_item object protected values like this, for example, if you want the title, loop through the $order_items array, foreach ($order_items as $order_item) { kint($order_item->title->value); } If you want the quantity use $order_item->quantity->value, if you want the unit price use $order_item->unit_price->value etc...
    – No Sssweat
    Commented Jul 8, 2020 at 23:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.