Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 当区块加载AJAX时,Magento 2中缺少追加销售产品价格_Php_Ajax_Magento_Magento2 - Fatal编程技术网

Php 当区块加载AJAX时,Magento 2中缺少追加销售产品价格

Php 当区块加载AJAX时,Magento 2中缺少追加销售产品价格,php,ajax,magento,magento2,Php,Ajax,Magento,Magento2,我正在开发一个Magento 2模块,它使用AJAX加载upsell产品。每个客户的upsell产品可能不同,因此使用AJAX加载块以允许缓存破坏 为此,我有一个自定义模块,其中我的块扩展了\Magento\Catalog\block\Product\ProductList\Upsell。在catalog\u product\u view.xml的模块布局中,我有以下内容- <?xml version="1.0"?> <page xmlns:xsi="http://www.w3

我正在开发一个Magento 2模块,它使用AJAX加载upsell产品。每个客户的upsell产品可能不同,因此使用AJAX加载块以允许缓存破坏

为此,我有一个自定义模块,其中我的块扩展了
\Magento\Catalog\block\Product\ProductList\Upsell
。在
catalog\u product\u view.xml的模块布局中,我有以下内容-

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="product.info.upsell" remove="true" />
        <referenceContainer name="content.aside">
            <block class="MyCompany\MyModule\Block\Product\ProductList\Upsell"
                   name="personalised.product.upsell"
                   template="MyCompany_MyModule::upsell.phtml" />
        </referenceContainer>
    </body>
</page>
我的控制器(upsellAjax)——

我的
个性化产品\u upsellajax.xml
-

<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
    <container name="root">
        <block class="MyCompany\MyModule\Block\Product\ProductList\Upsell" name="product.info.personalised.upsell" template="Magento_Catalog::product/list/items.phtml">
            <arguments>
                <argument name="type" xsi:type="string">upsell</argument>
            </arguments>
        </block>
    </container>
</layout>
我想这可能与在我的布局中首先删除upsell块有关,即
我决定注释掉这一行,这会导致出现两个upsell块,一个是默认加载的块,另一个是我的AJAX块。默认块显示正确信息的结果相同,但是我的AJAX块仍然缺少价格

非常感谢您的帮助。

您可以尝试:

<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
    <container name="root">
        <block class="Magento\Catalog\Block\Product\ProductList\Upsell" name="product.info.upsell" template="Magento_Catalog::product/list/items.phtml">
            <arguments>
                <argument name="type" xsi:type="string">upsell</argument>
            </arguments>
        </block>
        <block class="Magento\Framework\Pricing\Render" name="product.price.render.default">
            <arguments>
                <argument name="price_render_handle" xsi:type="string">catalog_product_prices</argument>
                <argument name="use_link_for_as_low_as" xsi:type="boolean">true</argument>
            </arguments>
        </block>
    </container>
</layout>

追加销售
目录产品价格
真的

我最近遇到了同样的问题,经过大量的挖掘和搜索,我们似乎找到了解决方案。将
添加到布局文件似乎可以解决问题并显示定价。因此,在您的情况下,布局最终会像

<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
<update handle="empty"/>
        <container name="root">
            <block class="MyCompany\MyModule\Block\Product\ProductList\Upsell" name="product.info.personalised.upsell" template="Magento_Catalog::product/list/items.phtml">
                <arguments>
                    <argument name="type" xsi:type="string">upsell</argument>
                </arguments>
            </block>
        </container>
    </layout>

追加销售

我无法解释它为什么起作用。我是通过查看1列的布局并以此为基础得出这一结论的。到目前为止,我们已经在两个不同的地方使用它来解决这个问题。

我遇到了相同的问题:块
product.price.render.default
需要在布局中以及它的一些子块中可用,才能呈现价格。答案很简单:由于此块是为
默认
句柄加载的,因此请确保此句柄在布局中可用,方法是在请求的早期添加它:

public function __construct(Context $context, LayoutInterface $layout)
{
    parent::__construct($context);

    $this->layout = $layout;
    $this->layout->getUpdate()->addHandle('default');
}

虽然这段代码可能会回答这个问题,但提供关于如何和/或为什么解决问题的附加上下文将提高答案的长期价值。您找到任何解决方案了吗?根据我的经验,这是可行的,尽管使用
句柄可能更好,所以
$this->_layout->getUpdate()->addHandle('empty')
默认句柄会导致不必要的块及其逻辑的加载,而Magento通过
empty
句柄添加price renderer块,而不是其他。
<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
    <container name="root">
        <block class="Magento\Catalog\Block\Product\ProductList\Upsell" name="product.info.upsell" template="Magento_Catalog::product/list/items.phtml">
            <arguments>
                <argument name="type" xsi:type="string">upsell</argument>
            </arguments>
        </block>
    </container>
</layout>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
    <container name="root">
        <block class="Magento\Catalog\Block\Product\ProductList\Upsell" name="product.info.upsell" template="Magento_Catalog::product/list/items.phtml">
            <arguments>
                <argument name="type" xsi:type="string">upsell</argument>
            </arguments>
        </block>
        <block class="Magento\Framework\Pricing\Render" name="product.price.render.default">
            <arguments>
                <argument name="price_render_handle" xsi:type="string">catalog_product_prices</argument>
                <argument name="use_link_for_as_low_as" xsi:type="boolean">true</argument>
            </arguments>
        </block>
    </container>
</layout>
<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
<update handle="empty"/>
        <container name="root">
            <block class="MyCompany\MyModule\Block\Product\ProductList\Upsell" name="product.info.personalised.upsell" template="Magento_Catalog::product/list/items.phtml">
                <arguments>
                    <argument name="type" xsi:type="string">upsell</argument>
                </arguments>
            </block>
        </container>
    </layout>
public function __construct(Context $context, LayoutInterface $layout)
{
    parent::__construct($context);

    $this->layout = $layout;
    $this->layout->getUpdate()->addHandle('default');
}