0

I have a custom module where I want to get all items from the cart and take a custom attribute(days_to_dispatch), but I don't know why I don't see that attribute and others(description, short_description, etc). In the block Products.php:

protected $checkoutSession;
public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Catalog\Helper\Data $helper,
        \Magento\Catalog\Model\ProductFactory $productFactory,
        \Magento\Framework\App\ResourceConnection $resourceConnection,
        \Magento\Checkout\Model\Session $checkoutSession,
        array $data = []
    )
    {
        $this->_helper = $helper;
        $this->_productFactory = $productFactory;
        $this->_resourceConnection = $resourceConnection;
        $this->_checkoutSession = $checkoutSession;
        parent::__construct($context, $data);
    }

public function getCartProducts(){
        $this->_checkoutSession->getQuote();
        if (!$this->hasData('quote')) {
            $this->setData('quote', $this->_checkoutSession->getQuote());
        }
        return $this->_getData('quote');
    }

And in my phtml file, I put

<?php
$quote = $block->getCartProducts();
foreach($quote->getAllVisibleItems() as $_item) {
    echo "<pre>"; var_dump($_item->debug());
}
?>

but I don't see that attribute.

4 Answers 4

1

To get Custom Attribute at cart item level, you need to define attribute in Vendor/Module/etc/catalog_attributes.xml.

refer below Example.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/catalog_attributes.xsd">
    <group name="quote_item">
        <attribute name="days_to_dispatch" />
    </group>
</config>

You can then write below code to get value.

$product = $item->getProduct()->getDaysToDispatch(); // where item is cart item

1

Please try Below Code Create a catalog_attributes.xml in Vendor/Module/etc

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/catalog_attributes.xsd">
 <group name="quote_item">
     <attribute name="days_to_dispatch"/>
 </group>
</config>

Refer Below Code.

<?php
$quote = $block->getCartProducts();
foreach($quote->getAllVisibleItems() as $_item) {
    $product = $_item->getProduct();
    echo $product->getDaysToDispatch();
    echo "<pre>"; var_dump($_item->debug());
}
?>
0

You should use class \Magento\Checkout\Model\Cart to get data in Cart

// Block 

protected $_cart;
    
    public function __construct(\Magento\Checkout\Model\Cart $cart)
    {
        $this->_cart = $cart;
    }
    
    public function getItemCollectionInCart(){
        return $this->_cart->getQuote()->getAllVisibleItems();
    }


// Template file

$cart = $block->getItemCollectionInCart();

foreach($cart as $item) {
     echo 'ID: '.$item->getProductId().'<br />';
      echo 'Name: '.$item->getName().'<br />';
       echo 'Sku: '.$item->getSku().'<br />';
       echo 'Quantity: '.$item->getQty().'<br />';
      echo 'Price: '.$item->getPrice().'<br />';            
  }
0

The answers with xml works well. In meantime I tried another solution that also works, but is not the best one. In the file Products.php I put:

public function getCartProducts(){
        $this->_checkoutSession->getQuote();
        if (!$this->hasData('quote')) {
            $this->setData('quote', $this->_checkoutSession->getQuote());
        }
        return $this->_getData('quote');
    }
public function getProductById($id)
    {
        return $this->_productFactory->create()->load($id);
    }

And in the phtml file I put:

$quote = $block->getCartProducts();
$itemsId = [];
$daysToDispatch = [];
foreach($quote->getAllVisibleItems() as $_item) {
    $itemsId[] = $_item->getProductId();
    var_dump($_item->debug());
}
foreach($itemsId as $id){
    $prod = $block->getProductById($id);
    echo "<pre>"; var_dump($prod->debug());
}

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.