2

In my Carrier class for my custom shipping method, I have tried calling $request->getAllItems() as well as injecting an instance of Magento\Checkout\Model\Session and calling getAllItems as well. All that is returned is an array of empty objects. No matter what I do, they are empty. I am just trying to get the items in the cart to send to our shipping rate service to calculate the rates.

I referred to this as well to guide me: Magento 2 - How to get all items in cart?

1

It is very strange, because I can see in debug all items directly in the request, and they is not empty:

example from debug

Try to get items from the quote, like:

$items = $this->session->getQuote()->getAllItems();

It should work, since all items are constantly stored in the quote.

  • It seems that you have to call methods explicitly on the collection that is returned. Trying to var_dump() the getAllItems() result is always empty. After I iterated through the the items I was able to get methods. It seemed really odd though that I wasnt able to dump out the contents with var_dump. Anyone know why this is? – Stephen Hendricks Mar 13 '17 at 12:11
  • @StephenHendricks getAllitems returns all of the items in the cart gyazo.com/37d7761faef2e09ce4e669e4fc4311e9 not an empty array. Here the items has been added to the request: Magento\Quote\Model\Quote\Address::requestShippingRates in line 957: $request->setAllItems($item ? [$item] : $this->getAllItems()); and you can see how it works in the public function getAllItems() in the same class. – Siarhey Uchukhlebau Mar 13 '17 at 12:41
  • I saw how it worked, but just for some reason whenever i tried to log the contents of the result of this method, I went to json_encode() and log it, and it just serialized to an empty object is what I'm saying. – Stephen Hendricks Mar 13 '17 at 12:52
  • @StephenHendricks I'll try to reproduce your error, on which line you do this? – Siarhey Uchukhlebau Mar 13 '17 at 14:07
  • I've sense gotten around this, but I believe I was in the collectRates() method on in the shipping carrier class. I believe I had injected an instance of checkout/session and called getQuote() on that and then getAllItems on that. and I just tried to do json_encode($this->_logger->debug($this->_session->getQuote()->getAllItems())); or something along these lines. – Stephen Hendricks Mar 13 '17 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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