【问题标题】:Set Payment Method "Cash On Delivery" on Particular State Only仅在特定州设置付款方式“货到付款”
【发布时间】:2014-07-04 06:28:29
【问题描述】:

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

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

我正在使用 Magento 1.8

我怎样才能做到这一点?

【问题讨论】:

    标签: magento payment magento-1.8


    【解决方案1】:

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

    创建一个扩展,这里是步骤:

    在:app/code/community/Devamitbera/Statewisecod/etc/config.xml

    下创建config.xml
    <?xml version="1.0" encoding="utf-8"?>
    <!--
    @Author Amit Bera
    @Email dev.amitbera@gmail.com
    @ 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>
    

    创建观察者文件 Observer.phpapp/code/community/Devamitbera/Statewisecod/Model

    此文件的代码:

      <?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 dev.amitbera@gmail.com
    @ Website: www.amitbera.com
    -->
    <config>
        <modules>
        <Devamitbera_Statewisecod>
            <codePool>community</codePool>
            <active>true</active>
            <depends><Mage_Payment/></depends>
        </Devamitbera_Statewisecod>
        </modules>
    </config>
    

    这里cashondelivery是支付方式code of cash on delivery....保存在数据库中。

    - 编辑:

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

    【讨论】:

    • “if($method->getCode()=='code')”中的“code”是什么??
    • 抱歉,每次付款都有付款代码,只需转到 app\code\core\Mage\Payment\Model\Method 查看所有付款方式列表。例如:打开 Cashondelivery.php 查看代码保护 $_code = 'cashondelivery' ;
    • 好的兄弟谢谢,我试试..!!
    • 如果有任何问题..让我知道
    猜你喜欢
    • 2021-07-26
    • 2012-12-10
    • 2015-07-01
    • 2015-05-31
    • 2017-05-31
    • 1970-01-01
    • 2015-11-14
    • 2015-07-01
    • 2013-02-15
    相关资源
    最近更新 更多