Payment method only admin Magento 2?












2














When working with Magento 2, sometimes we want to disable Payment methods on the Front page and use it only for Admin. Or, if we want to set more rules for Payment methods to check whether payment method can be used. How we can do that?










share|improve this question
























  • you have declared sortOrder two times in di.xml.could you please remove it.
    – Renu Mishra
    1 hour ago










  • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
    – Teja Bhagavan Kollepara
    55 mins ago
















2














When working with Magento 2, sometimes we want to disable Payment methods on the Front page and use it only for Admin. Or, if we want to set more rules for Payment methods to check whether payment method can be used. How we can do that?










share|improve this question
























  • you have declared sortOrder two times in di.xml.could you please remove it.
    – Renu Mishra
    1 hour ago










  • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
    – Teja Bhagavan Kollepara
    55 mins ago














2












2








2


3





When working with Magento 2, sometimes we want to disable Payment methods on the Front page and use it only for Admin. Or, if we want to set more rules for Payment methods to check whether payment method can be used. How we can do that?










share|improve this question















When working with Magento 2, sometimes we want to disable Payment methods on the Front page and use it only for Admin. Or, if we want to set more rules for Payment methods to check whether payment method can be used. How we can do that?







magento2 overrides payment-methods payment-gateway






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 15 '16 at 11:41

























asked Jun 15 '16 at 11:31









Khoa TruongDinh

20.9k63884




20.9k63884












  • you have declared sortOrder two times in di.xml.could you please remove it.
    – Renu Mishra
    1 hour ago










  • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
    – Teja Bhagavan Kollepara
    55 mins ago


















  • you have declared sortOrder two times in di.xml.could you please remove it.
    – Renu Mishra
    1 hour ago










  • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
    – Teja Bhagavan Kollepara
    55 mins ago
















you have declared sortOrder two times in di.xml.could you please remove it.
– Renu Mishra
1 hour ago




you have declared sortOrder two times in di.xml.could you please remove it.
– Renu Mishra
1 hour ago












This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– Teja Bhagavan Kollepara
55 mins ago




This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– Teja Bhagavan Kollepara
55 mins ago










1 Answer
1






active

oldest

votes


















1














We should take a look at the payment abstract class vendor/magento/module-payment/Model/Method/AbstractMethod.php. We can see the method public function isAvailable() - This class will check whether payment method can be used.



   public function isAvailable(MagentoQuoteApiDataCartInterface $quote = null)
{
if (!$this->isActive($quote ? $quote->getStoreId() : null)) {
return false;
}

$checkResult = new DataObject();
$checkResult->setData('is_available', true);

// for future use in observers
$this->_eventManager->dispatch(
'payment_method_is_active',
[
'result' => $checkResult,
'method_instance' => $this,
'quote' => $quote
]
);

return $checkResult->getData('is_available');
}


As can we see, there is an Observer event, like Magento 1, we can use Observer to disable our Payment Methods. It's Magento 1 way. However, Magento 2 introduces to developers a new way - Plugin.



Create DI on front page:
app/code/{Vendor}/{Module Name}/etc/frontend/di.xml



<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoPaymentModelMethodAbstractMethod">
<plugin sortOrder="5" name="testValidate"
type="{Vendor}{Module Name}PluginModelMethodAvailable" sortOrder="1" disabled="false" />
</type>
</config>


app/code/{Vendor}/{Module Name}/Plugin/Model/Method/Available.php



<?php

namespace {Vender}{Module Name}PluginModelMethod;

class Available
{
/**
*
* @param MagentoPaymentModelMethodAbstractMethod $subject
* @param $result
* @return bool
* @throws MagentoFrameworkExceptionLocalizedException
*/
public function afterIsAvailable(MagentoPaymentModelMethodAbstractMethod $subject, $result)
{

if($subject->getCode() == 'your payment code, eg: cashondelivery') {
return false;
}
return $result;
}
}


$subject is the current Payment Method Model.



enter image description here






share|improve this answer



















  • 1




    its not working for me
    – Er Sarvesh V Tiwari
    May 30 '17 at 7:03










  • For reference, I found this answer helpful magento.stackexchange.com/a/189530/41603
    – tim.baker
    Jul 3 '18 at 10:14













Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f121019%2fpayment-method-only-admin-magento-2%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














