0

I am preparing an API in which I need to get product collection in Magento 2.

I have prepared a simple API module and its working fine, now I want to get all product collection for the API I want to make. And I want this collection in my module Model class.

I have written this code in my model file:

namespace Namespace\Connector\Model;
use Namespace\Connector\Api\OdooInterface;

class Odoo implements OdooInterface
{
    protected $_productCollectionFactory;

    public function __construct(
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        array $data = []
    ) {
        $this->_productCollectionFactory = $productCollectionFactory;
        //parent::__construct($data);
    }

    public function getProductCollection()
    {
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        $collection->setPageSize(3); // fetching only 3 products
        return $collection;
    }
}

But it shows below error:

. Fatal Error: 'Cannot access parent:: when current class scope has no parent' in '/var/www/html/magento2/app/code/Namespace/Modulename/Model/Odoo.php' on line 15

If anyone knows how to get the product collection in Custom Module Model class. Please let me know.

  • Can you put whole class content? – Sohel Rana Dec 27 '16 at 6:54
  • @SohelRana I have updated my question. Please check it. – Gaurav Agrawal Dec 27 '16 at 7:00
1

Please try below code.

use Magento\Catalog\Model\Product;
class Odoo implements OdooInterface
{   
    protected $_productFactory;

    public function __construct(
        .............
        \Magento\Catalog\Model\ProductFactory $productFactory,
        ............
    ) {
        $this->_productFactory = $productFactory;
        ...............
    }

    public function getProductCollection()
    {
        $collection = $this->_productFactory->create()->getCollection();        
        $collection->addAttributeToSelect('*');
        $collection->setPageSize(3);
        return $collection;
    }
}
  • Its not working. I have tried this. – Gaurav Agrawal Dec 27 '16 at 7:27
  • Is it giving any error? – Nalin Savaliya Dec 27 '16 at 7:32
  • please add 'use' statement as I updated code – Nalin Savaliya Dec 27 '16 at 7:37
0

Try following way:

namespace Namespace\Connector\Model;
use Namespace\Connector\Api\OdooInterface;

class Odoo implements OdooInterface
{
    protected $productCollectionFactory;

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

    public function getProductCollection()
    {
        $collection = $this->productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        $collection->setPageSize(3); // fetching only 3 products
        return $collection;
    }
}

Delete 'var/generation/*' and clear cache.

  • Its not working. I have tried this. – Gaurav Agrawal Dec 27 '16 at 7:27
  • It gives response : Class string does not exist – Gaurav Agrawal Dec 27 '16 at 7:29
  • 1
    show your config file. di.xml too. – Sohel Rana Dec 27 '16 at 7:37

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.