I need to create a simple custom payment method like "cash on delivery" but with extra fee when customer selects it. I implemented payment method, but can't find how to make part with extra fee. Can anybody give me a hint?
Magento 1.9 Payment Methods – Custom Payment Method with Extra Fee
custommagento-1.9payment-methods
Related Solutions
I use an plugin in a custom module to set the isAvailable()
function for Magento\OfflinePayments\Model\Cashondelivery
to false when shipping method flatrate_flatrate
is selected.
file: <magento-root>/app/code/MyCompany/MyModule/Plugin/CashondeliveryPlug.php
<?php
namespace MyCompany\MyModule\Plugin;
use Magento\Payment\Model\Method\AbstractMethod;
use Magento\Quote\Model\Quote;
class CashondeliveryPlug
{
/**
* @var \Magento\Checkout\Model\Session
*/
protected $_checkoutSession;
/**
* Constructor
*
* @param \Magento\Checkout\Model\Session $checkoutSession
*/
public function __construct(
\Psr\Log\LoggerInterface $logger,
\Magento\Checkout\Model\Session $checkoutSession
) {
$this->logger = $logger;
$this->_checkoutSession = $checkoutSession;
}
public function aroundIsAvailable(
\Magento\Payment\Model\Method\AbstractMethod $subject, callable $proceed)
{
$shippingMethod = $this->_checkoutSession->getQuote()->getShippingAddress()->getShippingMethod();
// $this->logger->debug($shippingMethod);
if ($shippingMethod == 'flatrate_flatrate')
return false;
return $proceed();
}
}
file: <magento-root>/app/code/MyCompany/MyModule/etc/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\OfflinePayments\Model\Cashondelivery">
<plugin name="cashondeliveryplug"
type="MyCompany\MyModule\Plugin\CashondeliveryPlug"
disabled="false" sortOrder="10"/>
</type>
</config>
Hope this helps you! Feel free to ask any questions.
you can try this extension, but before that test in staging or demo site : https://www.magentocommerce.com/magento-connect/cash-on-delivery-3.html
or
https://www.magentocommerce.com/magento-connect/cash-on-delivery-extra-charges.html
<?php
class Mfp_Cod_Model_Observer
{
public function getCashOnDelvery(Varien_Event_Observer $observer) {
$event = $observer->getEvent();
$method = $event->getMethodInstance();
$result = $event->getResult();
$isModuleEnable = Mage::getStoreConfig('cod/cod/enable');
$returnValue = false;
if($isModuleEnable) {
if($method->getCode() == 'cashondelivery' ){
$quote = Mage::getSingleton('checkout/cart')->getQuote();
if($quote->getEntityId()!='')
{
$add = $quote->getShippingAddress();
$postcode = $add->getData('postcode');
$comparisonMode = Mage::getStoreConfig('cod/cod/mode');
$zipCodes = Mage::getStoreConfig('cod/cod/zipcode');
$isExist = false;
if(strlen($postcode) >= 6) {
if(trim($zipCodes) == '') {
$result->isAvailable = true;
} else {
if(strpos($zipCodes, $postcode) !== false) {
$isExist = true;
}
if($isExist != true) {
$zipCodesArray = explode(',', nl2br($zipCodes));
if(count($elementLineArray) > 1) {
foreach($zipCodesArray as $codzipLine) {
$elementLineArray = explode('-', $codzipLine);
if(count($elementLineArray) == 2 && ( $postcode >= $elementLineArray[0] && $postcode <= $elementLineArray[1] )) {
$isExist = true;
break;
} else if($postcode == $codzipLine) {
$isExist = true;
break;
}
}
}
}
$returnValue = ($isExist)?true:false;
$result->isAvailable = $returnValue;
}
}
$result->isAvailable = $returnValue;
}
}
}
}
}
Best Answer
I found this article how to add custom fee and only added couple changes to collect() function, like this:
Now only when user selects my payment method this fee would be added to totals