1

I'm using Magento 2.4.6.

When I try to access $request->getAllItems() it returns an empty array.

Also, I'm trying with Quote and the same result.

public function collectRates(RateRequest $request) // NOSONAR
    {

        if (!$this->getConfigFlag('active')) {
            return false;
        }

        $this->_logger->info('COLLECT RATES 0 '. json_encode($request->toArray()));
        $this->_logger->info('COLLECT RATES 1 '. json_encode($this->_quote->getAllVisibleItems()));
        $this->_logger->info('COLLECT RATES 2 '. json_encode($this->_quote->getAllItems()));
        $this->_logger->info('COLLECT RATES 3 '. json_encode($this->_quote->getData()));
        $this->_logger->info('COLLECT RATES 4 '. json_encode($this->_quote->getCustomer()));

output:

[2023-11-16T13:20:20.882524+00:00] main.INFO: COLLECT RATES 0 {"all_items":[{},{}],....} [] []
[2023-11-16T13:20:20.883382+00:00] main.INFO: COLLECT RATES 1 [] [] []
[2023-11-16T13:20:20.883596+00:00] main.INFO: COLLECT RATES 2 [] [] []
[2023-11-16T13:20:20.883680+00:00] main.INFO: COLLECT RATES 3 [] [] []
[2023-11-16T13:20:20.884622+00:00] main.INFO: COLLECT RATES 4 {} [] []

What I'm missing?

thanks

4
  • is it a custom shipping method ?
    – Pawan
    Commented Nov 16, 2023 at 14:51
  • @Pawan yup. I do it by magento generator module (mage2gen.com)
    – carlos
    Commented Nov 16, 2023 at 15:37
  • How you are getting the quote item ? $this->_quote Commented Nov 16, 2023 at 16:17
  • @VyacheslavShmal importing use Magento\Quote\Model\Quote, then Injection in constructor and using in collectRates()
    – carlos
    Commented Nov 16, 2023 at 16:34

2 Answers 2

1

Problem is with json_encode. Use json_encode on item level.

    foreach ($request->getAllItems() as $item){
            $this->_logger->info('COLLECT RATES 2 '. json_encode($item->getData()));
        }
0

I think this quote class Magento\Quote\Model\Quote is not populated with the customer data, that is why you are not getting the products. Try $request->getAllItems() to get the products, otherwise try getting the quote from customer session.

    protected $checkoutSession;

    public function __construct(
        \Magento\Checkout\Model\Session $checkoutSession    
    ){
        $this->checkoutSession = $checkoutSession;
    }

    public function getQuotes()
    {
         return $this->checkoutSession->getQuote();       
    }
5
  • Still empty :/ $this->_logger->info('COLLECT RATES 1 '. json_encode($request->toArray())); $this->_logger->info('COLLECT RATES 2 '. json_encode($this->checkoutSession->getQuote())); $this->_logger->info('COLLECT RATES 3 '. json_encode($this->getQuotes()));
    – carlos
    Commented Nov 16, 2023 at 16:54
  • what about $request->getAllItems() ? Commented Nov 16, 2023 at 16:55
  • Empty too. With $request->toArray(), show me the "all_items" and is empty. If I try as $request->getAllItems() also are empty :/
    – carlos
    Commented Nov 16, 2023 at 17:22
  • It is weird because if I use $this->checkoutSession->getQuoteId() it return the correct ID.
    – carlos
    Commented Nov 16, 2023 at 17:35
  • Try loading the quote by id then, to see if it loads. \Magento\Quote\Api\CartRepositoryInterface $quoteRepository $this->quoteRepository->get($quoteId); Commented Nov 17, 2023 at 9:46

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.