1

I don't have any prior experience in this. I want to fetch the products from Database. I am trying from past few hours.But not working.

I am using following code.

protected $_productCollectionFactory;

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

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

I am getting following response.

Anything wrong with this code?

Even I also want to fetch the customer collection as well.

Also I want to know the difference between ProductCollectionFactory & product.

Also I tried below code.

 namespace Testmod\FetchDataApi\Api;


 interface CustomerRepositoryInterface
{

   /**
   * @param int|string $number
  * @return  \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
  */
    public function getCustomerParchases();
}

And my class:

namespace Testmod\FetchDataApi\Model;
use Magento\Framework\View\Element\Template;

 class CustomerRepository extends Template
 {

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    array $data = []
) {

    $this->_productCollectionFactory = $productCollectionFactory;
    parent::__construct($context, $data);
}



  public function getCustomerParchases()
  {
    $collection = $this->_productCollectionFactory->create();
      $collection->addAttributeToSelect('*');
    return $collection;

 }
}

Above code will return []. But collection->count(); returns product count wwhich is 5.

1 Answer 1

-1

I think the good way to get product collection is following :

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

public function getCollection()
{
    $collection = $this->product->create()->getCollection();
    return $collection;
}

And for customer collection you can try following code :

 public function __construct(
        ...
        \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerFactory
        ...
    ) {
        ...
        $this->_customerFactory = $customerFactory;
        ...
    }

    public function getCustomerCollection()
    {   
        //Get customer collection
        return $this->_customerFactory->create();
    }
2
  • Thank you for your reply. I got this error in postman : { "message": "Invalid method %1::%2", "parameters": [ "Magento\\Catalog\\Model\\Product\\Interceptor", "create" ] } Commented Apr 19, 2018 at 7:07
  • I have edited the post... Commented Apr 19, 2018 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.