Login as admin, then go to System->configuration, then select ‘Sales‘ from left Nav and click on ‘Minimum order amount‘.
Then select Yes from Enable dropdown, enter Minimum order amount, enter message and also enter error message that will be shown whenever the order will be less than specified amount at the shopping cart.
Thanks.
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(\Magento\Quote\Api\Data\CartInterface $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="Magento\Payment\Model\Method\AbstractMethod">
<plugin sortOrder="5" name="testValidate"
type="{Vendor}\{Module Name}\Plugin\Model\Method\Available" sortOrder="1" disabled="false" />
</type>
</config>
app/code/{Vendor}/{Module Name}/Plugin/Model/Method/Available.php
<?php
namespace {Vender}\{Module Name}\Plugin\Model\Method;
class Available
{
/**
*
* @param \Magento\Payment\Model\Method\AbstractMethod $subject
* @param $result
* @return bool
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function afterIsAvailable(\Magento\Payment\Model\Method\AbstractMethod $subject, $result)
{
if($subject->getCode() == 'your payment code, eg: cashondelivery') {
return false;
}
return $result;
}
}
$subject
is the current Payment Method Model.
Best Answer
I created this module to enable/disable a certain method of payment when a condition applies (in this case, to enable Money order only in the admin). You can fork it and modify it as you want to match the criteria you need. Key is in the Observer.php file where the logic you need to modify resides.