0

Button position decide by store owner(using admin panel). There are three positions.if user select test01 button's position others shouldn't be display.(Display only one place depend on user decision) enter image description here

Here is my xml code. `

<referenceBlock name="product.info.addtocart">
  <block class="Magento\Catalog\Block\Product\View" name="test1.tab" template="testbutton::catalog/product/view/toptoaddtocart.phtml?id=test1" group="main_info"></block>
</referenceBlock>

<referenceBlock name="product.info.main">
  <block class="Magento\Catalog\Block\Product\View" name="test2.tab" template="testbutton::catalog/product/view/toptoaddtocart.phtml?id=test2" group="main_info" after="product.info.addtocart"></block>
</referenceBlock>

` Previously I tried with 3 .Phtml pages but it effected to fail varnish cache test.Now I'm using one phtml file. Now cache issue is ok. but button display in three places.How can resolve this issue?

0

You need to create an admin configuration field for "Button position" with 3 values consisting of the name of the three blocks added in your xml file ('test1.tab', 'test2.tab'and the third one)

Then, your xml should look like:

<referenceBlock name="product.info.addtocart">
  <block class="Vendor\Module\Block\CustomButton" name="test1.tab" template="testbutton::catalog/product/view/toptoaddtocart.phtml" group="main_info"></block>
</referenceBlock>

<referenceBlock name="product.info.main">
  <block class="Vendor\Module\Block\CustomButton" name="test2.tab" template="testbutton::catalog/product/view/toptoaddtocart.phtml" group="main_info" after="product.info.addtocart"></block>
</referenceBlock>

As you can see, you need to create a custom block as follows. (i will assume that you need some kind of methods from Magento\Catalog\Block\Product\View and for that I will extend this class).

<?php

namespace Vendor\Module\Block;

class CustomButton extends \Magento\Catalog\Block\Product\View
{
    public function getButtonToDisplay()
    {
        return $this->_scopeConfig->getValue(
            'path_to/your/config_field',
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    }

    public function canShowButton()
    {
        return $this->getButtonToDisplay() == $this->getNameInLayout();
    }
}

And then, your phtml file should look like:

<?php if ($block->canShowButton()): ?>

your button goes here

<?php endif; ?>

If the field value will be changed in admin interface you'll have to clear cache in order to see the button in your new position.

  • Thank you @George M. Answer is well explained and it has worked for me. – Harsha Liyadipita May 14 at 4:38
  • Glad to hear that! :D – George M May 14 at 12:38

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.