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.