Magento – Not able to get product collection in Custom Model

magento2modelproduct-collection

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.

Best Answer

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;
    }
}