0

I need to develop a feature in my custom module, I would like to hide Amazon pay button from product page, cart page, mini cart, shipping page upon condition.

If the current product is in the cart then hide Amazon pay button from the product page, cart page, mini cart, shipping page.

How can I achieve these things?. can someone help me to achieve these things.

Any help would be appreciated! Thanks.

1

There is no need to create preference. You can create plugin for "isPwaButtonVisibleOnProductPage" instead of "isPaymentButtonEnabled" so that you will not need to create preference.

  • Update my question. Thanks for your time – Chirag Patel Jul 18 at 12:22
0

I do it my self!

Actualy there is a setting for display amazon button on product page, min cart, cart page, shipping method page and this all function manage by Amazon\Core\Helper\Data class function called public function isPaymentButtonEnabled(). and Amazon\Payment\Block\Minicart\Button class function called protected function _toHtml().

So create plugin for public function isPaymentButtonEnabled() and create prefrance for protected function _toHtml(). My code will look like this.

app/code/Namespace/ModuleName/etc/di.xml

<type name="Amazon\Core\Helper\Data">
    <plugin name="hide_amzn_buttom_product_page_plugin" type="Namespace\ModuleName\Plugin\ProductPageAmznButton" sortOrder="10" />
</type>
<preference for="Amazon\Payment\Block\Minicart\Button" type="Namespace\ModuleName\Block\Minicart\CartPageAmznButton"/>

app/code/Namespace/ModuleName/Plugin/ProductPageAmznButton.php

<?php
namespace Namespace\ModuleName\Plugin;

use Magento\Store\Model\ScopeInterface;
use Namespace\ModuleName\Helper\Data;

class ProductPageAmznButton
{
    /**
     * @var Data
     */
    private $helper;

    /**
     * Constructor
     *
     * @param Data $helper
     */
    public function __construct(
        Data $helper
    ) {
        $this->helper = $helper;
    }

    /**
     * @param $subject
     * @param $result
     * @param string $scope
     * @param null $scopeCode
     * @return bool
     */
    public function afterIsPaymentButtonEnabled($subject, $result, $scope = ScopeInterface::SCOPE_STORE, $scopeCode = null)
    {
        if($this->helper->isModuleEnable()){
            if($this->helper->hasProductIncart()){
                return false;
            }

        }
        return $result;
    }
}

app/code/Namespace/ModuleName/Block/Minicart/CartPageAmznButton.php

<?php
namespace Namespace\ModuleName\Block\Minicart;

use Amazon\Core\Helper\Data as AmazonCoreHelper;
use Amazon\Payment\Gateway\Config\Config;
use Amazon\Payment\Helper\Data;
use Namespace\ModuleName\Helper\Data as ModuleHelper;
use Magento\Checkout\Model\Session;
use Magento\Framework\App\Request\Http;
use Magento\Framework\Locale\ResolverInterface;
use Magento\Framework\View\Element\Template\Context;

class CartPageAmznButton extends \Amazon\Payment\Block\Minicart\Button
{
    protected $moduleHelper;

    public function __construct(
        Context $context,
        ResolverInterface $localeResolver,
        Data $mainHelper,
        Session $session,
        Config $payment,
        AmazonCoreHelper $coreHelper,
        Http $request,
        ModuleHelper $moduleHelper,
        array $data = []
    ) {
        $this->moduleHelper = $moduleHelper;
        parent::__construct($context, $localeResolver, $mainHelper, $session, $payment, $coreHelper, $request, $data);
    }

    /**
     * @return bool
     */
    protected function _toHtml()
    {
        if($this->moduleHelper->isModuleEnable()){
            if($this->moduleHelper->hasProductInCart()){
                return '';
            }

        }
        if (!$this->shouldRender()) {
            return '';
        }

        return parent::_toHtml();
    }
}

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.