1

Is it possible to return an array from a controller or from a block function to the phtml file?

<?php

namespace Mnuhell\FormSearchAttributes\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Registry;
use Magento\Framework\View\Result\PageFactory;


class Search extends Action
{

/**
 * @var PageFactory
 */
protected $_resultPageFactory;

protected $_resultJsonFactory;

protected $_coreRegistry;


public function __construct(Context $context, PageFactory $resultPageFactory, JsonFactory $_resultJsonFactory, Registry $_coreRegistry)
{
    $this->_resultPageFactory = $resultPageFactory;
    $this->_resultJsonFactory = $_resultJsonFactory;
    $this->_coreRegistry = $_coreRegistry;
    parent::__construct($context);
}

public function execute()
{


    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
    $connection = $resource->getConnection();

    $minPrice = $this->getRequest()->getParam('minPrice');
    $maxPrice = $this->getRequest()->getParam('maxPrice');
    $minQty = $this->getRequest()->getParam('minQty');
    $maxQty = $this->getRequest()->getParam('maxQty');
    //$category = $this->getRequest()->getParam('category');

    if($minPrice != "undefined" AND $maxPrice != "undefined" AND $minQty == "undefined" AND $maxQty == "undefined") {

        $sql = "SELECT cat.entity_id, cat1.value, count(prod.entity_id) AS totals FROM catalog_category_entity as cat 
            LEFT JOIN catalog_category_entity_varchar as cat1 ON cat.entity_id = cat1.entity_id 
            INNER JOIN catalog_category_product as catprod ON catprod.category_id = cat.entity_id 
            INNER JOIN catalog_product_entity as prod ON prod.entity_id = catprod.product_id 
            LEFT JOIN catalog_product_entity_decimal as prec on prec.entity_id = prod.entity_id 
            LEFT JOIN cataloginventory_stock_item AS stock ON stock.product_id = prod.entity_id 
            where children_count > 0 and cat1.attribute_id = 45 and cat.entity_id > 2 
            AND prec.value > $minPrice and prec.value < $maxPrice 
            group by cat.entity_id ";


        $result = $connection->fetchAll($sql);
    }

    if($minPrice != "undefined" AND $maxPrice != "undefined" AND $minQty != "undefined" AND $maxQty != "undefined") {

        $sql = "SELECT cat.entity_id, cat1.value, count(prod.entity_id) AS totals FROM catalog_category_entity as cat 
            LEFT JOIN catalog_category_entity_varchar as cat1 ON cat.entity_id = cat1.entity_id 
            INNER JOIN catalog_category_product as catprod ON catprod.category_id = cat.entity_id 
            INNER JOIN catalog_product_entity as prod ON prod.entity_id = catprod.product_id 
            LEFT JOIN catalog_product_entity_decimal as prec on prec.entity_id = prod.entity_id 
            LEFT JOIN cataloginventory_stock_item AS stock ON stock.product_id = prod.entity_id 
            where children_count > 0 and cat1.attribute_id = 45 and cat.entity_id > 2 
            AND prec.value > $minPrice AND prec.value < $maxPrice
            AND stock.qty > $minQty group by cat.entity_id";

            $result = $connection->fetchAll($sql);
    }

    $sql = "SELECT cat.entity_id, cat1.value, count(prod.entity_id) AS totals FROM catalog_category_entity as cat 
            LEFT JOIN catalog_category_entity_varchar as cat1 ON cat.entity_id = cat1.entity_id 
            INNER JOIN catalog_category_product as catprod ON catprod.category_id = cat.entity_id 
            INNER JOIN catalog_product_entity as prod ON prod.entity_id = catprod.product_id 
            LEFT JOIN catalog_product_entity_decimal as prec on prec.entity_id = prod.entity_id 
            LEFT JOIN cataloginventory_stock_item AS stock ON stock.product_id = prod.entity_id 
            where children_count > 0 and cat1.attribute_id = 45 and cat.entity_id > 2 group by cat.entity_id";

    $result = $connection->fetchAll($sql);

     $resultJson = $this->_resultJsonFactory->create($result); 

    $collection = $resultJson->setData($result);

    return $collection;

 }
}

How I can return collection array json in a phtml file?

Thanks!!

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.