2

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);
    }
}
2

Here is the solution for using custom collection for above problem.

<?php
namespace Kite\Boxes\Block\Rewrite\Product;
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 $_productCollectionFactory;
  protected $priceCurrency;
  protected $_coreRegistry = null;
    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,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        \Magento\Catalog\Model\Product\Attribute\Source\Status $productStatus,
       \Magento\Catalog\Model\Product\Visibility $productVisibility

    ) {
          $this->jsonHelper = $jsonHelper;
        $this->_catalogLayer = $layerResolver->get();
        $this->_postDataHelper = $postDataHelper;
        $this->categoryRepository = $categoryRepository;
        $this->urlHelper = $urlHelper;
        $this->priceCurrency = $priceCurrency;
        $this->_productCollectionFactory = $productCollectionFactory;
        $this->productStatus = $productStatus;
        $this->productVisibility = $productVisibility;
        parent::__construct(
            $context,
            $postDataHelper,
            $layerResolver,
            $categoryRepository,
            $urlHelper,
            $data

        );
    }

    public function getProductCollectinJsonData()
    {
    $_productCollection = $this->_productCollectionFactory->create();
    $_productCollection->addAttributeToSelect('*');
    $_productCollection->addAttributeToFilter('type_id','simple');
    $_productCollection->addCategoriesFilter(['eq' => 28]);
    $_productCollection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()]);
    $_productCollection->setVisibility($this->productVisibility->getVisibleInSiteIds())->addTierPriceData();;
    //echo "<pre/>";print_r($collection->getData());exit;


        //$_productCollection = $this->getLoadedProductCollection()->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);
    }
}

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.