2

I try to get a collection of configurable product but disabled product are showing up.here is my code

  $currentStoreId = $this->_storeManager->getStore()->getId();
    $this->_productCollection = $this->_reportCollectionFactory->create()
                    ->addAttributeToSelect('*')             
                    ->addViewsCount()
                    ->setStoreId($currentStoreId)
                    ->addStoreFilter($currentStoreId)
                    ->addFilter('type_id','configurable' )

So I tried

 $this->_productCollection->addAttributeToFilter('status', array('eq' => 1) );
 $this->_productCollection->addAttributeToFilter('status', ['in' => $this->_productStatus->getVisibleStatusIds()]);
 $this->_productCollection->addAttributeToFilter(
                            'status', array('eq' => \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
                        );

But none on them works, It return all product since it seems that status of disabled product is 1 also:

var_dump($_product->getStatus())

return 1 on disabled and enabled product

I tried to refresh cache php bin/magento cache:flush, remove everything in var/generation, also reindex php bin/magento indexer:reindex but no luck.

Is it a bug or am I missing something?

Here is my complete class

use Magento\Catalog\Api\CategoryRepositoryInterface;


class ListProduct extends \Magento\Catalog\Block\Product\ListProduct{


    protected $_reportCollectionFactory;
    protected $_typeFilter;




     public function __construct(
        //parent
        \Magento\Catalog\Block\Product\Context $context,
        \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
        \Magento\Catalog\Model\Layer\Resolver $layerResolver,
        CategoryRepositoryInterface  $categoryRepository,
        \Magento\Framework\Url\Helper\Data $urlHelper,

        \Magento\Reports\Model\ResourceModel\Product\CollectionFactory $reportCollectionFactory,
        \Magento\Catalog\Model\Product\Attribute\Source\Status $productStatus,
        \Magento\Catalog\Model\Product\Visibility $productVisibility,

        array $data = []
    ) {


        $this->_reportCollectionFactory = $reportCollectionFactory;

        parent::__construct(
            $context,
            $postDataHelper,
            $layerResolver,
            $categoryRepository,
            $urlHelper,
            $data
        );
    }

  protected function _getProductCollection($force_reload = false){
        if ($this->_productCollection === null|| $force_reload == true) {

                $currentStoreId = $this->_storeManager->getStore()->getId();

                $this->_productCollection = $this->_reportCollectionFactory->create()

                    ->addAttributeToSelect('*')

                    ->addViewsCount()
                    ->setStoreId($currentStoreId)
                    ->addStoreFilter($currentStoreId)
                    //->addAttributeToFilter('status', array('eq' => 1)); 
                    //->addAttributeToFilter('status', ['in' => $this->_productStatus->getVisibleStatusIds()])
                    //->setVisibility($this->_productVisibility->getVisibleInSiteIds());
                    ;

                if (!empty($this->_typeFilter)){
                    $this->_productCollection->addFilter('type_id',$this->_typeFilter );  
                }



        }
        return $this->_productCollection;
   }

    public function setTypeFilter($typeFilter = []){
        return $this->_typeFilter = $typeFilter; 
    }

    public function getFilteredCollection($typeFilter = []){
        $this->setTypeFilter($typeFilter); 
        return $this->_getProductCollection(true);
    }







}
5
  • magento.stackexchange.com/questions/80179/… <--- You can try this one as reference maybe you can get something out of that thread. And also have you tried reindexing your site? – MazeStricks Mar 10 '17 at 1:53
  • I tried that already, problem is the 'status' filter doesn't work, it seems that every products got the status value to 1, instead of 2 for disabled – pajouk Mar 10 '17 at 1:57
  • Hmmmm that seems to be a bug as per my research in magento 2. But I'm not really sure if it's a bug. What is the version of your current magento 2? – MazeStricks Mar 10 '17 at 2:05
  • It's magento version 2.1.1 – pajouk Mar 10 '17 at 2:38
  • @pajouk please show your _constuct() method – Rakesh Jesadiya Mar 10 '17 at 5:06
2

Change your store to Default Store View* in backend and disable the product. Then check it again with your code.

3
  • Thanks that was the problem, Magento create an 'enable' variable for each store view and changing it from the default view doesn't change the child view – pajouk Jun 15 '17 at 7:26
  • In DB I have checked it is disabled(having value 2) but in front-end it shows 1.Do I need to reindex it? – Knight017 Oct 16 '18 at 12:08
  • Yes. Reindex the product and then check. – Dinesh Yadav Oct 16 '18 at 13:15
8

For Only Enable and In stock product visibility,

use Magento\Catalog\Api\CategoryRepositoryInterface;

class ListProduct extends \Magento\Catalog\Block\Product\ListProduct{

 public function __construct(
    \Magento\Catalog\Block\Product\Context $context,
    \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
    \Magento\Catalog\Model\Layer\Resolver $layerResolver,
    CategoryRepositoryInterface  $categoryRepository,
    \Magento\Framework\Url\Helper\Data $urlHelper,
    \Magento\Catalog\Model\ProductFactory $productFactory, 
    \Magento\CatalogInventory\Helper\Stock $stockFilter,
    array $data = []
) {

    $this->_productFactory = $productFactory;
    $this->_stockFilter = $stockFilter; 
    parent::__construct(
        $context,
        $postDataHelper,
        $layerResolver,
        $categoryRepository,
        $urlHelper,
        $data
    );
}
public function _getProductCollection(){
        $collection = $this->_productFactory->create()->getCollection();
        $collection->addAttributeToSelect('*');
        $collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
        //only filter in stock product
        $this->_stockFilter->addInStockFilterToCollection($collection);
        return $collection;
}
5
  • It doesn't work, since \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED is the same as 1, it return all the products. Somehow, disabled products seems to have 1 as status value – pajouk Mar 10 '17 at 5:57
  • Remove var folder from root and Just try with above code. Its working fine for my case. – Rakesh Jesadiya Mar 10 '17 at 6:02
  • I did, still not working. In the catalog from the backend, there are noted as disabled, but on the frontend $_product->getStatus() return 1 for disabled product . So it look like cache issue or 'status' is not loaded somehow – pajouk Mar 10 '17 at 6:16
  • please first do reindex and clear cache. Its working solutions. – Rakesh Jesadiya Mar 10 '17 at 6:18
  • I did that. I know it should work, that's why I post here. Also in your solution, Filters are missing ->addViewsCount() ->setStoreId($currentStoreId) ->addStoreFilter($currentStoreId)->addFilter('type_id',$this->_typeFilter ) – pajouk Mar 10 '17 at 6:29
0

For getting product collection with status enable

protected $_coreRegistry = null;

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Framework\Registry $registry,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    \Magento\Catalog\Model\Product\Attribute\Source\Status $productStatus,
    array $data = []
)
{

    $this->_coreRegistry = $registry;
    $this->_productCollectionFactory = $productCollectionFactory;
    $this->productStatus = $productStatus;
    parent::__construct($context, $data);
}

public function _getProductCollection()
{
    $collection = $this->_productCollectionFactory->create();
    $collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()]);

    return $collection;
}
1
  • doesn't work, disabled and enabled products still showing up – pajouk Mar 10 '17 at 6:01

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.