magento - 仅在特定状态下设置付款方式 "Cash On Delivery"

标签 magento payment magento-1.8

我只想为网站所有者所在的州设置“货到付款”

我知道如何设置“指定国家”

我使用的是 Magento 1.8

我怎样才能实现这个目标?

最佳答案

LuFFy,您使用 magento 事件观察器执行此操作:

创建一个扩展,步骤如下:

在以下位置创建 config.xml:app/code/community/Devamitbera/Statewisecod/etc/config.xml

<?xml version="1.0" encoding="utf-8"?>
<!--
@Author Amit Bera
@Email <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="791d1c0f571814100d1b1c0b18391e14181015571a1614" rel="noreferrer noopener nofollow">[email protected]</a>
@ Website: www.amitbera.com
*/-->
<config>
    <modules>
        <Devamitbera_Statewisecod>
            <version>1.0.0</version>
        </Devamitbera_Statewisecod>
    </modules>
    <global>
        <models>
            <statewisecod>
                <class>Devamitbera_Statewisecod_Model</class>
            </statewisecod>
        </models>
    </global>
    <frontend> <!--  run observer  event for frontend -->
        <events>
            <payment_method_is_active>
                <observers>
                    <enable_cod_for_some_state>
                        <class>statewisecod/observer</class>
                        <method>EnableCod</method>
                    </enable_cod_for_some_state>
                </observers>
            </payment_method_is_active>
        </events>
    </frontend>
</config>

创建观察者文件 在 app/code/community/Devamitbera/Statewisecod/Model

下的 Observer.php

此文件的代码:

  <?php
class Devamitbera_Statewisecod_Model_Observer
{
    public function EnableCod($observer){
        $result=$observer->getEvent()->getResult();
        $MethodInstance=$observer->getEvent()->getMethodInstance();
        $quote=$observer->getEvent()->getQuote();

        if($quote && $quote->getId()):
        /* If Payment method is  cashondelivery  then  conitnue  */
            if($MethodInstance->getCode()=='cashondelivery'){
                #Mage::log('Payment is Cod',null,'Cod.log',true);
                $ShippingAddress=$quote->getShippingAddress();

                /* region_id is working when country have  
                * drop state/regions.
                */
                /* Here i  have put USA coutry new work & Washinton redion */

                #Mage::log('redion'.$ShippingAddress->getRegionId(),null,'redion.log',true);

                $CodEnableRegionIds=array(62,43);
                if(in_array($ShippingAddress->getRegionId(),$CodEnableRegionIds)):
                      $result->isAvailable=true;
                elseif(is_null($ShippingAddress->getRegionId()) && !is_null($ShippingAddress->getRegion())):
                /* This section working when State/region is not dropdown 
                 and state is dropdown
                */
                $textListRegionName=array('West bengal','Delhi');
                    if(in_array($ShippingAddress->getRegion(),$textListRegionName)){
                          $result->isAvailable=true;
                    }else{
                          $result->isAvailable=false;
                    }
                else:
                  $result->isAvailable=false;
                endif;              


                return   $result->isAvailable;
            }

        endif;


    }
}

app/etc/modules下创建模块文件Devamitbera_Statewisecod.xml

<?xml version="1.0" encoding="utf-8"?>
<!--
@Author Amit Bera
@Email <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7e1a1b08501f13170a1c1b0c1f3e19131f1712501d1113" rel="noreferrer noopener nofollow">[email protected]</a>
@ Website: www.amitbera.com
-->
<config>
    <modules>
    <Devamitbera_Statewisecod>
        <codePool>community</codePool>
        <active>true</active>
        <depends><Mage_Payment/></depends>
    </Devamitbera_Statewisecod>
    </modules>
</config>

这里cashondelivery是付款方式货到付款代码....保存在数据库中。

-编辑:

region_id 在国家/地区删除州/地区列表时起作用。

if(in_array($ShippingAddress->getRegionId(),$CodEnableRegionIds)):
                      $result->isAvailable=true;

如果州/地区不是下拉菜单,则以下逻辑有效

elseif(is_null($ShippingAddress->getRegionId()) && !is_null($ShippingAddress->getRegion())):
$textListRegionName=array('West bengal','Delhi');
    if(in_array($ShippingAddress->getRegion(),$textListRegionName)){
      $result->isAvailable=true;
    }else{
      $result->isAvailable=false;
    }

关于magento - 仅在特定状态下设置付款方式 "Cash On Delivery",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24567979/

相关文章:

payment-gateway - Bluesnap 市场 - 如何阻止供应商销售?

php - fatal error : Call to a member function getId() on a non-object in/app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController. PHP

php - 如何在 Magento 的自定义产品网格中显示是/否属性?

mysql - Magento在具有正确的用户设置的同时发出访问被拒绝

php - Magento - 从购物车中删除产品

Paypal 形式的php脚本

image - 如何在 magento 上显示所有产品图片

magento - 获取产品更改

php - Magento Soap V2 CatalogProductListOfAdditionalAttributes 无法识别。

process - 这个支付用户的过程是否可靠和安全?