0

I added a custom category attribute type yes/no like this: enter image description here

In eav_attribute table: enter image description here

the problem is when i filter follow this attribute it return empty array:

$model = $this->categoryFactory->create();
$categories = $model->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('show_category', 1)
->addIsActiveFilter();

Any help? Thank in advance

1 Answer 1

0
 Try this :- 

namespace NameSpace\Module\Block;

    class FeaturedCategories extends \Magento\Framework\View\Element\Template
{


     protected $categoryCollectionFactory;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
        array $data = []
    ) {
        $this->categoryCollectionFactory = $categoryCollectionFactory;
        parent::__construct($context, $data);
    }

    public function getFeaturedCategories()
    {
        $collection = $this->categoryCollectionFactory->create();
        $collection->addAttributeToSelect('*')
            ->addAttributeToFilter('show_category', '1');

        return $collection;
    }
}
2
  • Thank for your answers but it still not working for me.
    – MichaelHa
    Oct 14, 2020 at 2:36
  • what error you are facing in code ? Oct 14, 2020 at 4:33

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.