0

I use magento 2.4. I have product collection in catalog view. There I have loop:

$_productCollection = $block->getLoadedProductCollection();
.
.
.
                <?php /** @var $_product \Magento\Catalog\Model\Product */ ?>
                <?php foreach ($_productCollection as $_product): $i++; ?>

Inside of that loop I want to get custom attribute like so:

<?php echo $_product->getData('available_soon');?>

Now I know that I can't get it because of collection so I write my plugin. di.xml

namespace [Vendor]\Front\Block\Product;


use Magento\Eav\Model\Entity\Collection\AbstractCollection;

/**
 * Product list
 * @api
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 * @since 100.0.2
 */
class ListProduct extends \Magento\Catalog\Block\Product\ListProduct
{
    /**
     * Retrieve loaded category collection
     *
     * @return AbstractCollection
     */
    public function getLoadedProductCollection()
    {
        $collection = $this->_getProductCollection()->addAttributeToSelect(['available_soon']);

        $categoryId = $this->getLayer()->getCurrentCategory()->getId();
        foreach ($collection as $product) {
            $product->setData('category_id', $categoryId);
        }

        return $collection;
    }
}

However, still I don`t have that custom attribute here. Can some one explain me what I doing ring here ?

2
  • If any one search for it. I found solution like so: $collection = clone $this->getLoadedProductCollection(); return $collection->clear()->addAttributeToSelect('available_soon')->load(); Commented Jul 8, 2021 at 11:21
  • check this magento.stackexchange.com/questions/313773/…
    – Devidas
    Commented Jul 8, 2021 at 12:23

1 Answer 1

0

Try like this:

<?php 
    $available_soon = $_product->getResource()->getAttribute('available_soon')->getFrontend()->getValue($_product);
    echo $available_soon;
?>

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.