0

I am trying to create a controller than I am using to load products into a page via ajax. However when i set the controller to just be of type layout the pricing no longer shows. However when using a standard page the prices show fine:

<?php


namespace Harrigo\ContractPricing\Controller\ContractProducts;

use Magento\Framework\Controller\ResultFactory;

class AjaxProducts extends \Magento\Framework\App\Action\Action
{

    protected $resultPageFactory;

    /**
     * Constructor
     * 
     * @param \Magento\Framework\App\Action\Context  $context
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    )
    {
        $this->resultPageFactory = $resultPageFactory;
        $this->_resultFactory = $context->getResultFactory();
        parent::__construct($context);
    }
    /**
     * Execute view action
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
            $resultLayout = $this->_resultFactory->create(ResultFactory::TYPE_LAYOUT);
            return $resultLayout;   

    }
}

This however shows pricing fine:

<?php
namespace Harrigo\ContractPricing\Controller\ContractProducts;

use Magento\Framework\Controller\ResultFactory;

class AjaxCategory extends \Magento\Framework\App\Action\Action
{

    protected $resultPageFactory;
    /**
     * Constructor
     *
     * @param \Magento\Framework\App\Action\Context  $context
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    /**
     * Execute view action
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        return $this->resultPageFactory->create();
    }
}

Both use the same block level code and templates.

Have also attempted to use controller like so:

<?php
namespace Harrigo\ContractPricing\Controller\ContractProducts;

class AjaxProducts extends \Magento\Framework\App\Action\Action
{
    protected $resultLayoutFactory;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\LayoutFactory $layoutFactory,
        \Magento\Framework\Controller\Result\JsonFactory $jsonResultFactory
    ) {
        $this->layoutFactory = $layoutFactory;
        $this->jsonResultFactory = $jsonResultFactory;
        parent::__construct($context);
    }

    /**
     * Execute view action
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        $productsBlockHtml = $this->layoutFactory->create()->createBlock('Harrigo\ContractPricing\Block\Index\Pricing')->setTemplate('Harrigo_ContractPricing::index/ajaxproducts.phtml')->toHtml();
        return $this->jsonResultFactory->create()->setData(['html' => [
            'products_list' => $productsBlockHtml
        ]]);
    }
}

However pricing just stops showing still.

0

Adding the value empty to the update handle seems to fix the issue.

<?xml version="1.0" ?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
   <update handle="empty"/>
   <container name="root" label="Root">
        <block class="Harrigo\ContractPricing\Block\Index\Pricing" name="contractproducts" template="Harrigo_ContractPricing::index/ajaxproducts.phtml" cacheable="false"></block>
    </container>
</layout>

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.