cancel
Showing results for 
Search instead for 
Did you mean: 

Magento product collection filter not working

Magento product collection filter not working

I am using magento list file class with override productlist.php.So i can use product collection object and try to filter using type_id but nothing happend.

 

Here is my code.

 

<?php

use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Framework\Json\Helper\Data as jsondata;
class ListProduct extends \Magento\Catalog\Block\Product\ListProduct
{
    /*protected $jsonHelper;*/

    /*public function __construct(\Magento\Framework\Json\Helper\Data $jsonHelper)
    {
        $this->jsonHelper = $jsonHelper;
    }*/
  protected $priceCurrency;
    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,
        array $data = [],
        jsondata $jsonHelper,
        PriceCurrencyInterface $priceCurrency
    ) {
        $this->jsonHelper = $jsonHelper;
        $this->_catalogLayer = $layerResolver->get();
        $this->_postDataHelper = $postDataHelper;
        $this->categoryRepository = $categoryRepository;
        $this->urlHelper = $urlHelper;
        $this->priceCurrency = $priceCurrency;
        parent::__construct(
            $context,
            $postDataHelper,
            $layerResolver,
            $categoryRepository,
            $urlHelper,
            $data

        );
    }

    public function getProductCollectinJsonData()
    {
        $_productCollection = $this->_productCollection->addAttributeToFilter('type_id','simple')->addTierPriceData();
        $i                  = 0;
        $ProductData        = array();
        foreach ($_productCollection as $product) {
            //if ($product->getTypeId() == 'simple') {

                $ProductData[$i]['Product Name'] = '<a href="'.$product->getProductUrl().'" title="'.$product->getName().'">'.$product->getName().'</a>';
        $showAttributes = explode(',', $this->getLayer()->getCurrentCategory()->getAttributeDisplayOnlisting());
        foreach ($product->getAttributes() as $attribute) {
          //echo $attribute->getStoreLabel().'<br>';
          //echo "<pre/>";print_r($attribute->getFrontend()->getValue($product));
          if(in_array($attribute->getStoreLabel(), $showAttributes)){
          if(!is_object($attribute->getFrontend()->getValue($product))){
              $ProductData[$i][$attribute->getStoreLabel()] = $attribute->getFrontend()->getValue($product);
            }else{
              $ProductData[$i][$attribute->getStoreLabel()] = '';
            }
          }
        }
                if ($product->getTierPrice()) {
                    $tierPriceList = $product->getTierPrice();
                    if (count($tierPriceList) > 0) {
                        $ProductData[$i]['1-' . (intval($tierPriceList[0]['price_qty'])) . ' Packs'] = $this->getFormatedPrice($product->getPrice());
                    }
                    $count = 0;
                    foreach ($tierPriceList as $key => $trList) {
                        if ($count == (count($tierPriceList) - 1)) {
                            $ProductData[$i][intval($trList['price_qty']) . '+ Packs'] = $this->getFormatedPrice($trList['price']);
                        } else {
                            $ProductData[$i][intval($trList['price_qty'] + 1) . '-' . (intval($tierPriceList[$key + 1]['price_qty'])) . ' Packs'] = $this->getFormatedPrice($trList['price']);
                        }

                        $count++;
                    }
                }

                $ProductData[$i]['QTY'] = '<div class="control qty"><input type="number" name="multi_add['.$product->getId().']"
                                    data-selector="multi_add['.$product->getId().']"
                                    maxlength="12"
                                    value="0"
                                    title="Qty"
                                    class="input-text qty"
                                    /><div>';
                $i++;
            //}
        }
        //return $this->jsonHelper->jsonEncode($ProductData);
    //echo "<pre/>";print_r($ProductData);exit;
        return $ProductData;
    }
   public function getFormatedPrice($amount)
    {
        return $this->priceCurrency->convertAndFormat($amount);
    }
}
1 REPLY 1

Re: Magento product collection filter not working

You need to add eq operation between your addAttributeToFilter function.

 

Try below code : 

 

<?php

use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Framework\Json\Helper\Data as jsondata;
class ListProduct extends \Magento\Catalog\Block\Product\ListProduct
{
    /*protected $jsonHelper;*/

    /*public function __construct(\Magento\Framework\Json\Helper\Data $jsonHelper)
    {
        $this->jsonHelper = $jsonHelper;
    }*/
  protected $priceCurrency;
    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,
        array $data = [],
        jsondata $jsonHelper,
        PriceCurrencyInterface $priceCurrency
    ) {
        $this->jsonHelper = $jsonHelper;
        $this->_catalogLayer = $layerResolver->get();
        $this->_postDataHelper = $postDataHelper;
        $this->categoryRepository = $categoryRepository;
        $this->urlHelper = $urlHelper;
        $this->priceCurrency = $priceCurrency;
        parent::__construct(
            $context,
            $postDataHelper,
            $layerResolver,
            $categoryRepository,
            $urlHelper,
            $data

        );
    }

    public function getProductCollectinJsonData()
    {
        $_productCollection = $this->_productCollection->addAttributeToFilter('type_id',array('eq' => 'simple'))->addTierPriceData();
        $i                  = 0;
        $ProductData        = array();
        foreach ($_productCollection as $product) {
            //if ($product->getTypeId() == 'simple') {

                $ProductData[$i]['Product Name'] = '<a href="'.$product->getProductUrl().'" title="'.$product->getName().'">'.$product->getName().'</a>';
        $showAttributes = explode(',', $this->getLayer()->getCurrentCategory()->getAttributeDisplayOnlisting());
        foreach ($product->getAttributes() as $attribute) {
          //echo $attribute->getStoreLabel().'<br>';
          //echo "<pre/>";print_r($attribute->getFrontend()->getValue($product));
          if(in_array($attribute->getStoreLabel(), $showAttributes)){
          if(!is_object($attribute->getFrontend()->getValue($product))){
              $ProductData[$i][$attribute->getStoreLabel()] = $attribute->getFrontend()->getValue($product);
            }else{
              $ProductData[$i][$attribute->getStoreLabel()] = '';
            }
          }
        }
                if ($product->getTierPrice()) {
                    $tierPriceList = $product->getTierPrice();
                    if (count($tierPriceList) > 0) {
                        $ProductData[$i]['1-' . (intval($tierPriceList[0]['price_qty'])) . ' Packs'] = $this->getFormatedPrice($product->getPrice());
                    }
                    $count = 0;
                    foreach ($tierPriceList as $key => $trList) {
                        if ($count == (count($tierPriceList) - 1)) {
                            $ProductData[$i][intval($trList['price_qty']) . '+ Packs'] = $this->getFormatedPrice($trList['price']);
                        } else {
                            $ProductData[$i][intval($trList['price_qty'] + 1) . '-' . (intval($tierPriceList[$key + 1]['price_qty'])) . ' Packs'] = $this->getFormatedPrice($trList['price']);
                        }

                        $count++;
                    }
                }

                $ProductData[$i]['QTY'] = '<div class="control qty"><input type="number" name="multi_add['.$product->getId().']"
                                    data-selector="multi_add['.$product->getId().']"
                                    maxlength="12"
                                    value="0"
                                    title="Qty"
                                    class="input-text qty"
                                    /><div>';
                $i++;
            //}
        }
        //return $this->jsonHelper->jsonEncode($ProductData);
    //echo "<pre/>";print_r($ProductData);exit;
        return $ProductData;
    }
   public function getFormatedPrice($amount)
    {
        return $this->priceCurrency->convertAndFormat($amount);
    }
}

Try the above code and let me know if issue is still not resolved.

if issue solved,Click Kudos & Accept as Solution