0

I want to change the template Magento_Catalog::product/price/amount/default.phtml but only for Crosssell Related and Upsell. Im starting here with related and have tried like below:

Step 1

Block for related I extended class and added below code for priceHtml

public function getProductPriceHtml(
    \Magento\Catalog\Model\Product $product,
    $priceType,
    $renderZone = \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST,
    array $arguments = []
) {
    if (!isset($arguments['zone'])) {
        $arguments['zone'] = $renderZone;
    }

    /** @var \Magento\Framework\Pricing\Render $priceRender */
    $priceRender = $this->getLayout()->getBlock('product.price.render.itemscustom');
    $price = '';

    if ($priceRender) {
        $price = $priceRender->render($priceType, $product, $arguments);
    }
    return $price;
}

Changed just the $priceRender to a new block which I use to change layout settings via xml. Custom block:

product.price.render.itemscustom

Step 2

Then added below block with this new name to my XML. Uses the same class just sets a new layout handle to modify the default template paths in step 3:

<block class="Magento\Framework\Pricing\Render" name="product.price.render.itemscustom">
  <arguments>
    <argument name="price_render_handle" xsi:type="string">items_catalog_product_prices</argument>
    <argument name="use_link_for_as_low_as" xsi:type="boolean">false</argument>
</arguments>
</block>

app/design/frontend/Vendor/Theme/Magento_Catalog/layout/catalog_product_view.xml

Step 3

And then added the layout to point to my custom templates items.phtml based on the above handle:

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
    <block class="Magento\Framework\Pricing\Render\RendererPool" name="product.price.render.itemscustom.render">
        <arguments>
            <argument name="default" xsi:type="array">
                <item name="default_render_class" xsi:type="string">Magento\Catalog\Pricing\Render\PriceBox</item>
                <item name="default_render_template" xsi:type="string">Magento_Catalog::product/price/default.phtml</item>
                <item name="default_amount_render_class" xsi:type="string">Magento\Framework\Pricing\Render\Amount</item>
                <item name="default_amount_render_template" xsi:type="string">Magento_Catalog::product/price/amount/items.phtml</item>
                <item name="prices" xsi:type="array">
                    <item name="special_price" xsi:type="array">
                        <item name="render_template" xsi:type="string">Magento_Catalog::product/price/special_price.phtml</item>
                    </item>
                    <item name="tier_price" xsi:type="array">
                        <item name="render_template" xsi:type="string">Magento_Catalog::product/price/tier_prices.phtml</item>
                    </item>
                    <item name="final_price" xsi:type="array">
                        <item name="render_class" xsi:type="string">Magento\Catalog\Pricing\Render\FinalPriceBox</item>
                        <item name="render_template" xsi:type="string">Magento_Catalog::product/price/final_price_items.phtml</item>
                    </item>
                    <item name="custom_option_price" xsi:type="array">
                        <item name="amount_render_template" xsi:type="string">Magento_Catalog::product/price/amount/items.phtml</item>
                    </item>
                    <item name="configured_price" xsi:type="array">
                        <item name="render_class" xsi:type="string">Magento\Catalog\Pricing\Render\ConfiguredPriceBox</item>
                        <item name="render_template" xsi:type="string">Magento_Catalog::product/price/configured_price.phtml</item>
                    </item>
                </item>
                <!--<item name="adjustments" xsi:type="array"></item>-->
            </argument>
        </arguments>
    </block>
</layout>

app/design/frontend/Vendor/Theme/Magento_Catalog/layout/items_catalog_product_prices.xml

Above renders the price however the template remains the same.

Changing the block in step 1 to anything other than the new block I just made (or the default) does not render price so it seems to be using my function in step one and seeing my block in step 2. Just the layouts do not seem to use the ones defined in the layout in step 3.

Wondering what I have done wrong or how else st to go about this?

Update

The above looks like it will not just work and was just a bit of a guess and hope based on other answers on here. The RendererPool.php just selects the template bassed on $type and $priceCode like below:

$amountBlock->setTemplate($this->getAmountRenderBlockTemplate($type, $priceCode));

Feel I need to change these and make sure my new templates will be matched by this however not sure how to add new templates so they will match. The below functions are called in order to get the templates so starting here to try and figure it out:

protected function getAmountRenderBlockTemplate($type, $priceCode)
{
    $pattern = [
        $type . '/prices/' . $priceCode . '/amount_render_template',
        $type . '/default_amount_render_template',
        'default/prices/' . $priceCode . '/amount_render_template',
        'default/default_amount_render_template',
    ];
    $template = $this->findDataByPattern($pattern);

    if (!$template) {
        throw new \InvalidArgumentException(
            'For type "' . $type . '" amount render block not configured'
        );
    }
    return $template;
}

protected function findDataByPattern(array $pattern)
{
    $data = null;
    foreach ($pattern as $key) {
        $data = $this->getData($key);
        if ($data) {
            break;
        }
    }
    return $data;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.