1

I have to display a list of product on the product view, depending on its sku.

In order to do that, i have simply pasted the Random.php list example of Magento_Catalog.

But in order to find the proper product, i need to find the attribut_set_id of a specific attribute_set_name

So i injected an object:

<?php

namespace Extranet\Catalog\Block\Product\ProductList;

use Magento\Catalog\Api\CategoryRepositoryInterface;

class Echantillon extends \Magento\Catalog\Block\Product\ListProduct
{

    protected $_productCollectionFactory;

    protected $categoryRepository;

    protected $_attributeSetOptions;


    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
        \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
        \Magento\Catalog\Model\Layer\Resolver $layerResolver,
        CategoryRepositoryInterface $categoryRepository,
        \Magento\Framework\Url\Helper\Data $urlHelper,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        \Magento\Catalog\Model\Product\AttributeSet\Options $attributeSetOptions,
        array $data = []
    )
    {
        $this->_attributeSetOptions = $attributeSetOptions;
        $this->_productCollectionFactory = $productCollectionFactory;
        parent::__construct(
            $context,
            $postDataHelper,
            $layerResolver,
            $categoryRepository,
            $urlHelper,
            $data
        );
    }

    protected function _getProductCollection()
    {
        if ($this->_productCollection === null) {
            $product = $this->_coreRegistry->registry('product');

            $sku = $product->getSku();

            /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
            $collection = $this->_productCollectionFactory->create();
            $this->_catalogLayer->prepareProductCollection($collection);
            $collection->addAttributeToFilter('entity_id', 100);
            $collection->addStoreFilter();
            $collection->setPage(1, 1);
            $this->_productCollection = $collection;
        }
        return $this->_productCollection;
    }

}

But it tells me :

Fatal error: Uncaught TypeError: Argument 7 passed to Extranet\Catalog\Block\Product\ProductList\Echantillon::__construct() must be an instance of Magento\Catalog\Model\Product\AttributeSet\Options, array given,

I really don't understand that. I tried several different object, I even tried through the di.xml to inject an object in the $data , and nop...

Edit: Note if i remove the $attributeSetOptions injection, my block works.

Final edit :

As everyone responded, it has something to do with var/

I tried my project on another server, and everything was ok. Weirdly, it came from my virtual machine: some classes were properly generated, other were not... Just restarted it this morning, and everything works... So sorry, not trully a Magento 2 PB, but i will know now that this kind of pb probably come from /var

2
  • remove var/generation/* – Sohel Rana Nov 30 '16 at 17:46
  • I did it about 1000 times :) – Pol Ravalitera Nov 30 '16 at 17:46
0

Clear the var/generation and var/di folders

rm -rf var/generation var/di

enable developer mode (so the di configuration is not compiled)

bin/magento deploy:mode:set developer

search for Magento\Catalog\Model\Product\AttributeSet\Options in your xml files. If there is not any preference configuration magento should inject the correct class

8
  • Developer mode already enable, var/ already well cleaned. I will try on a clean Magento 2 install... – Pol Ravalitera Nov 30 '16 at 18:07
  • do you have any di configuration for your own class Extranet\Catalog\Block\Product\ProductList? – David Verholen Nov 30 '16 at 18:12
  • or another thing I've sometimes overlooked, if this class is extended by another class which calls the parent::__construct() with the wrong arguments – David Verholen Nov 30 '16 at 18:15
  • No. After 1 hour of not understanding, I tried to inject via di.xml the $data argument, and the $data is always empty. I have a lot of modules in my project, maybe something else is doing that... – Pol Ravalitera Nov 30 '16 at 18:15
  • It's trully a copy paste of the "Random" Block list of Magento_Catalog. I did nothing more for the moment. – Pol Ravalitera Nov 30 '16 at 18:16
0

Hmm, indeed for this kind of error, I usually remove all folder in var/ For your task, maybe you could use catalog widget in BO. It allows to display a list of product with severals conditions.

Otherwise make a custom select field in system config to select your attributeSet and check in your block if your product attributeSetId match with your custom field in system config.

0

As everyone responded, it has something to do with var/

I tried my project on another server, and everything was ok. Weirdly, it came from my virtual machine: some classes were properly generated, other were not... Just restarted it this morning, and everything works...

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.