We should take a look at the payment abstract class vendor/magento/module-payment/Model/Method/AbstractMethod.php. We can see the method public function isAvailable() - This class will check whether payment method can be used.



   public function isAvailable(MagentoQuoteApiDataCartInterface $quote = null)
{
if (!$this->isActive($quote ? $quote->getStoreId() : null)) {
return false;
}

$checkResult = new DataObject();
$checkResult->setData('is_available', true);

// for future use in observers
$this->_eventManager->dispatch(
'payment_method_is_active',
[
'result' => $checkResult,
'method_instance' => $this,
'quote' => $quote
]
);

return $checkResult->getData('is_available');
}


As can we see, there is an Observer event, like Magento 1, we can use Observer to disable our Payment Methods. It's Magento 1 way. However, Magento 2 introduces to developers a new way - Plugin.



Create DI on front page:
app/code/{Vendor}/{Module Name}/etc/frontend/di.xml



<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoPaymentModelMethodAbstractMethod">
<plugin sortOrder="5" name="testValidate"
type="{Vendor}{Module Name}PluginModelMethodAvailable" sortOrder="1" disabled="false" />
</type>
</config>


app/code/{Vendor}/{Module Name}/Plugin/Model/Method/Available.php



<?php

namespace {Vender}{Module Name}PluginModelMethod;

class Available
{
/**
*
* @param MagentoPaymentModelMethodAbstractMethod $subject
* @param $result
* @return bool
* @throws MagentoFrameworkExceptionLocalizedException
*/
public function afterIsAvailable(MagentoPaymentModelMethodAbstractMethod $subject, $result)
{

if($subject->getCode() == 'your payment code, eg: cashondelivery') {
return false;
}
return $result;
}
}


$subject is the current Payment Method Model.



enter image description here






share|improve this answer



















  • 1




    its not working for me
    – Er Sarvesh V Tiwari
    May 30 '17 at 7:03










  • For reference, I found this answer helpful magento.stackexchange.com/a/189530/41603
    – tim.baker
    Jul 3 '18 at 10:14


















1














We should take a look at the payment abstract class vendor/magento/module-payment/Model/Method/AbstractMethod.php. We can see the method public function isAvailable() - This class will check whether payment method can be used.



   public function isAvailable(MagentoQuoteApiDataCartInterface $quote = null)
{
if (!$this->isActive($quote ? $quote->getStoreId() : null)) {
return false;
}

$checkResult = new DataObject();
$checkResult->setData('is_available', true);

// for future use in observers
$this->_eventManager->dispatch(
'payment_method_is_active',
[
'result' => $checkResult,
'method_instance' => $this,
'quote' => $quote
]
);

return $checkResult->getData('is_available');
}


As can we see, there is an Observer event, like Magento 1, we can use Observer to disable our Payment Methods. It's Magento 1 way. However, Magento 2 introduces to developers a new way - Plugin.



Create DI on front page:
app/code/{Vendor}/{Module Name}/etc/frontend/di.xml



<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoPaymentModelMethodAbstractMethod">
<plugin sortOrder="5" name="testValidate"
type="{Vendor}{Module Name}PluginModelMethodAvailable" sortOrder="1" disabled="false" />
</type>
</config>


app/code/{Vendor}/{Module Name}/Plugin/Model/Method/Available.php



<?php

namespace {Vender}{Module Name}PluginModelMethod;

class Available
{
/**
*
* @param MagentoPaymentModelMethodAbstractMethod $subject
* @param $result
* @return bool
* @throws MagentoFrameworkExceptionLocalizedException
*/
public function afterIsAvailable(MagentoPaymentModelMethodAbstractMethod $subject, $result)
{

if($subject->getCode() == 'your payment code, eg: cashondelivery') {
return false;
}
return $result;
}
}


$subject is the current Payment Method Model.



enter image description here






share|improve this answer



















  • 1




    its not working for me
    – Er Sarvesh V Tiwari
    May 30 '17 at 7:03










  • For reference, I found this answer helpful magento.stackexchange.com/a/189530/41603
    – tim.baker
    Jul 3 '18 at 10:14
















1












1








1






We should take a look at the payment abstract class vendor/magento/module-payment/Model/Method/AbstractMethod.php. We can see the method public function isAvailable() - This class will check whether payment method can be used.



   public function isAvailable(MagentoQuoteApiDataCartInterface $quote = null)
{
if (!$this->isActive($quote ? $quote->getStoreId() : null)) {
return false;
}

$checkResult = new DataObject();
$checkResult->setData('is_available', true);

// for future use in observers
$this->_eventManager->dispatch(
'payment_method_is_active',
[
'result' => $checkResult,
'method_instance' => $this,
'quote' => $quote
]
);

return $checkResult->getData('is_available');
}


