/app/code/local/Ebizmarts/Mailchimp/Model/Ecomm360.php

https://github.com/seloshow/Magento-Pruebas · PHP · 123 lines · 98 code · 25 blank · 0 comment · 17 complexity · fc0ecec406d68bb98812cae03d15aa44 MD5 · raw file

  1. <?php
  2. class Ebizmarts_Mailchimp_Model_Ecomm360 extends Ebizmarts_Mailchimp_Model_MCAPI{
  3. protected $_info = array();
  4. protected $_auxPrice = 0;
  5. protected $_order;
  6. protected $_productsToSkip = array(Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE, Mage_Catalog_Model_Product_Type::TYPE_BUNDLE);
  7. public function _construct(){
  8. parent::_construct();
  9. $this->_init('mailchimp/ecomm360');
  10. }
  11. public function registerMe(){
  12. if(Mage::helper('mailchimp')->isEcomm360Activated()){
  13. $thirty_days = time()+60*60*24*30;
  14. if (isset($_REQUEST['mc_cid'])){
  15. setcookie('ebiz_mc_pro_campaign_id',trim($_REQUEST['mc_cid']), $thirty_days);
  16. }
  17. if (isset($_REQUEST['mc_eid'])){
  18. setcookie('ebiz_mc_pro_email_id',trim($_REQUEST['mc_eid']), $thirty_days);
  19. }
  20. }
  21. return $this;
  22. }
  23. public function runEcomm360($order){
  24. if (isset($_COOKIE['ebiz_mc_pro_campaign_id'],$_COOKIE['ebiz_mc_pro_email_id']) && Mage::helper('mailchimp')->isEcomm360Activated()){
  25. $this->logSale($order);
  26. }
  27. return $this;
  28. }
  29. private function logSale($order){
  30. $this->_order = $order;
  31. $apikey = Mage::helper('mailchimp')->getApiKey();
  32. if(!$apikey){
  33. return false;
  34. }
  35. $this->MCAPI($apikey);
  36. $this->_info = array(
  37. 'id' => $this->_order->getIncrementId(),
  38. 'campaign_id'=>$_COOKIE['ebiz_mc_pro_campaign_id'],
  39. 'email_id'=>$_COOKIE['ebiz_mc_pro_email_id'],
  40. 'total'=>$this->_order->getSubtotal(),
  41. 'shipping'=>$this->_order->getShippingAmount(),
  42. 'tax' =>$this->_order->getTaxAmount(),
  43. 'store_id'=>$this->_order->getStoreId(),
  44. 'store_name' => $this->_order->getStoreName(),
  45. 'plugin_id'=>1215,
  46. 'items'=>array()
  47. );
  48. $this->setItemstoSend();
  49. $res = $this->campaignEcommOrderAdd($this->_info);
  50. if ($this->errorCode){
  51. $this->setCode($this->errorCode);
  52. $this->setMessage($this->errorMessage);
  53. Mage::helper('mailchimp')->addException($this);
  54. return false;
  55. }
  56. if($res) $this->registerInfo();
  57. return $this;
  58. }
  59. private function setItemstoSend(){
  60. foreach ($this->_order->getAllItems() as $item) {
  61. $mcitem = array();
  62. $product = Mage::getSingleton('catalog/product')->load($item->getProductId());
  63. if(in_array($product->getTypeId(), $this->_productsToSkip) && $product->getPriceType() == 0){
  64. if($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE){
  65. $this->_auxPrice = $item->getPrice();
  66. }
  67. continue;
  68. }
  69. $mcitem['product_id'] = $product->getEntityId();
  70. $mcitem['product_name'] = $product->getName();
  71. $names = array();
  72. $cat_ids = $product->getCategoryIds();
  73. if (is_array($cat_ids) && count($cat_ids)>0){
  74. $category = Mage::getModel('catalog/category')->load($cat_ids[0]);
  75. $mcitem['category_id'] = $cat_ids[0];
  76. $names[] = $category->getName();
  77. while ($category->getParentId() && $category->getParentId()!=1){
  78. $category = Mage::getModel('catalog/category')->load($category->getParentId());
  79. $names[] = $category->getName();
  80. }
  81. }
  82. $mcitem['category_name'] = (count($names))? implode(" - ",array_reverse($names)) : 'None';
  83. $mcitem['qty'] = $item->getQtyOrdered();
  84. $mcitem['cost'] = ($this->_auxPrice > 0)? $this->_auxPrice : $item->getPrice();
  85. $this->_info['items'][] = $mcitem;
  86. $this->_auxPrice = 0;
  87. }
  88. return $this;
  89. }
  90. private function registerInfo(){
  91. $this->setStoreId($this->_info['store_id'])
  92. ->setIncrementId(trim($this->_info['id']))
  93. ->setOrderId($this->_order->getEntityId())
  94. ->setCampaignId($this->_info['campaign_id'])
  95. ->setSentTime(date("Y-m-d H:i:s",time()))
  96. ->save();
  97. }
  98. }