0

I'm trying to pull a product collection into a CMS page based on an attribute. I have the collection pulled in and I can return the SKU and URL of the product in my loop, but the product name returns null. Am i loading this collection in correctly? Is there a better way to go about doing this.

Here is my block:

namespace {{vendor}}\ManufacturerPages\Block;

class Manufacturer extends \Magento\Framework\View\Element\Template {
    protected $categoryFactory;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
        array $data = []
    ){
        $this->_productCollectionFactory = $productCollectionFactory;
        $this->_catalogProductVisibility = $catalogProductVisibility;
        parent::__construct($context, $data);
    }

    public function getProductCollection() {       
        $attrId = $this->getAttrId();
        $collection = $this->_productCollectionFactory->create();
        $collection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds());
        $collection->addAttributeToFilter('manufacturer', ['eq' => $attrId]);

        return $collection;
    }
}

This is the code in the template:

$productCollection = $this->getProductCollection();

foreach ($productCollection as $product) {
    var_dump(($product->getData('name')));
    var_dump(($product->getId()));
    var_dump(($product->getProductUrl()));
    var_dump(($product->getSku()));
}

And i'm calling in template and block in the cms page with this line:

{{block class="{{vendor}}\ManufacturerPages\Block\Manufacturer"  attr_id="2337" template="{{vendor}}_ManufacturerPages::manufacturers/pages.phtml"}}

In case anyone is already thinking this, the cms page widget that will pull in a product list is no longer working on the site i'm building. I have found a github post about the issue being corrected in v2.1, but of reasons out of my control I wont be able to update the site before it goes live. So I'm trying to find a work around.

1

You need to add the name field to your select:

$collection->addFieldToSelect('name');
  • I tried that following loosely the code here, but with no effect. I did place the code in the template and not the block. Where would you inject that? and after inserting that, would you restrict the collection to just the name attribute? magento.stackexchange.com/questions/120366/… – circlesix Aug 12 '16 at 21:17
  • You'd put it in the getProductCollection method after having instantiated the product collection. You shouldn't need to restrict to just the name. Be sure that you're using addFieldToSelect instead of addAttributeToSelect because the name is considered a field instead of an attribute. – Aaron Allen Aug 12 '16 at 21:24
  • Worked like a charm! – circlesix Aug 12 '16 at 21:59

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.