Set Payment Method "Cash On Delivery" on Particular State Only - magento

I want to set "Cash On Delivery" for the site owner's State only
I know how to set For "Specified Country"
I am using Magento 1.8
How can I achieve this ?

LuFFy,you doing this using magento event observer:
Create an extension and here the step:
create config.xml under :app/code/community/Devamitbera/Statewisecod/etc/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>
Create observer file
under Observer.php under app/code/community/Devamitbera/Statewisecod/Model
Code of this file:
<?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;
}
}
create module file Devamitbera_Statewisecod.xml under app/etc/modules
<?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>
Here cashondelivery is payment method code of cash on delivery.... which is saved in database.
- Edited:
region_id is working when country have drop state/regions list.
if(in_array($ShippingAddress->getRegionId(),$CodEnableRegionIds)):
$result->isAvailable=true;
If State/region is not dropdown then Below logic is work
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;
}

Related

Magento 1.9 disable plugin for specific ip

I've got a Magento store (1.9) with a very simple plugin. It changes the customer group when someone places an order in a website. But we also work with a POS.
In the POS the plugin is also trying to change the customer group but it gives an error.
So what I want to do is disable the plugin for our local IP (or User).
The code is the following or check Github:
observer.php
<?php
class RvdH_GroupChange_Model_Observer
{
public function changeGroup(Varien_Event_Observer $observer)
{
$order = $observer->getEvent()->getOrder();
$customer = Mage::getModel('customer/customer')->load($order->getCustomerId());
/*$event = $observer->getEvent(); //Fetches the current event"
$customer = $event->getCustomer();
$dbcustomer = Mage::getModel('customer/customer')->load($customer[entity_id]);*/
// ensure it's not guest checkout
if ($customer->getId()) {
$customer->setGroupId(5);
$customer->save();
}
}
}
config.xml
<?xml version="1.0"?>
<config>
<modules>
<RvdH_GroupChange>
<version>0.1.0</version>
</RvdH_GroupChange>
</modules>
<global>
<models>
<RvdH_GroupChange>
<class>RvdH_GroupChange_Model</class>
<resourceModel>module_mysql4</resourceModel>
</RvdH_GroupChange>
</models>
<events>
<sales_order_place_after>
<observers>
<RvdH_GroupChange>
<class>RvdH_GroupChange_Model_Observer</class>
<method>changeGroup</method>
</RvdH_GroupChange>
</observers>
</sales_order_place_after>
</events>
</global>
</config>
Solved.
I just had to change the <global> tag to <frontend> in the config.xml.
Now it only works in frontend and not global.

Which Event call on Clear Shopping Cart Button Magento

I want to call event/observer when user click on "clear shopping cart button" which perform some action on database. i search a lot but didn't find any specific solution.
please anyone will give me the solution that Which event is calling on clear shopping cart button in magento?
Try this event controller_action_predispatch_checkout_cart_updatePost.
And your config.xml file should be,
<?xml version="1.0"?>
<config>
<modules>
<Packagename_ModuleName>
<version>0.1.0</version>
</Packagename_ModuleName>
</modules>
<global>
<helpers>
<modulename>
<class>Packagename_ModuleName_Helper</class>
</modulename>
</helpers>
<models>
<modulename>
<class>Packagename_ModuleName_Model</class>
<resourceModel>modulename_mysql4</resourceModel>
</modulename>
</models>
<events>
<controller_action_predispatch_checkout_cart_updatePost> <!-- identifier of the event we want to catch -->
<observers>
<controller_action_predispatch_checkout_cart_updatePost_handler> <!-- identifier of the event handler -->
<type>singleton</type> <!-- class method call type; valid are model, object and singleton -->
<class>modulename/observer</class> <!-- observers class alias -->
<method>clearCart</method> <!-- observer's method to be called -->
<args></args> <!-- additional arguments passed to observer -->
</controller_action_predispatch_checkout_cart_updatePost_handler>
</observers>
</controller_action_predispatch_checkout_cart_updatePost>
</events>
</global>
</config>
And Model/Observer.php :
<?php
class Packagename_ModuleName_Model_Observer
{
public function clearCart(Varien_Event_Observer $observer)
{
//execute only in empty the cart function(all items removed )
$updateAction = (string)Mage::app()->getRequest()->getParam('update_cart_action'); if ($updateAction != 'empty_cart') return;
echo "got it"; exit;
//your stuffs goes here.
}
}
Note: It is not triggered when we clear single cart (product) item . I tested it in my localserver and it is working fine.

magento: save categories with custom attribute to custom database table