As can we see, there is an Observer event, like Magento 1, we can use Observer to disable our Payment Methods. It's Magento 1 way. However, Magento 2 introduces to developers a new way - Plugin.



Create DI on front page:
app/code/{Vendor}/{Module Name}/etc/frontend/di.xml



<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoPaymentModelMethodAbstractMethod">
<plugin sortOrder="5" name="testValidate"
type="{Vendor}{Module Name}PluginModelMethodAvailable" sortOrder="1" disabled="false" />
</type>
</config>


app/code/{Vendor}/{Module Name}/Plugin/Model/Method/Available.php



<?php

namespace {Vender}{Module Name}PluginModelMethod;

class Available
{
/**
*
* @param MagentoPaymentModelMethodAbstractMethod $subject
* @param $result
* @return bool
* @throws MagentoFrameworkExceptionLocalizedException
*/
public function afterIsAvailable(MagentoPaymentModelMethodAbstractMethod $subject, $result)
{

if($subject->getCode() == 'your payment code, eg: cashondelivery') {
return false;
}
return $result;
}
}


$subject is the current Payment Method Model.



enter image description here






share|improve this answer














We should take a look at the payment abstract class vendor/magento/module-payment/Model/Method/AbstractMethod.php. We can see the method public function isAvailable() - This class will check whether payment method can be used.



   public function isAvailable(MagentoQuoteApiDataCartInterface $quote = null)
{
if (!$this->isActive($quote ? $quote->getStoreId() : null)) {
return false;
}

$checkResult = new DataObject();
$checkResult->setData('is_available', true);

// for future use in observers
$this->_eventManager->dispatch(
'payment_method_is_active',
[
'result' => $checkResult,
'method_instance' => $this,
'quote' => $quote
]
);

return $checkResult->getData('is_available');
}


As can we see, there is an Observer event, like Magento 1, we can use Observer to disable our Payment Methods. It's Magento 1 way. However, Magento 2 introduces to developers a new way - Plugin.



Create DI on front page:
app/code/{Vendor}/{Module Name}/etc/frontend/di.xml



<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoPaymentModelMethodAbstractMethod">
<plugin sortOrder="5" name="testValidate"
type="{Vendor}{Module Name}PluginModelMethodAvailable" sortOrder="1" disabled="false" />
</type>
</config>


app/code/{Vendor}/{Module Name}/Plugin/Model/Method/Available.php



<?php

namespace {Vender}{Module Name}PluginModelMethod;

class Available
{
/**
*
* @param MagentoPaymentModelMethodAbstractMethod $subject
* @param $result
* @return bool
* @throws MagentoFrameworkExceptionLocalizedException
*/
public function afterIsAvailable(MagentoPaymentModelMethodAbstractMethod $subject, $result)
{

if($subject->getCode() == 'your payment code, eg: cashondelivery') {
return false;
}
return $result;
}
}


$subject is the current Payment Method Model.



enter image description here







share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 6 '16 at 6:56

























answered Jun 15 '16 at 11:31









Khoa TruongDinh

20.9k63884




20.9k63884








  • 1




    its not working for me
    – Er Sarvesh V Tiwari
    May 30 '17 at 7:03










  • For reference, I found this answer helpful magento.stackexchange.com/a/189530/41603
    – tim.baker
    Jul 3 '18 at 10:14
















  • 1




    its not working for me
    – Er Sarvesh V Tiwari
    May 30 '17 at 7:03










  • For reference, I found this answer helpful magento.stackexchange.com/a/189530/41603
    – tim.baker
    Jul 3 '18 at 10:14










1




1




its not working for me
– Er Sarvesh V Tiwari
May 30 '17 at 7:03




its not working for me
– Er Sarvesh V Tiwari
May 30 '17 at 7:03












For reference, I found this answer helpful magento.stackexchange.com/a/189530/41603
– tim.baker
Jul 3 '18 at 10:14






For reference, I found this answer helpful magento.stackexchange.com/a/189530/41603
– tim.baker
Jul 3 '18 at 10:14




















draft saved

draft discarded




















































Thanks for contributing an answer to Magento Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f121019%2fpayment-method-only-admin-magento-2%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown