Magento - OnePage Checkout - 根据运输方式隐藏付款方式

2023-11-27

我已经问过这个问题Magento 堆栈交换没有任何成功,因此我现在在这里问。


我正在使用 Magento Community Edition 1.9.0.1 并已正确创建和注册我的模块,但我似乎无法检测到运输方式。基本上,我想隐藏货到付款 if 扁平率 or 免运费被选中。这是我的观察者类的代码:

class Kol_PaymentToggle_Model_Observer
{
  public function paymentMethodIsActive(Varien_Event_Observer $observer) {
      $event  = $observer->getEvent();
      $method = $event->getMethodInstance();
      $result = $event->getResult(); 
    $quote = $observer->getEvent()->getQuote();
    $shippingMethod = $quote->getShippingAddress()->getShippingMethod();
      if($shippingMethod == "standardshipping" || $shippingMethod == "free") {
        if($method->getCode() == 'cashondelivery' ) {
              $result->isAvailable = false;
          }
    }
  }
}

我猜我没有使用正确的运输方式代码名称或付款方式代码名称,但我不确定。有人有什么建议吗?

我只启用了 3 种运输方式:

  • 到店取货
    标题 = 到店取货
    方法名称 = 在商店收集 (扩展链接)
  • 扁平率
    标题 = 标准交付
    方法名称 = 标准运输
  • 免运费
    标题 = 免费送货
    方法名称 = 免费

输出配置文件

<?xml version="1.0"?>
<config>
    <modules>
        <Kol_PaymentToggle>
            <version>0.0.1</version>
        </Kol_PaymentToggle>
    </modules>
    <frontend>
        <events>
            <payment_method_is_active>
                <observers>
                    <paymentfilter_payment_method_is_active>
                        <type>singleton</type>
                        <class>Kol_PaymentToggle_Model_Observer</class>
                        <method>paymentMethodIsActive</method>
                    </paymentfilter_payment_method_is_active>
                </observers>
            </payment_method_is_active>
        </events>
    </frontend>
</config>

至于我,你试图根据运输方式隐藏一些付款方式。为此,你根本不需要观察事物。只需跟随我,你就可以做到这一点,

每个方法(在一页中查看)都会发布选择到下一级的方法。这样您就可以获得在付款方式级别中选择的运输方式。只需打印帖子中的内容即可

app/design/frontend/base/default/template/checkout/onepage/payment/methods.phtml

在此添加以下一项,

<?php print_r($_POST); ?>

现在您可以获得上一步选择的运输方式。请注意,现在您可以在同一文件中添加简单的逻辑(if else)条件来隐藏付款,

例如这里我想隐藏check / money order付款方式,如果运输方式是flat。这里的付款方式代码是checkmo。您可以通过简单地打印该变量来获取付款方式代码,例如echo $_code = $_method->getCode();在同一个文件中。所以这里只需添加简单的 if else ,

  <?php
    $methods = $this->getMethods();


    $oneMethod = count($methods) <= 1;
?>
<?php if (empty($methods)): ?>
    <dt>
        <?php echo $this->__('No Payment Methods') ?>
    </dt>
<?php else:
    foreach ($methods as $_method):
       echo  $_code = $_method->getCode();


if($_POST['shipping_method'] == 'flatrate_flatrate') {
if($_code == 'checkmo') {
    continue;
}
}
?>

Here,

 if($_POST['shipping_method'] == 'flatrate_flatrate') {
if($_code == 'checkmo') {
    continue;
}
}

检查运输方式并跳过我们不想显示的付款方式。就是这样。如果您有任何疑问,请在此评论。

Note:

 shipping_method => flatrate_flatrate
 paymet_method   => checkmo
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Magento - OnePage Checkout - 根据运输方式隐藏付款方式 的相关文章