I added custom attribute to a Category "custom_attribute". If selected I need to save this category name and url to new table in a database that I created "store_custom_categories".
This table has columns: id, name, url
How I do that?
Iva,You can used the magento Event function for that case.....
I am creating an extension that case....
app/code/local/Amit/Autoupdatecat/etc/ config.xml
<?xml version="1.0" ?>
<config>
<modules>
<Amit_Autoupdatecat>
<version>1.0.0</version>
</Amit_Autoupdatecat>
</modules>
<global>
<models>
<autoupdatecat>
<class>Amit_Autoupdatecat_Model</class>
</autoupdatecat>
</models>
</global>
<global>
<events>
<catalog_category_save_commit_after>
<observers>
<autoupdatecatgories>
<type>singleton</type>
<class>autoupdatecat/observer</class>
<method>saveCategorytabs</method>
</autoupdatecatgories>
</observers>
</catalog_category_save_commit_after>
</events>
</global>
</config>
app/code/local/Amit/Autoupdatecat/Model/ Observer.php
<?php
class Amit_Autoupdatecat_Model_Observer
{
public function saveCategorytabs($observer)
{
/*get category value */
$data= $observer->getEvent()->getData();
$name=$data['name'];
/*more fields and write code in below for custom table*/
}
}

magento how to change cart account in checkout page

in product detail page, the product price is 50 dollar, I use JavaScript change the price to 80 dollar, but when add to cart, it is still 50 dollar in checkout page. how to let it still 80 dollar in checkout page?
You need use "sales_quote_add_item" magento event to update the product price in cart session.
You have to make a custom module for that purpose.
Create a file in app/etc/modules/Company_All.xml
<?xml version="1.0"?> <config> <modules>
<Company_Product>
<codePool>local</codePool>
<active>true</active>
</Company_Product> </modules> </config>
Create configuration file for our module file in app/code/local/Company/Product/etc/config.xml
<?xml version="1.0"?> <config> <global>
<models>
<product>
<class>Company_Product_Model</class>
</product>
</models>
<events>
<sales_quote_add_item><!--Event to override price after adding product to cart-->
<observers>
<company_product_price_observer><!--Any unique identifier name -->
<type>singleton</type>
<class>Company_Product_Model_Price_Observer</class><!--Our observer class name-->
<method>update_book_price</method><!--Method to be called from our observer class-->
</company_product_price_observer>
</observers>
</sales_quote_add_item>
</events> </global> </config>
Create our observer file in app/code/local/Company/Product/Model/Price/Observer.php
class Company_Product_Model_Price_Observer{
public function update_book_price(Varien_Event_Observer $observer) {
$quote_item = $observer->getQuoteItem();
//if(){ //your logic goes here
$customprice = 50;
//}
$quote_item->setOriginalCustomPrice($customprice);
$quote_item->save();
return $this;
}
}

Change Magento default status for duplicated products

I have a Magento store installed, and when a product is duplicated in the backend, Magento sets its status to Disabled by default. I don't want that to happen, the duplicated product should have its status copied from the original product as well.
In this post a partial solution was given. I see where I can find the config.xml and make the necessarry changes. However, where do I put such an observer class? Which file should I use/create and would that require any changes to the config.xml input?
Or does somebody have an overall solution for this issue? Thanks in advance!
Try this:
Create: app/code/local/MagePal/EnableDuplicateProductStatus/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<MagePal_EnableDuplicateProductStatus>
<version>1.0.1</version>
</MagePal_EnableDuplicateProductStatus>
</modules>
<global>
<models>
<enableduplicateproductstatus>
<class>MagePal_EnableDuplicateProductStatus_Model</class>
</enableduplicateproductstatus>
</models>
<events>
<catalog_model_product_duplicate>
<observers>
<enableduplicateproductstatus>
<type>singleton</type>
<class>enableduplicateproductstatus/observer</class>
<method>productDuplicate</method>
</enableduplicateproductstatus>
</observers>
</catalog_model_product_duplicate>
</events>
</global>
</config>
Create: app/code/local/MagePal/EnableDuplicateProductStatus/Model/Observer.php
class MagePal_EnableDuplicateProductStatus_Model_Observer
{
/**
* Prepare product for duplicate action.
*
* #param Varien_Event_Observer $observer
* #return object
*/
public function productDuplicate(Varien_Event_Observer $observer)
{
$newProduct = $observer->getEvent()->getNewProduct();
$newProduct->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
return $this;
}
}
Create: app/etc/modules/MagePal_EnableDuplicateProductStatus.xml
<?xml version="1.0"?>
<config>
<modules>
<MagePal_EnableDuplicateProductStatus>
<active>true</active>
<codePool>local</codePool>
</MagePal_EnableDuplicateProductStatus>
</modules>
</config>
Then clear cache and try duplicating a product.
read more # :
http://magento4u.wordpress.com/2009/06/08/create-new-module-helloworld-in-magento/
http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/customizing_magento_using_event-observer_method
make a new product active by default in magento
I found error on this code and find out the solution below:
On app/code/local/MagePal/EnableDuplicateProductStatus/etc/config.xml change
<method> duplicateProduct </method>
TO
<method>productDuplicate</method>

Resources