Magento 2-仅针对特定的发货方式启用货到付款


Answers:


9

当选择运输方式“ flatrate_flatrate”时,我在自定义模块中使用插件将CashOnDelivery的isAvailable函数设置为false。

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;
            return;
        }

        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;
            }
            $result = $proceed();
            return $result;
          }
    }

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>

希望这对您有所帮助!随意问任何问题


1
如何在后端执行此操作
Mahi M

您必须使用“ IsAvailable”功能插件创建自定义模块。您无法使用stock-magento在后端执行此操作
juhanix

这是我的状况...如何在后端执行此操作
Mahi M

也许您应该打开一个新问题,在这里您可以提供更多的信息
juhanix

非常感谢@juhanix,我花了两个多小时才找到一个流程。您的解决方案对我有很大帮助。保持编码:)
divya sekar
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.