我正在开发一个使用 AJAX 加载追加销售产品的 Magento 2 模块。每个客户的追加销售产品可能不同,因此使用 AJAX 加载 block 以允许缓存破坏。
为此,我有一个自定义模块,其中我的 block 扩展了 \Magento\Catalog\Block\Product\ProductList\Upsell
.在 catalog_product_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>
在我的 upsell.phtml -
<div id="personalised-upsells-container" data-role="personalised-upsells"></div>
<script type="text/x-magento-init">
{
"*": {
"MyCompany_MyModule/js/upsell": {
"upsellAjaxUrl": "<?php echo $block->getUpsellAjaxUrl(); ?>"
}
}
}
</script>
getUpsellAjaxUrl()
生成 http://magento2.dev/personalised/products/upsellAjax/id/6
我的 upsell.js -
define([
'jquery',
'upsellProducts'
], function($) {
function getUpsellContent(url) {
$.ajax({
url: url,
dataType: 'html'
}).done(function (data) {
$('#personalised-upsells-container').html(data).promise().done(function(){
$('.upsell').upsellProducts();
});
});
}
return function (config, element) {
getUpsellContent(config.upsellAjaxUrl);
};
});
我的 Controller (upsellAjax) -
class UpsellAjax extends ProductController
{
public function execute()
{
$productId = (int) $this->getRequest()->getParam('id');
$product = $this->loadProduct($productId);
if (!$product) {
return false;
}
/** @var \Magento\Framework\View\Result\Layout $resultLayout */
$resultLayout = $this->resultFactory->create(ResultFactory::TYPE_LAYOUT);
return $resultLayout;
}
}
我的 personalised_products_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>
如您所料,这会通过 ajax 正确加载产品追加销售 block ,将 HTML 推送到我的容器中,然后初始化 upsellProducts
页面上的小部件。我的追加销售产品按预期显示但没有价格。
我已经尝试了一些方法来调试这种情况,但据我所知,它无法加载 priceRender
在 \Magento\Catalog\Block\Product\AbstractProduct
的第 428 行在getProductPriceHtml()
里面方法。线路$priceRender = $this->getLayout()->getBlock('product.price.render.default');
总是返回 false
当 block 通过 AJAX 加载时。
当我将我的 block 替换为布局中的默认 block 时也是这种情况 (personalised_products_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="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>
我想这可能与在我的布局中首先删除加售 block 有关,即 <referenceBlock name="product.info.upsell" remove="true" />
我决定将这一行注释掉,这会导致出现两个追加销售 block ,一个是默认加载的 block ,另一个是我的 AJAX block 。默认 block 显示正确信息的结果相同,但我的 AJAX block 仍然缺少价格。
如有任何帮助,我们将不胜感激。
最佳答案
我有同样的问题: block product.price.render.default
需要在布局中可用,以及它的一些子 block ,以呈现价格。答案的想法很简单:因为这个 block 是为 default
句柄加载的,所以通过在请求的早期添加它来确保这个句柄在布局中可用:
public function __construct(Context $context, LayoutInterface $layout)
{
parent::__construct($context);
$this->layout = $layout;
$this->layout->getUpdate()->addHandle('default');
}
关于php - 当使用 AJAX 加载 block 时,Magento 2 中缺少追加销售产品价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34629019/