I am working on a custom script (not custom module) where I need to fetch product collection and display 5 products at a time. After displaying 5 products there is a link Next which displays next 5 records and so on.
How can i do this. Please.
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityDONE,
//your code goes here...
$page_number = $_GET['p'];
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$productCollection = $productCollectionFactory->create()
->addAttributeToSelect('*')
->setPageSize(5)
->setCurPage($page_number)
->load();
foreach ($productCollection as $p){
echo $p->getName().'<br>';
}
$page_number++;
echo "<a href='?p=$page_number'>Next</a>";
protected $_productCollectionFactory;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
array $data = []
)
{
$this->_productCollectionFactory = $productCollectionFactory;
parent::__construct($context, $data);
}
public function getProductCollection() { $collection = $this->_productCollectionFactory->create(); $collection->addAttributeToSelect('*'); ->setPageSize(5) ->setCurPage($page_number) // you need to get page number ->load();
return $collection;
}