7

I need to understand some doubts facing in Magento-2. I need to display price(same as displayed in product description page) in my custom phtml file. I have a custom layout xml file which has declared some blocks and its associated templates, but after some line arguments are being passed in that. For example:

<block class="Magento\Wishlist\Block\Customer\Wishlist\Item\Column\Cart" name="customer.wishlist.item.price" template="item/column/price.phtml" cacheable="false">
                    <block class="Magento\Catalog\Pricing\Render" name="product.price.render.wishlist">
                        <arguments>
                            <argument name="price_render" xsi:type="string">product.price.render.default</argument>
                            <argument name="price_type_code" xsi:type="string">configured_price</argument>
                            <argument name="price_label" xsi:type="boolean">false</argument>
                            <argument name="zone" xsi:type="string">item_list</argument>
                        </arguments>
                    </block>
                    <block class="Magento\Wishlist\Block\Customer\Wishlist\Item\Options" name="customer.wishlist.item.options" cacheable="false"/>
                </block>

This code is from wishlist_index_index.xml file. Where are these arguments passed, what are these arguments and how this will work for displaying price in wishlist page? Take one more example:

<block class="Magento\Framework\Pricing\Render\RendererPool" name="render.product.prices">
    <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/default.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.phtml</item>
                </item>
                <item name="custom_option_price" xsi:type="array">
                    <item name="amount_render_template" xsi:type="string">Magento_Catalog::product/price/amount/default.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>

This code is from catalog_product_prices.xml file. Here, we pass items to arguments. What are these items in arguments and how is it going to work**?????**

3

You doesn't need to do passing value between layout block. You just keep below line to your view file and getting price same as details page.

 $priceBlock =  $block->getLayout()->createBlock('Magento\Catalog\Block\Product\ListProduct');

//getting $product object, passed $product object inside function
 echo $priceBlock->getProductPrice($product);

You have to display price same as details page and you have to apply some css for special price product to hide text of special price.

4
  • i got the price with this code. But i need to understand how these arguments values are internally working. Arguments are passed to constructor, after that i am bit confused. Apr 22 '16 at 9:26
  • we can directly call function which are used for rendering price in details page. So we have to set only product object inside function and we got price value. This is core function used for getting price. so we doesnt need to do any layout changes. Apr 22 '16 at 9:30
  • This code is not working for you? Apr 25 '16 at 11:33
  • this code is working for me Apr 25 '16 at 12:05
12

Arguments are injected in the block __construct method:

<?php
...
class YourBlock extends Template {
    ...
    public function __construct(
        Context $context,
        array $data = []
    ) {

        // Here, $data contains an hash with the passed arguments

        parent::__construct($context, $data);
    }
    ...
}
...

And according to DataObject, you should be able to accesss them from your template by using:

$block->getData('mykey');

And as you can understand their meaning depends on the block's logic.

But prices are not passed as arguments, what you need is probably need to add a price block or add a piece of code in your template files. I think XML layout will not help you in this.

2
  • with this I am clear but not full. I got to know about arguments and all with this reference devdocs.magento.com/guides/v2.0/frontend-dev-guide/layouts/…. and got the price in my custom page, but many things are not clear to me now also. Apr 22 '16 at 9:10
  • This is actually the right answer for the op, but could be better explained with a full example from layout xml to how it's used in the view.
    – Joel Davey
    Sep 24 '18 at 15:21

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.