4

How to hide PayPal Express Checkout in code at the following location:

vendor/magento/module-checkout/view/frontend/templates/onepage.phtml

<!-- ko template: getTemplate() --><!-- /ko -->

preview

How to debug this ko.?

Case:

Express checkout is enabled at admin. Example:

  • product sku: commission-product
  • if commission-product is added to cart: payment through Express checkout
  • else: show all payment methods expect Express checkout
  • Are you want to remove paypal express checkout payment method? – Rakesh Jesadiya Jun 23 '17 at 6:23
  • yes but with code prntscr.com/fn5bf7 – Raghu Jun 23 '17 at 6:24
  • you can turn off from admin , else hide via css – Manoj Deswal Jun 23 '17 at 6:49
  • yes but i want to hide by code. – Raghu Jun 23 '17 at 6:58
  • this has no specific class to display none prntscr.com/fn5uhl or if we hide by css nth child, if any other payment added issue will be there – Raghu Jun 23 '17 at 7:22
1

You can write a plugin:

Vendor/Module/etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <!-- Plugins -->
    <type name="Magento\Paypal\Model\Express">
        <plugin name="disable_paypal_express"
                type="Vendor\Module\Plugin\DisablePayPalExpress"
                sortOrder="10"
                disabled="false"/>
    </type>
</config>

Vendor/Module/Plugin/DisablePayPalExpress.php

<?php
namespace Vendor\Module\Plugin;

class DisablePayPalExpress
{
    /**
     * @var \Magento\Checkout\Model\Session|\Magento\Backend\Model\Session\Quote
     */
    protected $session;

    /** List of the skus for which this method should be disabled */
    protected $disabledForSku = [
        'commission-product',
        'another-sku',
        'Gloves'
    ];

    /**
     * @param \Magento\Checkout\Model\Session $checkoutSession
     * @param \Magento\Backend\Model\Session\Quote $backendQuoteSession
     * @param \Magento\Framework\App\State $state
     */
    public function __construct(
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Backend\Model\Session\Quote $backendQuoteSession,
        \Magento\Framework\App\State $state
    ) {
        if ($state->getAreaCode() == \Magento\Framework\App\Area::AREA_ADMINHTML) {
            $this->session = $backendQuoteSession;
        } else {
            $this->session = $checkoutSession;
        }
    }

    /**
     * @param \Magento\Paypal\Model\Express $subject
     * @param $result
     * @return bool
     */
    public function afterIsAvailable(
        \Magento\Paypal\Model\Express $subject,
        $result
    ) {
        /** @var \Magento\Quote\Model\Quote $quote */
        $quote = $this->session->getQuote();
        /** @var \Magento\Quote\Model\Quote\Item[] $quoteItems */
        $quoteItems = $quote->getAllItems();
        foreach ($quoteItems as $quoteItem) {
            if (in_array($quoteItem->getSku(), $this->disabledForSku)) {
                return false; // disable the method if we found product with specified sku
            }
        }

        return $result; // return default result
    }
}

If you return false, method should be always disabled. If you need a custom validation, just add it in the methods body and use a desired result.

  • @EwallTester I've updated my answer based on your comments to the question (disable method if specific sku is found in the cart) – Siarhey Uchukhlebau Jun 23 '17 at 8:00
  • @EwallTester I'm glad to help you :) – Siarhey Uchukhlebau Jun 23 '17 at 9:36
  • hi I have a issue here Siarhey – Raghu Jun 30 '17 at 5:27
  • @EwallTester which issue? – Siarhey Uchukhlebau Jun 30 '17 at 7:05
  • From the code, For the first time if the product sku: commission-product is added or not Express checkout should be disabled, and if commission-product is in cart only Express checkout should be shown and all other should be disabled – Raghu Jun 30 '17 at 7:07

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