0

I have created an attribute for product like enable/disable to show product in slider in frontend. How can i get the product collection of the product enabled for this attribute value using category id in magento 2

  • show us what you tried. give us some code and explain whats not working – Philipp Sander May 8 at 6:51
  • what is your attribute type? – Devidas May 8 at 6:52
  • @Devidas Its YES/NO see prnt.sc/sd031f Want to retrieve products based on this value and specific to category – Nafsss May 8 at 6:53
0

Check below code

I have created a block and set collection in this file and get in the template file.

I have got product collection in current category id and set best_seller custom attribute filter

<?php 

namespace Vendor\Module\Block;

use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\View\Page\Config;
use Magento\Framework\Registry;
use Magento\Catalog\Model\CategoryFactory;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Model\Product\Attribute\Source\Status;

class Sandwich extends Template
{
    /**
     * @var \Magento\Framework\Registry $registry
     */
    private $_registry;
    /**
     * @var \Magento\Catalog\Model\CategoryFactory $categoryfactory
     */
    private $_categoryFactory;
    /**
     * @var Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
     */
    protected $_productCollectionFactory;
    /**
     * Constructor
     * @param \Magento\Framework\App\Helper\Context $context
     * @param \Magento\Catalog\Model\Session $catalogSession
     **/
    public function __construct(
        Context $context,
        Config $pageConfig,
        Registry $registry,
        CategoryFactory $categoryfactory,
        CollectionFactory $productCollectionFactory,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->pageConfig = $pageConfig;
        $this->_registry = $registry;
        $this->_categoryFactory = $categoryfactory;
        $this->_productCollectionFactory = $productCollectionFactory;
    }
    /**
    *@return Array
    **/
    public function getCategoryProductAttributeData()
    {
        //Attribute best_sellers
        //1 = Yes
        //0 = No
        $category_load = $this->_registry->registry('current_category');
        if($category_load):
            $categoryId = $category_load->getId();
            $category_product_collection = $this->_categoryFactory->create()->load($categoryId);
            $collection = $this->_productCollectionFactory->create();
            $collection->addAttributeToSelect('*');
            $collection->addCategoryFilter($category_product_collection);
            $collection->addAttributeToFilter('visibility', Visibility::VISIBILITY_BOTH);
            $collection->addAttributeToFilter('status', Status::STATUS_ENABLED);
            $collection->addAttributeToFilter('best_sellers',1);
            $collection->setPageSize(4);

            return $collection;

        endif;
    }
}
?>

I hope this will help you.

| improve this answer | |
  • if you have any query feel free to ask me – Devidas May 8 at 7:11
  • Here why we are loading entire category? cant we pass just category id in filter? – Nafsss May 8 at 8:40
  • you can pass category id in filter @Nafisa – Devidas May 8 at 8:45
  • @Devidad .. How?? then we need to load category collection? – Nafsss May 8 at 9:11
  • i am getting this error Type Error occurred when creating object: Mageplaza\Productslider\Block\BestSellerProducts\Interceptor – Nafsss May 8 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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