2

I need some direction...

Within Magento 2, I am not finding exactly how to do a getCollection() that will allow me to see a list of all products. I am developing an Rest API to allow me to get a list of products based on a defined set of attributes. The code I have is the following:

class Inventory implements InventoryInterface {
    private $scopeConfig;
    private $product;

    public function __construct(
        Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        Magento\Catalog\Model\ProductFactory $product
    ) {
        $this->scopeConfig = $scopeConfig;
        $this->product = $product;
    }

    public function get($id = null) {
        $data = $this->product->create()->getCollection();
        $data->addFieldToFilter('status', 1);
        $data->addFieldToFilter('type_id', 'simple');

        if ($id) {
            $data->addFieldToFilter('entity_id', $id);
        }

        return $data->getData();
    }
}

with this, I am getting back the XML, but I am missing how to be able to select additional attributes to display.

Please be gentle in your responses... I am new to this list AND Magento 2, so feeling a little stupid at this point. Thanks in advance.

4

I was finally able to get this to work... The code is as follows:

class Inventory implements InventoryInterface {
    private $scopeConfig;
    private $product;

    public function __construct(
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $product
    ) {
        $this->product = $product;
    }

    public function get($id = null) {
        $data = $this->product->create();
        $data->addFieldToFilter('status', 1);
        $data->addFieldToFilter('type_id', 'simple');

        if ($id) {
            $data->addFieldToFilter('entity_id', $id);
        }
        $data->addAttributeToSelect('price');
        return $data->load()->toArray();
    }
}

Hope this helps someone. Thanks again for the assist Pradeep.

2

try this

class Inventory  extends \Magento\Framework\View\Element\Template {
    private $scopeConfig;
    private $product;

    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $product
    ) {
        $this->scopeConfig = $scopeConfig;
        $this->product = $product;
    }

    public function get($id = null) {
        $data = $this->product->create();
        $data->addFieldToFilter('status', 1);
        $data->addFieldToFilter('type_id', 'simple');
        $id=1;
        if ($id) {
            $data->addFieldToFilter('entity_id', $id);
        }

        return $data;
    }

hope it will help you:-)

  • Thanks for the reply.... I am getting the following error when I use that. report.CRITICAL: exception 'Exception' with message 'Report ID: webapi-568de5e300ce5; Message: Recoverable Error: Argument 2 passed to XXXXXXXX\Product\Model\Inventory::__construct() must be an instance of Magento\Catalog\Model\ResourceModel\Product\CollectionFactory, instance of Magento\Catalog\Model\ProductFactory given Thoughts? Thanks again for the assist! – Stephen Malenshek Jan 7 '16 at 4:18
  • extends \Magento\Framework\View\Element\Template, anwsers is edited please recheck the anweer code if it work accept the answer so it helps others – Pradeep Kumar Jan 7 '16 at 4:19
  • No joy... Thanks for the attempt though – Stephen Malenshek Jan 7 '16 at 4:23
  • answer was edited recheck or reuse the code now – Pradeep Kumar Jan 7 '16 at 4:23
  • Still the same error... Thanks for the attempt to assist. Much appreciated. – Stephen Malenshek Jan 7 '16 at 4:57
1

Regarding this error...

CRITICAL: exception 'Exception' with message 'Report ID: webapi-568de5e300ce5; Message: Recoverable Error: Argument 2 passed to XXXXXXXX\Product\Model\Inventory::__construct() must be an instance of Magento\Catalog\Model\ResourceModel\Product\CollectionFactory, instance of Magento\Catalog\Model\ProductFactor

What's happening here is that the called block within var/generation has the older version of your current block. Magento cache system is crazy here. Easy fix tho. Go to ROOT/VAR/GENERATION/ Delete all folders there. It would be regenerated (Refresh) once you load the page.

For a cleaner fix. Go to SHELL, on your root directory type

$ php bin/magento setup:upgrade

This will refresh all cache and regenerate al magento modules along custom module. Same as deleting VAR/GENERATION folders.

Hope it helps.

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.