【问题标题】:Active an extension based on condition in magento 2根据 magento 2 中的条件激活扩展
【发布时间】:2020-07-02 10:51:43
【问题描述】:

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

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

我的产品有一个名为“otherthancod”的产品属性。如果 'otherthancod' 处于活动状态,然后只显示自定义 结帐页面中的支付网关。为此,我编写了以下代码。

        $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{
 

}

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

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

【问题讨论】:

  • 隐藏扩展是什么意思?
  • 在结帐页面我可以看到货到付款和自定义支付网关。如果条件为假,那么我需要从结帐页面隐藏自定义支付网关。你能帮忙解决这个问题吗?
  • webkul.com/blog/… 看到这个
  • 非常感谢。但是这段代码需要写在 app/code/ 文件夹中。所以在那之后我必须运行 php bin/magento setup:upgrade 。我太担心 abi=out 运行这个命令了。这就是我不想创建其他扩展的原因。我可以在任何其他已安装的扩展中添加此代码吗?
  • 你为什么担心 setup:upgrade 命令?它是 OOTB 命令,应该可以正常工作

标签: magento magento2


【解决方案1】:

不要犹豫,在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

【讨论】:

  • 我必须运行 php bin/magento setup:upgrade 。我太担心运行这个命令了。这就是我不想创建其他扩展的原因。我可以在任何其他已安装的扩展中添加此代码吗?
  • 如果您在app/code 中已有一些模块,您可以将其添加到您自己的另一个模块中。请勿将其添加到第三方扩展程序中,否则更新时会遇到麻烦。
  • 你可能不得不在某一时刻运行setup:upgrade ;) 所以,我建议在本地版本(而不是你的服务器)上尝试它。如果您担心,请先备份数据库。
【解决方案2】:

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

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

<?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
        }
    }
}

现在付款方式 Check Money Order 已从结帐页面禁用。

参考请查看this链接

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    • 2014-02-09
    • 2022-08-16
    相关资源
    最近更新 更多