Magento 2 – Add New Argument in Reference Block and Get in Phtml

blocksmagento2page-layoutsreference

I am working on reference block concept, in that I need to add new arguments and get the argument value on the .phmlt file.

in catalog_product_view.xml I've added the below line of code.

 <referenceBlock name="product.info.addtocart">
        <arguments>
            <argument name="current_page" xsi:type="string">detail</argument>
            <argument name="all_page" xsi:type="string">all</argument>
         </arguments> 
         <!-- <action method="setData">
                 <name>current_page</name>
                 <value>detail</value>
                 <name>all_page</name>
                 <value>all</value>
        </action> -->

       <block class="Learn\Test\Block\Sample" name="sample.otherproducts" as="sample.products" after="-" template="catalog/product/view/sample.phtml" />
</referenceBlock>

Then in my sample.phtml file I am using the following lines for getting the value of the arguments

$this->getData('current_page');
$this->getCurrentPage();

But nothing is get the value of the current_page arugment, what is the exact procedure to bind the value on reference block layout and get it into the .phtml file

Kindly share your thoughts and ideas, thanks in advance for your support

Best Answer

Add inside sample block:


<block class="Learn\Test\Block\Sample" name="sample.otherproducts" as="sample.products" after="-" template="catalog/product/view/sample.phtml">
    <arguments>
        <argument name="current_page" xsi:type="string">detail</argument>
        <argument name="all_page" xsi:type="string">all</argument>
    </arguments>
</block>

Now try following way:


$block->getData('current_page');
$block->getCurrentPage();

[Update]

Extend your block by Magento\Catalog\Block\Product\View class. For example:


class Learn\Test\Block\Sample extends Magento\Catalog\Block\Product\View
{
}
Related Topic