2

In list.phtml I am fetching newly created block's function. like e.g.

$blockObj= $block->getLayout()->createBlock('Custom\Module\Block\Entree');

$dataget = $blockObj->getProductdata($_productCollection);

Now I am using this $dataget variable by replacing of $_productCollection variable. But product count showing wrong. How can I use below $call variable and return it from this block to my phtml, coz having problem as its in foreach loop. I have created block, but collection is showing wrong. Please find below my block code:

<?php
 /**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
  */

 namespace Custom\Module\Block;

class Entree extends \Magento\Framework\View\Element\Template
{
protected $_productRepository;
protected $attributeRepository;
protected $_categoryFactory;
private $registry;

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    \Magento\Catalog\Model\ProductRepository $productRepository,
    \Magento\Catalog\Model\Product\Attribute\Repository $attributeRepository,
    \Magento\Catalog\Model\CategoryFactory $categoryFactory,
    \Magento\Framework\Registry $registry,
    array $data = []
)
{
    $this->registry = $registry;
    $this->_categoryFactory = $categoryFactory;
    $this->attributeRepository = $attributeRepository;
    $this->_productRepository = $productRepository;
    $this->productCollectionFactory = $productCollectionFactory;
    parent::__construct($context);
}
public function getProductdata($_productCollection)
{
     $currentCategory = $this->registry->registry('current_category');
     $ids = $currentCategory->getParentCategory()->getId();//$currentCategory->getId();

        $category = $this->_categoryFactory->create()->load($ids);

        $filterOptionIds = array();
        foreach($_productCollection as $products){
            if($products->getFilterType()){

                $filterOptionIds[][$products->getId()] = explode(",", $products->getFilterType());

            }
        }

        foreach ($filterOptionIds as $filterOption) {

            if($filterOption != ''){


            $coll = $this->productCollectionFactory->create()->addAttributeToFilter('filter_type', array('eq' => $filterOption))->addCategoryFilter($category)->load();


            }
        }

Your Answer

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

Browse other questions tagged or ask your own question.