We are doing custom Magento themes, Magento design, custom Magento extensions and creative services through Magento designers & developers for worldwide shop owners.

Last News
Magento : active  deactivate or Change payment method,on city county or region change

Magento : active deactivate or Change payment method,on city county or region change

many people want active for example cash on delivery payment method just for specific country region or city.
I will explain how develop this technique in page checkout for any onestepchekout in magento :
before start add field multiselect frontend type example in your module app/local/YouCompany/YourModule/config/system.xml (for cashondelivery section add this code):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<config>
   <sections>
        <payment translate="label" module="payment">
            <label>Payment Methods</label>
            <tab>sales</tab>
            <frontend_type>text</frontend_type>
            <sort_order>400</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
            <groups>
                <cashondelivery translate="label">
                    <label>Cash On Delivery Payment</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>30</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <fields>
                        <specificregion translate="label">
                            <label>Payment from Specific region</label>
                            <frontend_type>multiselect</frontend_type>
                            <sort_order>51</sort_order>
                            <source_model>adminhtml/system_config_source_allregion</source_model>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                            <can_be_empty>1</can_be_empty>
                        </specificregion>
                    </fields>
                </cashondelivery>
            </groups>
        </payment>
    </sections>
</config>

1 – First go to desin/forntend/youtheme-or-base-default/template/onepagecheckout-or-onestepchekout/onepagecheckout.phtml

and change (by example if you want active or deactivate payment method by region ) every line inside javascript code :
save_address_information(save_address_url);
to
save_address_information(save_address_url+"region_id/"+$('billing:region_id').getValue());

and add after this declaration is js code :
var save_address_url = 'getUrl('onepagecheckout/index/save_address', array('_secure' => true))?>';
this line :
save_address_information(save_address_url+"region_id/"+$('billing:region_id').getValue());

2 – Now go and create observer inside your custom module already exist or create the module :
got to app/local/Yourcompany/Yourmodule/etc/config.xml
add :

1
2
3
4
5
6
7
8
9
10
11
12
13
<frontend>
        <events>
            <payment_method_is_active>
                <observers>
                    <paymentfilter_payment_method_is_active>
                        <type>singleton</type>
                        <class>Yourmodule/observer</class>
                        <method>filterpaymentmethod</method>
                    </paymentfilter_payment_method_is_active>
                </observers>
            </payment_method_is_active>
        </events>
    </frontend>

go and create the observer class inside to app/local/Yourcompany/Yourmodule/model/observer.php

the content of class :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
class Yourcompany_Yourmodule_Model_Observer {
 public function filterpaymentmethod(Varien_Event_Observer $observer) {
    /* call get payment method */
        $method = $observer->getEvent()->getMethodInstance();
        $postDatas =  Mage::app()->getRequest()->getParams();

        //echo $postDatas["region_id"];
        if($method->getCode()=='cashondelivery'){
            
      if(isset($postDatas["region_id"]))
      {

       
            $RegionIdadmin=Mage::getStoreConfig("payment/cashondelivery/specificregion");
            $RegionIdadmin=explode(",", $RegionIdadmin);
           if(in_array($postDatas["region_id"],$RegionIdadmin)){
            $result = $observer->getEvent()->getResult();   
            $result->isAvailable = true;
            return;
            }
            else{
            $result = $observer->getEvent()->getResult();   
            $result->isAvailable = false;
            }
                      
       
        }
        else
        {
            $currentAction=Mage::app()->getRequest()->getActionName();
            if($currentAction=="save_address")
            {
            $result = $observer->getEvent()->getResult();   
            $result->isAvailable = false;
           }
        }
         }
        return;
    }
}
?>

3 – go to your admin and choose the specific allowed region in configuration of cash on delivery

and all is done

One comment

  1. Wow, you have explained this very well. We do prefer default Magento cash on delivery option but would love to explore a more customized and polished solution. Gonna give it a try now :)

    Thanks for sharing.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*