0

I created a preference for the block Magento\Catalog\Block\Product\ListProduct to add a specific product at the begining of the collection in getLoadedProductCollection(). Almost everything is well displayed except the price.

public function getLoadedProductCollection()
    {
        $originalCollection= parent::getLoadedProductCollection();
        $featuredProduct = $this->productFactory->create()->load(18); //test productId
        if ($this->_request->getFullActionName() !== 'catalogsearch_result_index' || !$featuredProduct) {
            return $originalCollection;
        }

        $newCollection = clone $originalCollection;
        $newCollection->removeAllItems();

        try {
            if($featuredProduct->getId()) {
                $newCollection->addItem($featuredProduct);
            }
            foreach ($originalCollection as $item) {
                if ($item->getId() != $featuredProduct->getId()) {
                    $newCollection->addItem($item);
                }
            }
        } catch (\Exception) {}
        return $newCollection;
    }

I'm not sure why in list.phtml, $block->getProductPrice($_product) return an empty string only for this product. The rest of the catalog is working normally.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.