{"id":1821,"date":"2021-03-19T08:04:26","date_gmt":"2021-03-19T02:04:26","guid":{"rendered":"http:\/\/promincproductions.com\/blog\/?p=1821"},"modified":"2021-09-13T13:59:35","modified_gmt":"2021-09-13T18:59:35","slug":"magento-2-set-payment-method-availability-for-frontend-vs-backend","status":"publish","type":"post","link":"https:\/\/promincproductions.com\/blog\/magento-2-set-payment-method-availability-for-frontend-vs-backend\/","title":{"rendered":"Magento 2 Set Payment Method Availability for Frontend (website) vs Backend (admin)"},"content":{"rendered":"<p>Magento admin configuration doesn&#8217;t allow you to control if a payment method is available in just the frontend or backend (admin).<\/p>\n\n\n\n<p>Thankfully they did offer some pre-built methods for configuring this via the codebase.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2021\/03\/Magento-2-Set-Payment-Method-Availability.png\" alt=\"\" class=\"wp-image-1822\" width=\"500\" height=\"500\" srcset=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2021\/03\/Magento-2-Set-Payment-Method-Availability.png 1000w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2021\/03\/Magento-2-Set-Payment-Method-Availability-500x500.png 500w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2021\/03\/Magento-2-Set-Payment-Method-Availability-150x150.png 150w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2021\/03\/Magento-2-Set-Payment-Method-Availability-768x768.png 768w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2021\/03\/Magento-2-Set-Payment-Method-Availability-600x600.png 600w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2021\/03\/Magento-2-Set-Payment-Method-Availability-450x450.png 450w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><figcaption>Control payment methods in Magento to &#8211; enabling and disabling for frontend or backend as needed.<\/figcaption><\/figure><\/div>\n\n\n\n<p>If you take a look at <code>vendor\/magento\/module-payment\/Model\/Method\/AbstractMethod.php<\/code> you&#8217;ll see there are several protected methods that can be set in a payment method model.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grep 'protected $_can' vendor\/magento\/module-payment\/Model\/Method\/AbstractMethod.php\n    protected $_canOrder = false;\n    protected $_canAuthorize = false;\n    protected $_canCapture = false;\n    protected $_canCapturePartial = false;\n    protected $_canCaptureOnce = false;\n    protected $_canRefund = false;\n    protected $_canRefundInvoicePartial = false;\n    protected $_canVoid = false;\n    protected $_canUseInternal = true;\n    protected $_canUseCheckout = true;\n    protected $_canFetchTransactionInfo = false;\n    protected $_canReviewPayment = false;\n    protected $_canCancelInvoice = false;\n<\/code><\/pre>\n\n\n\n<p>The two of these that are relevant to the topic at hand are <code>$_canUseInternal<\/code> and <code>$_canUseCheckout<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">$_canUseInternal &#8211; Enable\/Disable for Backend (admin)<\/h2>\n\n\n\n<p>This method specifies if the payment method is available in the Magento Admin or not.<\/p>\n\n\n\n<p>A good example of how this is used is PayPal.  The PayPal payment method is ONLY available for the customer to use on the frontend &#8211; not when placing an order in the Magento admin.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    \/**\n     * Using internal pages for input payment data.\n     *\n     * Can be used in admin.\n     *\n     * @return bool\n     * @deprecated 100.2.0\n     *\/\n    public function canUseInternal()\n    {\n        return $this-&gt;_canUseInternal;\n    }\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">$_canUseCheckout  &#8211; Enable\/Disable for Frontend (website) <\/h2>\n\n\n\n<p>This method specifies if the payment method is available in the public facing frontend or not.<\/p>\n\n\n\n<p>One example here is to accept the Check \/ Money Order payment method is accessible only in the Magento admin but not for public customers to use.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    \/**\n     * Can be used in regular checkout\n     *\n     * @return bool\n     * @deprecated 100.2.0\n     *\/\n    public function canUseCheckout()\n    {\n        return $this-&gt;_canUseCheckout;\n    }\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example Module to Disable Check \/ Money Order (checkmo) on Frontend (available to Admin only)<\/h2>\n\n\n\n<p>Here is a quick example module to set the Magento default Check \/ Money Order (checkmo) payment method to be used in the admin only.  This allows for accepting payments via check, money order, or a virtual terminal with a payment gateway but not giving this option to your customers on the frontend.<\/p>\n\n\n\n<p>First define your module.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>## app\/code\/Namespace\/MagentoOfflinePayments\/registration.php\n\n&lt;?php\n\\Magento\\Framework\\Component\\ComponentRegistrar::register(\n    \\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\n    'Namespace_MagentoOfflinePayments',\n    __DIR__\n);<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>## app\/code\/Namespace\/MagentoOfflinePayments\/etc\/module.xml\n\n&lt;?xml version=\"1.0\"?&gt;\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\"&gt;\n    &lt;module name=\"Namespace_MagentoOfflinePayments\"&gt;\n        &lt;sequence&gt;\n            &lt;module name=\"Magento_OfflinePayments\"\/&gt;\n        &lt;\/sequence&gt;\n    &lt;\/module&gt;\n&lt;\/config&gt;\n<\/code><\/pre>\n\n\n\n<p>Then extend the payment model, setting the <code>$_canUseCheckout<\/code> method to false.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>## app\/code\/Namespace\/MagentoOfflinePayments\/etc\/di.xml\n\n&lt;?xml version=\"1.0\" ?&gt;\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager\/etc\/config.xsd\"&gt;\n    &lt;preference for=\"Magento\\OfflinePayments\\Model\\Checkmo\" type=\"Namespace\\MagentoOfflinePayments\\Model\\Checkmo\" \/&gt;\n&lt;\/config&gt;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>## app\/code\/Namespace\/MagentoOfflinePayments\/Model\/Checkmo.php\n\n&lt;?php\n\nnamespace Namespace\\MagentoOfflinePayments\\Model;\n\nclass Checkmo extends \\Magento\\OfflinePayments\\Model\\Checkmo\n{\n\n    \/**\n     * Availability on frontend checkout\n     *\n     * @var bool\n     *\/\n    protected $_canUseCheckout = false;\n\n}\n<\/code><\/pre>\n\n\n\n<p>As a bonus touch, add a note in the admin on the Check \/ Money Order settings section that this change has been made.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>## app\/code\/Namespace\/MagentoOfflinePayments\/etc\/adminhtml\/system.xml\n\n&lt;?xml version=\"1.0\"?&gt;\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:module:Magento_Config:etc\/system_file.xsd\"&gt;\n    &lt;system&gt;\n        &lt;section id=\"payment\"&gt;\n            &lt;group id=\"checkmo\"&gt;\n                &lt;field id=\"note_disabled\" translate=\"label\" type=\"label\" sortOrder=\"0\" showInDefault=\"1\" showInWebsite=\"1\" showInStore=\"1\"&gt;\n                    &lt;label&gt;NOTE:&lt;\/label&gt;\n                    &lt;comment&gt;&lt;!&#091;CDATA&#091;&lt;span style=\"background-color: #fbdccc;color: #e22626;font-weight: bold;padding: 5px;display: inline-block;\"&gt;This payment method is only available to the Magento admin via a code modification.&lt;\/span&gt;]]&gt;&lt;\/comment&gt;\n                &lt;\/field&gt;\n            &lt;\/group&gt;\n        &lt;\/section&gt;\n    &lt;\/system&gt;\n&lt;\/config&gt;<\/code><\/pre>\n\n\n\n<p>Lastly, you just need to tell Magneto to install this module.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php bin\/magento setup:upgrade &amp;&amp; php bin\/magento setup:di:compile &amp;&amp; php bin\/magento cache:flush<\/code><\/pre>\n\n\n\n<p>And that&#8217;s it.  Now the Check \/ Money Order (checkmo) payment method will no longer be available on the frontend but will work without issue in the admin.<\/p>","protected":false},"excerpt":{"rendered":"<p>Magento admin configuration doesn&#8217;t allow you to control if a payment method is available in just the frontend [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1822,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wprm-recipe-roundup-name":"","wprm-recipe-roundup-description":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[46,5],"tags":[],"class_list":["post-1821","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-magento","category-website-development"],"jetpack_featured_media_url":"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2021\/03\/Magento-2-Set-Payment-Method-Availability.png","jetpack_shortlink":"https:\/\/wp.me\/p4BbcR-tn","jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/1821","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/comments?post=1821"}],"version-history":[{"count":3,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/1821\/revisions"}],"predecessor-version":[{"id":1948,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/1821\/revisions\/1948"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/media\/1822"}],"wp:attachment":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/media?parent=1821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/categories?post=1821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/tags?post=1821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}