0

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

2 Answers 2

1

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
{
}
5
  • 1
    thanks for your update, but as per my requirement I want to add the argument on reference block section, is there has any way to do that?
    – senthil
    Commented Jan 23, 2019 at 14:46
  • Check updated answer
    – Sohel Rana
    Commented Jan 23, 2019 at 15:11
  • thanks for your support, but I am adding the same sample block also in catalog_category_view, catalogsearch_result_index, catalogsearch_advanced_result. so that the above procedure is not applicable in my case. if you have any other idea it'll much help me.
    – senthil
    Commented Jan 23, 2019 at 15:17
  • I tried as you mentioned way, it's working
    – senthil
    Commented Jan 24, 2019 at 5:36
  • Then how can I get the parameter values in block(sample.php) file instead of phtml file(sample.phtml)
    – senthil
    Commented Jan 24, 2019 at 5:58
0

The block sample.products is a child block for the product.info.addtocart parent block, so you can call the parent block in the child block (also in the .phtml file) using getParentBlock() method

In the phtml you can use the following syntax:

$block->getParentBlock()->getData('current_page')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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