2

Controller

<?php

namespace Test\QuickView\Controller\Catalog\Product;

class View extends \Magento\Catalog\Controller\Product\View
{
}

Layout

quickview_catalog_product_view.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="page.top" remove="true" />
        <referenceContainer name="header.container" remove="true" />
        <referenceContainer name="content">
            <container name="product.info.main" htmlTag="div" htmlClass="product-info-main" before="-">
                <container name="product.info.price" label="Product info auxiliary container" htmlTag="div" htmlClass="product-info-price" after="product.info.review">
                    <container name="product.info.stock.sku" label="Product auxiliary info" htmlTag="div" htmlClass="product-info-stock-sku">
                        <container name="product.info.type" before="-"/>
                        <block class="Magento\Catalog\Block\Product\View\Description" name="product.info.sku" template="product/view/attribute.phtml" after="product.info.type">
                            <arguments>
                                <argument name="at_call" xsi:type="string">getSku</argument>
                                <argument name="at_code" xsi:type="string">sku</argument>
                                <argument name="css_class" xsi:type="string">sku</argument>
                                <argument name="at_label" xsi:type="string">default</argument>
                                <argument name="add_attribute" xsi:type="string">itemprop="sku"</argument>
                            </arguments>
                        </block>
                    </container>
                    <block class="Magento\Catalog\Block\Product\View" name="product.info.review" template="product/view/review.phtml" after="product.info.stock.sku" />
                    <block class="Magento\Catalog\Pricing\Render" name="product.price.final" after="product.info.sku">
                        <arguments>
                            <argument name="price_render" xsi:type="string">product.price.render.default</argument>
                            <argument name="price_type_code" xsi:type="string">final_price</argument>
                            <argument name="zone" xsi:type="string">item_view</argument>
                        </arguments>
                    </block>
                </container>
                <block class="Magento\Catalog\Pricing\Render" name="product.price.tier" after="product.info.price">
                    <arguments>
                        <argument name="price_render" xsi:type="string">product.price.render.default</argument>
                        <argument name="price_type_code" xsi:type="string">tier_price</argument>
                        <argument name="zone" xsi:type="string">item_view</argument>
                    </arguments>
                </block>
                <container name="alert.urls" as="alert_urls" label="Alert Urls" after="product.price.tier"/>
                <block class="Magento\Catalog\Block\Product\View" name="product.info" template="product/view/form.phtml" after="alert.urls">
                    <container name="product.info.form.content" as="product_info_form_content">
                        <block class="Magento\Catalog\Block\Product\View" name="product.info.addtocart" as="addtocart" template="product/view/addtocart.phtml"/>
                    </container>
                    <block class="Magento\Framework\View\Element\Template" name="product.info.form.options" as="options_container">
                        <block class="Magento\Catalog\Block\Product\View" name="product.info.options.wrapper" as="product_options_wrapper" template="product/view/options/wrapper.phtml">
                            <block class="Magento\Catalog\Block\Product\View\Options" name="product.info.options" as="product_options" template="product/view/options.phtml">
                                <block class="Magento\Catalog\Block\Product\View\Options\Type\DefaultType" as="default" template="product/view/options/type/default.phtml"/>
                                <block class="Magento\Catalog\Block\Product\View\Options\Type\Text" as="text" template="product/view/options/type/text.phtml"/>
                                <block class="Magento\Catalog\Block\Product\View\Options\Type\File" as="file" template="product/view/options/type/file.phtml"/>
                                <block class="Magento\Catalog\Block\Product\View\Options\Type\Select" as="select" template="product/view/options/type/select.phtml"/>
                                <block class="Magento\Catalog\Block\Product\View\Options\Type\Date" as="date" template="product/view/options/type/date.phtml"/>
                            </block>
                            <block class="Magento\Framework\View\Element\Html\Calendar" name="html_calendar" as="html_calendar" template="Magento_Theme::js/calendar.phtml"/>
                        </block>
                        <block class="Magento\Catalog\Block\Product\View" name="product.info.options.wrapper.bottom" as="product_options_wrapper_bottom" template="product/view/options/wrapper/bottom.phtml">
                            <block class="Magento\Catalog\Block\Product\View" name="product.info.addtocart.additional" as="product.info.addtocart" template="product/view/addtocart.phtml"/>
                        </block>
                    </block>
                </block>
                <container name="product.info.extrahint" as="extrahint" label="Product View Extra Hint">
                    <container name="product.info.social" label="Product social links container" htmlTag="div" htmlClass="product-social-links" after="product.info.overview">
                        <block class="Magento\Catalog\Block\Product\View" name="product.info.addto" as="addto" template="product/view/addto.phtml"/>
                        <block class="Magento\Catalog\Block\Product\View" name="product.info.mailto" template="product/view/mailto.phtml"/>
                    </container>
                </container>
                <block class="Magento\Catalog\Block\Product\View\Description" name="product.info.overview" template="product/view/attribute.phtml" group="detailed_info" after="product.info.extrahint">
                    <arguments>
                        <argument name="at_call" xsi:type="string">getShortDescription</argument>
                        <argument name="at_code" xsi:type="string">short_description</argument>
                        <argument name="css_class" xsi:type="string">overview</argument>
                        <argument name="at_label" translate="true" xsi:type="string">none</argument>
                        <argument name="title" translate="true" xsi:type="string">Overview</argument>
                        <argument name="add_attribute" xsi:type="string">itemprop="description"</argument>
                    </arguments>
                </block>
            </container>
            <container name="product.info.media" htmlTag="div" htmlClass="product media" after="product.info.main">
                <block class="Magento\Catalog\Block\Product\View\Gallery" name="product.info.media.image" template="product/view/gallery.phtml"/>
            </container>
        </referenceContainer>
        <referenceContainer name="footer-container" remove="true" />
        <referenceBlock name="copyright" remove="true" />
    </body>
