Created
April 19, 2013 22:56
-
-
Save tegansnyder/5423811 to your computer and use it in GitHub Desktop.
Mailchimp Ecommerce 360 for Magento Order Event Observer .... Some Code from ebizmart's extension.... but I just needed the Ecomerce360 aspect not all the other stuff there extension provides.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<config> | |
<modules> | |
<Somecompany_Extensionname> | |
<version>0.1.0</version> | |
</Somecompany_Extensionname> | |
</modules> | |
<global> | |
<models> | |
<myobserver> | |
<class>Somecompany_Extensionname_Model</class> | |
</myobserver> | |
</models> | |
</global> | |
<frontend> | |
<events> | |
<controller_action_predispatch> | |
<observers> | |
<mailchimp_ecom_listen> | |
<type>singleton</type> | |
<class>myobserver/observer</class> | |
<method>mailchimpListen</method> | |
</mailchimp_ecom_listen> | |
</observers> | |
</controller_action_predispatch> | |
<sales_order_save_after> | |
<observers> | |
<mailchimp_ecom_sale> | |
<type>singleton</type> | |
<class>myobserver/observer</class> | |
<method>mailchimpSale</method> | |
</mailchimp_ecom_sale> | |
</observers> | |
</sales_order_save_after> | |
</events> | |
</frontend> | |
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Somecompany_Extensionname_Model_Observer extends Mage_Core_Model_Abstract { | |
public function mailchimpListen($observer) { | |
$thirty_days = time()+60*60*24*30; | |
if (isset($_REQUEST['mc_cid']) && isset($_REQUEST['mc_eid'])) { | |
setcookie('mc_campaign_id',trim($_REQUEST['mc_cid']), $thirty_days); | |
setcookie('mc_email_id',trim($_REQUEST['mc_eid']), $thirty_days); | |
} | |
} | |
public function mailchimpSale($observer) { | |
$order = $observer->getEvent()->getOrder(); | |
if (isset($_COOKIE['mc_campaign_id'],$_COOKIE['mc_email_id'])) { | |
$this->addEcomToMailchimp($order); | |
} | |
} | |
//http://apidocs.mailchimp.com/api/rtfm/campaignecommorderadd.func.php | |
public function addEcomToMailchimp($order) { | |
// include the MCAPI PHP library Mailchimp | |
// http://apidocs.mailchimp.com/api/downloads/mailchimp-api-class.zip | |
require_once '/full/path/to/mailchimp/MCAPI.class.php'; | |
require_once '/full/path/to/mailchimp/config.inc.php'; | |
$MCAPI = new MCAPI($apikey); | |
$order_info = array(); | |
$order_info['id'] = $order->getIncrementId(); | |
$order_info['campaign_id'] = $_COOKIE['mc_campaign_id']; | |
$order_info['email_id'] = $_COOKIE['mc_email_id']; | |
$order_info['total'] = $order->getSubtotal(); | |
$order_info['shipping'] = $order->getShippingAmount(); | |
$order_info['tax'] = $order->getTaxAmount(); | |
$order_info['store_id'] = $order->getStoreId(); | |
$order_info['store_name'] = $order->getStoreName(); | |
$auxPrice = 0; | |
$productsToSkip = array(Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE, Mage_Catalog_Model_Product_Type::TYPE_BUNDLE); | |
$mcitem = array(); | |
foreach ($order->getAllItems() as $item) { | |
$product = Mage::getSingleton('catalog/product')->load($item->getProductId()); | |
if(in_array($product->getTypeId(), $productsToSkip) && $product->getPriceType() == 0){ | |
if($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE){ | |
$auxPrice = $item->getPrice(); | |
} | |
continue; | |
} | |
$mcitem['product_id'] = $product->getEntityId(); | |
$mcitem['product_name'] = $product->getName(); | |
$names = array(); | |
$cat_ids = $product->getCategoryIds(); | |
if(is_array($cat_ids) && count($cat_ids)>0){ | |
$category = Mage::getModel('catalog/category')->load($cat_ids[0]); | |
$mcitem['category_id'] = $cat_ids[0]; | |
$names[] = $category->getName(); | |
while ($category->getParentId() && $category->getParentId()!=1){ | |
$category = Mage::getModel('catalog/category')->load($category->getParentId()); | |
$names[] = $category->getName(); | |
} | |
} | |
$mcitem['category_name'] = (count($names))? implode(" - ",array_reverse($names)) : 'None'; | |
$mcitem['qty'] = $item->getQtyOrdered(); | |
$mcitem['cost'] = ($auxPrice > 0)? $auxPrice : $item->getPrice(); | |
$order_info['items'][] = $mcitem; | |
$auxPrice = 0; | |
} //end foreach | |
try { | |
$MCAPI->campaignEcommOrderAdd($order_info); | |
} catch (Exception $e) { | |
Mage::log($e); | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment