1

I have 2 questions.

I've found this solution on stackOverflow:

class Page extends \RonisBT\Cms\Block\Page
{
    protected $_productCollectionFactory;
    public $catalogProductVisibility;

    public function __construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility, \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory, \Magento\Framework\Registry $coreRegistry, \RonisBT\Cms\Helper\Data $helper, array $data = [])
    {
        $this->_productCollectionFactory = $productCollectionFactory;
        $this->catalogProductVisibility = $catalogProductVisibility;
        parent::__construct($context, $coreRegistry, $helper, $data);
    }

    public function getFeaturedProducts(){
        $collection = $this->_productCollectionFactory->create()->addAttributeToSelect('*')
            ->addAttributeToFilter('status', '1')
            ->addAttributeToFilter('featured_product', '1');
        $collection->setVisibility($this->catalogProductVisibility->getVisibleInCatalogIds());
        return $collection;
    }
}

I use it for getting products which marked as featured.

Everything works ok, but I want to order products by id and limit collection to 10 units.

Thank you guys!

3
  • Use setSize function to limit for collection. $collection->setSize(10);
    – akgola
    Aug 30, 2017 at 8:52
  • @akgola, can you also describe what means that row? Aug 30, 2017 at 8:57
  • I think the row sets visibility based off the product's visibility within a catalog, example a product is out of stock it can't be shown. Aug 30, 2017 at 8:59

1 Answer 1

2

Use setPageSize function to limit for collection and order by product_id.

$collection->setPageSize(10)->setOrder('product_id');
1

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.