根据 magento 2 中的条件激活扩展

Active an extension based on condition in magento 2

您好,我的 magento2 商店有 2 种付款方式。一种是货到付款,另一种是自定义支付网关。我安装了自定义支付网关扩展,它工作正常。

现在我有一些条件,如果那个条件没问题,那么我只需要激活那个自定义支付网关扩展。

My products have a product attribute called 'otherthancod' . If 'otherthancod' is active then only show the custom payment gateway in checkout page. For that i write the following code .

        $items = $cart->getItems();
        $flag = 0;
        $count=0;
        foreach($items as $item){
            
            $attribute1 = $item->getProduct()->getData('otherthancod');
             if($attribute1){
                $flag++;
                $count++;
            }else{
                $flag--;
            }
        }
        
        if($flag == $count){
                    $checkResult = $observer->getEvent()->getResult();
                    $checkResult->setData('is_available', true); 
        }else{
                    $checkResult = $observer->getEvent()->getResult();
                    $checkResult->setData('is_available', false); 
        }
        
  

现在我想知道我需要把这段代码放在哪里?我不想为此创建另一个扩展。

请帮忙。

在我的自定义付款扩展中,我看到了以下页面 app/code/custompaymentgaetway/custom/Gateway/Config/config.php

class Config extends \Magento\Payment\Gateway\Config\Config{
 

}

我可以在此 class 之前添加 if 条件吗? 我认为此 class 正在激活支付网关。

我可以看到我支付网关的前端模板是 \view\frontend\web\template\custompaymentgaetway.html。实际上我想在条件为假时隐藏这个前端。

不要犹豫,在 app/code/YourNamespace 中创建您自己的模块。

基本上你只需要一个 registration.php 文件和一个 etc/module.xml:

https://devdocs.magento.com/videos/fundamentals/create-a-new-module/


另请参阅以下示例,说明如何为此事件声明观察者:

https://magento.stackexchange.com/a/188367/27460

你不应该害怕 setup:upgrade 命令,因为它是 OOTB Magento 命令,它应该可以正常工作,如果出现问题,请修复它可能是服务器端的一些权限问题

首先您需要在 app/code/Company/Module/etc/. 下创建 events.xml 文件,然后在其中写入 “payment_method_is_active” 事件。这是点击付款方式可用性结帐页面的事件。

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="payment_method_is_active">
        <observer name="custom_payment" instance="Company\Module\Observer\PaymentMethodAvailable" />
    </event>
</config>

现在在Company/Module/Observer/下创建PaymentMethodAvailable.php并在文件中写入以下代码。我正在禁用支票汇票付款方式,您可以根据需要更改付款方式代码。

<?php

namespace Company\Module\Observer;

use Magento\Framework\Event\ObserverInterface;


class PaymentMethodAvailable implements ObserverInterface
{
    /**
     * payment_method_is_active event handler.
     *
     * @param \Magento\Framework\Event\Observer $observer
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        // you can replace "checkmo" with your required payment method code
        if($observer->getEvent()->getMethodInstance()->getCode()=="checkmo"){
            $checkResult = $observer->getEvent()->getResult();
            $checkResult->setData('is_available', false); //this is disabling the payment method at checkout page
        }
    }
}

现在付款方式支票汇票在结帐页面被禁用。

参考请查看thislink