Magento 2: How to get quote product id in an observer?

Magento 2: How to get quote product id in an observer?

August 28, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2: How to get quote product id in an observer?

Magento 2: Getting Quote Product Id in an Observer

In this post, I’ll guide you through resolving the issue of getting quote product ids in a Magento 2 observer. I’ll also address the “Warning: Invalid argument supplied for foreach()” error and provide a way to redirect based on attribute options.

Understanding the Current Issue

You’re facing two main problems:

  1. The product id is not displaying.
  2. You’re receiving the “Warning: Invalid argument supplied for foreach()” error when trying to iterate over quote items.

Let’s dive into solving these issues step-by-step.

Getting Quote Product Ids in an Observer

First, let me explain why your current approach isn’t working:

foreach($event->getQuoteItem()  as $_item){
   $product = $objectManager->create('Magento\Catalog\Model\Product')->load($_item->getProductId());
}

The error occurs because $event->getQuoteItem() doesn’t return an array of items. Instead, it returns a single quote item if the event is related to a specific quote.

To fix this, you need to retrieve all visible items from the quote:

$quote = $observer->getEvent()->getQuote();
$items = $quote->getAllVisibleItems();

foreach($items as $_item) {
    $product = $objectManager->create('Magento\Catalog\Model\Product')->load($_item->getProductId());
}

Now you should be able to access the product ids without any errors.

Redirecting Based on Attribute Options

You’ve updated your code but are still stuck with the redirection logic. The issue lies in how you’re checking the attribute value:

$is_priscription = $product->getData('prescription');

This will not work because getData() retrieves the raw database value, which might be different from what you expect.

Instead, use the appropriate method to get the attribute and its options. For a custom attribute named “prescription” with an option id of 5431:

$prescriptionAttribute = $product->getResource()->getAttribute('prescription');
$is_priscription = $prescriptionAttribute->getFrontend()->getValue($_item);

Now, check if the option value is equal to 5431:

if ($is_priscription != '5431') {
    // Redirect logic here...
}

To redirect to either the success page or a custom URL based on the condition, you can use Magento’s ResultFactory:

$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);

if ($is_priscription != '5431') {
    $resultRedirect->setPath('order-success');
} else {
    $resultRedirect->setUrl($this->_url->getUrl('prescription/index'));
}

return $resultRedirect;

Conclusion

In this post, I demonstrated how to:

  • Retrieve quote product ids in an observer without errors.
  • Check for specific attribute options using the correct methods.
  • Redirect to either a success page or a custom URL based on your condition.

Remember that observers are powerful tools for executing logic after certain events occur. By understanding their structure and Magento’s object management system, you can create robust extensions that enhance your store’s functionality.

For more advanced topics in Magento 2 development, feel free to explore our Magento Development Services at Laravel Company. We’re committed to providing top-notch solutions for e-commerce businesses.

Happy coding!

Magento 2 Catalog Extensions Errors

Our network