</page>

when I go to http://localhost/magento2/quickview/catalog_product/view/id/5 and view source, the page load full content with html head body tags.

I want use ajax get only content of product without html head body tags.

How to make this?

2

To remove HTML head tags, Try this code:

<referenceContainer name="head" remove="true"/>
<referenceContainer name="header" remove="true"/>
<referenceContainer name="menu" remove="true"/>
<referenceContainer name="notifications" remove="true"/>
<referenceContainer name="footer" remove="true"/>
<referenceContainer name="body" remove="true"/>
  • Try to make custom HTML which you want to show as result with use of Ajax request. – user41152 Jun 21 '16 at 11:16
2

I found a better way to get content for ajax calls using any layout and getting exactly the html markup block you need. I wanted to load products depending on the category chosen without a page load but with an ajax call. I created a controller, a block and its corresponding phtml. I tried creating also a layout .xml file for the controller but even in my best attempts I got the <head> with some undesired css or js files as a part of the response to the ajax call...

So I modified my controller in this way:

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

protected $_registry;
protected $template;
protected $pageFactory;
protected $_translateInline;
protected $_resultRawFactory;

public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\Registry $registry,
    \Magento\Framework\View\Element\Template $template,
    \Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
    \Magento\Framework\Translate\InlineInterface $translateInline
)
{
    $this->_registry = $registry;
    $this->template = $template;
    $this->_resultRawFactory = $resultRawFactory;
    $this->_translateInline = $translateInline;
    return parent::__construct($context);
}

public function execute()
{
    $this->_registry->register('categoryId', $this->getRequest()->getParam('categoryId'));
    $this->_registry->register('mode', $this->getRequest()->getParam('mode'));

    $html = $this->template->getLayout()->createBlock('Company\Catalog\Block\Product\AjaxCategoryContent')->toHtml();
    $this->_translateInline->processResponseBody($html);

    $resultRaw = $this->_resultRawFactory->create();
    $resultRaw->setContents($html);
    return $resultRaw;
}
}

So this controller is taking the two parameters provided and writing them in the registry, I use them later in the block. It's creating the block directly there, keeping the html resultant and processing because it has translations/phrases.

And the most important part, instead of create and return the object \Magento\Framework\View\Result\Page, it's creating a raw result \Magento\Framework\Controller\Result\Raw and setting its content with the html from the block. The result is a piece of pure html markup without any <head> or <body>, or well uniquely the content of the phtml file.

  • What does your block file look like? – Alex Jul 13 '17 at 14:15
0

A solution more simple: create a new root.phtml only for you controller action.

In our controller:

class Index extends Action
{

/**
 * @var Onepage
 */
private $onepage;
private $resultPageFactory;

public function __construct(
    Context $context,
    Onepage $onepage,
    PageFactory $resultPageFactory
)
{
    $this->onepage = $onepage;
    $this->resultPageFactory = $resultPageFactory;
    return parent::__construct($context);
}

public function execute()
{
    return $this->resultPageFactory->create(false, [
        'template' => 'GLB_Checkout::root.phtml',
    ]);
}
}

So, you create a new root.phtml in your-module/view/frontend/templates/root.phtml

<html>
<head></head>
<body>
    <h1>It's working...</h1>
</body>

Sorry for indentation.

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.