Debugging Magento 2 Upsell Product Pricing Issues
If you’re working on a Magento 2 module that uses AJAX to load upsell products per customer, you might be facing issues where the product prices are missing when the block is loaded via AJAX. Let’s dive into this common scenario and explore potential solutions.
The Context: Loading Upsell Products with AJAX
You’ve created a custom module where your upsell block extends Magento\Catalog\Block\Product\ProductList\Upsell. In your layout file catalog_product_view.xml, you’re removing the default upsell block and adding your custom one under the content aside container.
<?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>
In your upsell PHTML file, you’re using a script to initialize the JavaScript code for loading the upsells via AJAX.
<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>
The Issue: Missing Product Prices
Your AJAX request is correctly loading the upsell block content. However, when you inspect the loaded HTML, you notice that the product prices are missing from your custom block.
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);
};
});
Potential Causes and Debugging Steps
1. The priceRender Block is Not Being Loaded
Upon closer inspection of the Magento core code in \Magento\Catalog\Block\Product\AbstractProduct, you notice that the line $priceRender = $this->getLayout()->getBlock('product.price.render.default'); always returns false when the block is loaded via AJAX.
$priceRender = $this->getLayout()->getBlock('product.price.render.default');
if ($priceRender instanceof \Magento\Framework\Pricing\Render\RendererInterface) {
return $priceRender->render(
$this->getProductPrice(\Magento\Catalog\Model\Product::PRICE_CODE_STYLE_FIXED),
$product,
[
'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
'show_decimal_no_frac' => true
]
);
}
2. The Default Upsell Block Removal
Initially, you suspected that the issue might be related to the removal of the default upsell block using <referenceBlock name="product.info.upsell" remove="true" />. However, even when you comment out this line and allow both blocks (default and your custom one) to load, the prices are still missing from your AJAX block.
3. Debugging the Layout Configuration
To further investigate, let’s look at your layout file for the upsell AJAX controller action (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>
Potential Solutions
1. Use the Default Upsell Block Template
One approach to troubleshoot this issue is to use the default upsell block template (Magento_Catalog::product/list/items.phtml) for your custom block.
<block class="MyCompany\MyModule\Block\Product\ProductList\Upsell" name="personalised.product.upsell" template="Magento_Catalog::product/list/items.phtml" />
2. Implement a Custom Price Renderer
Another solution is to create your own custom price renderer block that extends Magento\Framework\Pricing\Render\RendererInterface. This allows you to override the pricing logic and ensure it works correctly with AJAX-loaded blocks.
You can find more information about creating custom price renderers in the official Magento 2 documentation: Creating Custom Price Renderers
3. Review the Product Pricing Configuration
Ensure that your product pricing configuration is set up correctly in the Magento admin panel:
- Check if the product prices are not zero or hidden.
- Verify that the tier price configuration (if used) matches the conditions for the current customer.
Conclusion: Troubleshooting AJAX-Loaded Upsell Prices
When dealing with upsell products loaded via AJAX in Magento 2, it’s crucial to carefully inspect the layout and block initialization process. By following the debugging steps outlined above and considering potential solutions such as using the default template or implementing a custom price renderer, you can effectively resolve issues related to missing product prices.
Remember to consult the official documentation and consider reaching out to the community forums for further assistance with your specific implementation requirements.
For more in-depth technical articles on Magento 2 development, visit Laravel Company.