Magento2: Upsell product prices missing when block loaded with AJAX

Created on 6 Jan 2016  路  8Comments  路  Source: magento/magento2

While I am not 100% sure if this is a bug or if it's something I have not implemented correctly but I am failing to find any evidence I am doing it wrong. I have posted this question on StackOverflow to ask the wider community however this has not been successful.

I am working on a Magento 2 module that uses AJAX to load the upsell products. The upsell products can be different per customer so AJAX is used to load the block to allow for cache busting.

For this I have a custom module where my block extends the \Magento\Catalog\Block\Product\ProductList\Upsell.

In the modules layout for app/code/MyCompany/MyModule/view/frontend/layout/catalog_product_view.xml

I have the following -

<?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 my app/code/MyCompany/MyModule/view/frontend/templates/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>

The getUpsellAjaxUrl() method generates http://magento2.dev/personalised/products/upsellAjax/id/6

In my app/code/MyCompany/MyModule/view/frontend/web//js/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);
    };
});

In my app/code/MyCompany/MyModule/Controller/products/upsellAjax.php -

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;
    }
}

In my app/code/MyCompany/MyModule/view/frontend/layout/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>

As you would expect this correctly loads the product upsell block via ajax, pushes the HTML into my container then initialises the upsellProducts widget on the page. My upsell products show as expected but without prices.

I have tried a couple of things to debug the situation but as far as I can see it fails to load the priceRender on line 428 of \Magento\Catalog\Block\Product\AbstractProduct inside the getProductPriceHtml() method. The line $priceRender = $this->getLayout()->getBlock('product.price.render.default'); always returns false when the block is loaded via AJAX.

This is also the situation when I replace using my block for the default block within the layout (app/code/MyCompany/MyModule/view/frontend/layout/personalised_products_upsellajax.xml) e.g.

<?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>

I was thinking it might have something to do with the removal of the upsell block first in my layout i.e <referenceBlock name="product.info.upsell" remove="true" /> I decided to comment this line out which results in two upsell blocks appearing, one is the default loaded block and the other is my AJAX block. Same results where the default block show correct information however my AJAX block is still missing prices.

The issue is persistent in both community and enterprise edition.

Any help would be greatly appreciated.

Tax Format is not valid needs update bug report

Most helpful comment

@magento-engcom-team Thanks for getting back to me 22 months later. Not only is this the slowest response for anyone to receive its also the most irrelevant. Magento 2 moved on by two major versions in this time. The detailed explanation given in January 2016 was relevant at the time on the Magento 2.0.0 version.

This is also the reason there are a large number of merchants moving away from Magento since it cannot answer business critical questions in a timely manner.

I look forward to your reply in 2019 馃ぃ

All 8 comments

@richdynamix thank you for your report.
Is this issue still actual?
If it is, please provide used Magento version.
If it is not, please close this issue.

@veloraven I am not sure if this is still an issue as I reported this over 8 months ago and had to do a workaround for the module. That said, this was with the stable community and enterprise edition 2.0.0.

I would have hoped for a speeder response along with any internal investigation if you felt this was a bug based on my initial description. If you don't think this is a bug and it is indeed intended behaviour then perhaps confirmation of that before I prematurely close the ticket.

I know this is an old issue but we were running into a similar issue. I have found that adding <update handle="empty"/> to your layout seems to resolve the issue. So in the example you gave above it would end up looking like

<?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">
<update handle="empty"/>
        <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>

I'm really not sure why it fixes the issue, but it did in both cases we were running into where ajax requests weren't returning prices. It should probably be noted that if we used a handle instead of it worked as well but also included head tags we didn't need.

@davepullen which Magento version do you use?

Community 2.1.0

@richdynamix, thank you for your report.
Please use the issue reporting guidelines to report an issue. Please, edit issue description and include the preconditions, the steps required to reproduce, the actual result, and the expected result.
Please, add the following information:

  • [ ] Preconditions: Magento version
  • [ ] Steps to reproduce
  • [ ] Expected result
  • [ ] Actual result

@magento-engcom-team Thanks for getting back to me 22 months later. Not only is this the slowest response for anyone to receive its also the most irrelevant. Magento 2 moved on by two major versions in this time. The detailed explanation given in January 2016 was relevant at the time on the Magento 2.0.0 version.

This is also the reason there are a large number of merchants moving away from Magento since it cannot answer business critical questions in a timely manner.

I look forward to your reply in 2019 馃ぃ

Hi @richdynamix
Thank you for your response. We are processing our tech dept on GitHub issues now and trying to respond on new reports ASAP.
Closing this issue now. If you still have the issue, please follow issue reporting guideline to get the response faster.
Thank you

Was this page helpful?
0 / 5 - 0 ratings