Вопрос

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

Это было полезно?

Решение

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

Другие советы

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')
Лицензировано под: CC-BY-SA с атрибуция
Не связан с magento.stackexchange
scroll top