There is an attribute in the product AllowSameDayDelivery how can I get it out of the product?

I got the products ids from the cart, But how do I get the attribute now?


namespace Ronis\SameDayDelivery\Observer;

use Magento\Framework\Event\ObserverInterface;
use \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;

class DayDelivery implements ObserverInterface
    protected $resultPageFactory;

     * @var \Magento\Checkout\Model\Cart
    protected $_cart;
    protected $_productCollectionFactory;

     * [__construct description]
     * @param \Magento\Framework\App\Action\Context $context [description]
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory [description]
     * @param \Magento\Checkout\Model\Cart $cart [description]
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Checkout\Model\Cart $cart,
        CollectionFactory $productCollectionFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->_cart = $cart;
        $this->_productCollectionFactory = $productCollectionFactory;

    public function execute(\Magento\Framework\Event\Observer $observer)
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/delivery.log');
        $logger = new \Zend\Log\Logger();

        $cartProductIds = $this->_cart->getQuoteProductIds();
        $productCollection = $this->_productCollectionFactory->create();
        $productCollection->addFieldToFilter('entity_id', ['in' => [$cartProductIds]]);
        foreach ($productCollection as $item){
            var_dump ($item->getAllowSameDayDelivery());die;

now it always returns null even though it is in this product.


In the product collection you will get only product attributes with the flag Used in Product Listing set to 'yes'.

Therefore you have two solutions:

  1. Set the flag Used in Product Listing set to 'yes' for your attribute allow_same_day_delivery (I guess that's your attribute_code)

or alternative

  1. load the product by Id for each product in the foreach loop using the ProductFactory, then you have all attributes available
  • this is how I have yes values ​​in the product. I downloaded the product by "_productCollectionFactory". How do I get my attribute now? Apr 8 '19 at 15:05
  • That depends on the type of your attribute: For text/varchar attributes $item->getData('allow_same_day_delivery'); or $item->getAllowSameDayDelivery() should work. For select attributes you should be able to get the attribute text like this $item->getAttributeText('allow_same_day_delivery'); But anyway if $item->getData('allow_same_day_delivery'); is empty the attribute seems not to be in the collection
    – HelgeB
    Apr 8 '19 at 15:10
  • Not one of the options came up. All give out null, except for the last one, but they also give out not what is needed. Apr 8 '19 at 15:15
  • So $item->getData('allow_same_day_delivery'); will get you some not empty / null output, but $item->getAttributeText('allow_same_day_delivery'); not? What kind of attribute is allow_same_day_delivery?
    – HelgeB
    Apr 8 '19 at 15:20
  • How then to add it to the collection? Apr 8 '19 at 15:20

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.