{"id":5643,"date":"2018-08-11T14:29:07","date_gmt":"2018-08-11T14:29:07","guid":{"rendered":"https:\/\/blog.mageworx.com\/?p=5643"},"modified":"2022-05-20T12:28:19","modified_gmt":"2022-05-20T12:28:19","slug":"an-easy-way-to-remove-a-payment-method-in-magento-2","status":"publish","type":"post","link":"https:\/\/www.mageworx.com\/blog\/how-to-disable-payment-method-magento-2","title":{"rendered":"Easy Way to Remove Payment Method in Magento 2"},"content":{"rendered":"\n<!-- SEO Ultimate (http:\/\/www.seodesignsolutions.com\/wordpress-seo\/) - Code Inserter module -->\n<!-- Google Tag Manager (noscript) -->\r\n<noscript><iframe src=\"https:\/\/www.googletagmanager.com\/ns.html?id=GTM-5DTCW7B8\"\r\nheight=\"0\" width=\"0\" style=\"display:none;visibility:hidden\"><\/iframe><\/noscript>\r\n<!-- End Google Tag Manager (noscript) -->\n<!-- \/SEO Ultimate -->\n\n<span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\"> 3<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span><p>Quite often you may want to display a certain payment method to a particular group of customers while hiding it from the other ones.<\/p>\n<p>Here is a step-by-step instructdion on how to quickly implement that in Magento 2.<\/p>\n<p>The solution is based on the Payment Restriction module, the code of which is available below, where <strong>MageWorx<\/strong> is a vendor name and <strong>Payment Restriction<\/strong> is the name of the module. <!--more--><\/p>\n<h2>How to disable payment method in Magento 2?<\/h2>\n<p><strong>1. At the first step you need to create a directory for the module:<\/strong><\/p>\n<pre class=\"theme:github font:courier-new font-size:16 line-height:18 lang:default decode:true \">app\/code\/MageWorx\/PaymentRestriction<\/pre>\n<p><strong>2. When done, you may add a registration file.<\/strong><\/p>\n<pre class=\"theme:github font:courier-new font-size:16 line-height:18 lang:default decode:true \">app\/code\/MageWorx\/PaymentRestriction\/registration.php<\/pre>\n<pre class=\"theme:github font:courier-new font-size:16 line-height:18 lang:default decode:true \">&lt;?php\n\/**\n * Copyright \u00a9 2016 MageWorx. All rights reserved.\n * See LICENSE.txt for license details.\n *\/\n\n\\Magento\\Framework\\Component\\ComponentRegistrar::register(\n    \\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\n    'MageWorx_PaymentRestriction',\n    __DIR__\n);<\/pre>\n<p>the Composer file (just in case you may want to transfer the module with the ability to install it via Composer):<\/p>\n<pre class=\"theme:github font:courier-new font-size:16 line-height:18 lang:default decode:true \">app\/code\/MageWorx\/PaymentRestriction\/composer.json<\/pre>\n<pre class=\"theme:github font:courier-new font-size:16 line-height:18 lang:default decode:true\">{\n    \"name\": \"mageworx\/module-paymentrestriction\",\n    \"description\": \"N\/A\",\n    \"require\": {\n        \"php\": \"~5.6.0|~7.0.0\"\n    },\n    \"type\": \"magento2-module\",\n    \"version\": \"1.0.0\",\n    \"license\": [\n        \"OSL-3.0\",\n        \"AFL-3.0\"\n    ],\n    \"autoload\": {\n        \"files\": [\n            \"registration.php\"\n        ],\n        \"psr-4\": {\n            \"MageWorx\\\\PaymentRestriction\\\\\": \"\"\n        }\n    }\n}\n<\/pre>\n<p>and a file needed for the declaration of the module:<\/p>\n<pre class=\"theme:github font:courier-new font-size:16 line-height:18 lang:default decode:true \">app\/code\/MageWorx\/PaymentRestriction\/etc\/module.xml<\/pre>\n<pre class=\"theme:github font:courier-new font-size:16 line-height:18 lang:default decode:true \">&lt;?xml version=\"1.0\"?&gt;\n&lt;!--\n\/**\n * Copyright \u00a9 2016 MageWorx. All rights reserved.\n * See LICENSE.txt for license details.\n *\/\n--&gt;\n&lt;config xmlns:xsi=\"https:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\"&gt;\n    &lt;module name=\"MageWorx_PaymentRestriction\" setup_version=\"1.0.0\"&gt;\n    &lt;\/module&gt;\n&lt;\/config&gt;<\/pre>\n<p>At the next step, I&#8217;m going to create a <strong><code>di.xml<\/code> file<\/strong> in a sharing space (note if the file is declared in the frontend scope, you won&#8217;t get the desired result) and declare our plugin there:<\/p>\n<pre class=\"theme:github font:courier-new font-size:16 line-height:18 lang:default decode:true \">app\/code\/MageWorx\/PaymentRestriction\/etc\/di.xml<\/pre>\n<pre class=\"theme:github font:courier-new font-size:16 line-height:18 lang:default decode:true \">&lt;?xml version=\"1.0\"?&gt;\n&lt;!--\n\/**\n * Copyright \u00a9 2016 MageWorx. All rights reserved.\n * See LICENSE.txt for license details.\n *\/\n--&gt;\n&lt;config xmlns:xsi=\"https:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager\/etc\/config.xsd\"&gt;\n    &lt;type name=\"Magento\\OfflinePayments\\Model\\Cashondelivery\"&gt;\n        &lt;plugin sortOrder=\"1\" name=\"restrictByCustomer\"\n                type=\"MageWorx\\PaymentRestriction\\Plugin\\Payment\\Method\\CashOnDelivery\\Available\"\/&gt;\n    &lt;\/type&gt;\n&lt;\/config&gt;<\/pre>\n<p>Now, let&#8217;s add the last and the main file with the plugin class that is responsible for the check:<\/p>\n<pre class=\"theme:github font:courier-new font-size:16 line-height:18 lang:default decode:true \">app\/code\/MageWorx\/PaymentRestriction\/Plugin\/Payment\/Method\/CashOnDelivery\/Available.php<\/pre>\n<pre class=\"theme:github font:courier-new font-size:16 line-height:18 lang:default decode:true \">&lt;?php\n\/**\n * Copyright \u00a9 2016 MageWorx. All rights reserved.\n * See LICENSE.txt for license details.\n *\/\n\nnamespace MageWorx\\PaymentRestriction\\Plugin\\Payment\\Method\\CashOnDelivery;\n\nuse Magento\\Customer\\Model\\Session as CustomerSession;\nuse Magento\\Backend\\Model\\Auth\\Session as BackendSession;\nuse Magento\\OfflinePayments\\Model\\Cashondelivery;\n\nclass Available\n{\n\n    \/**\n     * @var CustomerSession\n     *\/\n    protected $customerSession;\n\n    \/**\n     * @var BackendSession\n     *\/\n    protected $backendSession;\n\n    \/**\n     * @param CustomerSession $customerSession\n     * @param BackendSession $backendSession\n     *\/\n    public function __construct(\n        CustomerSession $customerSession,\n        BackendSession $backendSession\n    ) {\n        $this-&gt;customerSession = $customerSession;\n        $this-&gt;backendSession = $backendSession;\n    }\n\n    \/**\n     *\n     * @param Cashondelivery $subject\n     * @param $result\n     * @return bool\n     * @throws \\Magento\\Framework\\Exception\\LocalizedException\n     *\/\n    public function afterIsAvailable(Cashondelivery $subject, $result)\n    {\n        \/\/ Do not remove payment method for admin\n        if ($this-&gt;backendSession-&gt;isLoggedIn()) {\n            return $result;\n        }\n\n        $isLogged = $this-&gt;customerSession-&gt;isLoggedIn();\n        if (!$isLogged) {\n            return false;\n        }\n\n        return $result;\n    }\n}<\/pre>\n<p><strong>IMPORTANT!\u00a0<\/strong><\/p>\n<p>Please note that for some reason, the plugin created only for the frontend won&#8217;t let you delete the chosen payment method &#8211; it will anyway be shown on the checkout page. However, if a customer tries to choose it, that will trigger an error and throw a customer back to the checkout step.<\/p>\n<p>That&#8217;s why to prevent this, it&#8217;s necessary to validate not only the customer&#8217;s session, but also the session of the admin. If you&#8217;re logged in as an admin, don&#8217;t change anything to get the original result.<\/p>\n<p>Also, you can add an extra check for the frontend. That can be done by developing a simple module that segments the available payment methods according to customer groups.<\/p>\n<p>Moreover, you can disable certain payment methods not only for the selected countries (the default option) but also for streets, phone numbers or any other location criteria.<\/p>\n<p><strong>Below is an example:<\/strong><\/p>\n<ol>\n<li>The Cash on <a href=\"https:\/\/www.mageworx.com\/delivery-date-magento-2.html\">Delivery method<\/a> is hidden for the not logged in customer:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-5647\" src=\"https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2016\/08\/1-1024x629.png\" alt=\"1\" width=\"768\" height=\"472\" srcset=\"https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2016\/08\/1-1024x629.png 1024w, https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2016\/08\/1-150x92.png 150w, https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2016\/08\/1-300x184.png 300w, https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2016\/08\/1-768x472.png 768w, https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2016\/08\/1.png 1393w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/li>\n<li>The same for the logged in customer:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-5646\" src=\"https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2016\/08\/IjcdJ-1024x616.png\" alt=\"IjcdJ\" width=\"768\" height=\"462\" srcset=\"https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2016\/08\/IjcdJ-1024x616.png 1024w, https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2016\/08\/IjcdJ-150x90.png 150w, https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2016\/08\/IjcdJ-300x180.png 300w, https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2016\/08\/IjcdJ-768x462.png 768w, https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2016\/08\/IjcdJ.png 1368w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/li>\n<li>Visible:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-5645\" src=\"https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2016\/08\/5H9o3-1024x630.png\" alt=\"5H9o3\" width=\"768\" height=\"473\" srcset=\"https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2016\/08\/5H9o3-1024x630.png 1024w, https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2016\/08\/5H9o3-150x92.png 150w, https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2016\/08\/5H9o3-300x185.png 300w, https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2016\/08\/5H9o3-768x473.png 768w, https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2016\/08\/5H9o3.png 1373w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/li>\n<li>Always visible for a store Admin:<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-5644\" src=\"https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2016\/08\/l6lgz-1024x471.png\" alt=\"l6lgz\" width=\"768\" height=\"353\" srcset=\"https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2016\/08\/l6lgz-1024x471.png 1024w, https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2016\/08\/l6lgz-150x69.png 150w, https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2016\/08\/l6lgz-300x138.png 300w, https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2016\/08\/l6lgz-768x353.png 768w, https:\/\/www.mageworx.com\/blog\/wp-content\/uploads\/2016\/08\/l6lgz.png 1895w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/p>\n<h4>Some Extra Recommendations<\/h4>\n<ol>\n<li>Implement the same activities in a separate module to get an ability to disable it.<\/li>\n<li>Change a <em>vendor<\/em> and a <em>module name<\/em> to your own.<\/li>\n<\/ol>\n<p style=\"text-align: center;\"><em>This solution was created and tested on Magento 2.1.<\/em><\/p>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p class=\"has-text-align-center\"> <em><strong>Still &#8216;ve got troubles removing a payment method in Magento 2? Feel free to leave a comment below.<\/strong><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\"> 3<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span>Quite often you may want to display a certain payment method to a particular group of customers while hiding it from the other ones. Here is a step-by-step instructdion on how to quickly implement that in Magento 2. The solution is based on the Payment Restriction module, the code of which is available below, where [&hellip;]<\/p>\n","protected":false},"author":15,"featured_media":10186,"comment_status":"open","ping_status":"open","sticky":true,"template":"","format":"standard","meta":{"footnotes":""},"categories":[255,425],"tags":[436],"class_list":{"0":"post-5643","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-magento-2","8":"category-magento-how-tos","9":"tag-developer-diaries"},"_links":{"self":[{"href":"https:\/\/www.mageworx.com\/blog\/wp-json\/wp\/v2\/posts\/5643","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mageworx.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mageworx.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mageworx.com\/blog\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mageworx.com\/blog\/wp-json\/wp\/v2\/comments?post=5643"}],"version-history":[{"count":14,"href":"https:\/\/www.mageworx.com\/blog\/wp-json\/wp\/v2\/posts\/5643\/revisions"}],"predecessor-version":[{"id":16092,"href":"https:\/\/www.mageworx.com\/blog\/wp-json\/wp\/v2\/posts\/5643\/revisions\/16092"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mageworx.com\/blog\/wp-json\/wp\/v2\/media\/10186"}],"wp:attachment":[{"href":"https:\/\/www.mageworx.com\/blog\/wp-json\/wp\/v2\/media?parent=5643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mageworx.com\/blog\/wp-json\/wp\/v2\/categories?post=5643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mageworx.com\/blog\/wp-json\/wp\/v2\/tags?post=5643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}