From 745e78212b5caafb6b080ccb72416faa8e3257a1 Mon Sep 17 00:00:00 2001 From: Santiago Date: Wed, 8 Jun 2016 17:24:07 -0300 Subject: [PATCH] Changed declaration of arrays. Added better display on error messages for APICalls. Updated module version. --- .../Ebizmarts/MailChimp/Model/Api/Orders.php | 6 ++--- .../Ebizmarts/MailChimp/Model/Observer.php | 27 ++++++++++++++++--- .../Ebizmarts/MailChimp/etc/config.xml | 2 +- lib/Ebizmarts/Mailchimp.php | 9 ++----- lib/Ebizmarts/Mailchimp/Exceptions.php | 15 ++++++----- lib/Ebizmarts/Mailchimp/ListsMembers.php | 5 ++-- 6 files changed, 42 insertions(+), 22 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Orders.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Orders.php index 1c9fd15f7..00d551bee 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Orders.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Orders.php @@ -63,20 +63,20 @@ protected function GeneratePOSTPayload($order_from_collection) $data["id"] = $order->getEntityId(); $data["currency_code"] = $order->getOrderCurrencyCode(); $data["order_total"] = $order->getGrandTotal(); - $data["lines"] = []; + $data["lines"] = array(); //order lines $items = $order->getAllVisibleItems(); $item_count = 0; foreach ($items as $item) { $item_count += 1; - $data["lines"][] = [ + $data["lines"][] = array( "id" => (string)$item_count, "product_id" => $item->getProductId(), "product_variant_id" => $item->getProductId(), "quantity" => (int)$item->getQtyOrdered(), "price" => $item->getPrice(), - ]; + ); } //customer data diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index 3a73f2c7f..887154fba 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -90,6 +90,11 @@ protected function _saveCustomerGroups($listId, $apiKey, $hookUrl) $api->lists->webhooks->add($listId, $hookUrl, $events, $sources); } } + catch(Mailchimp_Error $e) + { + Mage::helper('mailchimp')->logError($e->getFriendlyMessage()); + Mage::getSingleton('adminhtml/session')->addError($e->getFriendlyMessage()); + } catch (Exception $e){ Mage::helper('mailchimp')->logError($e->getMessage()); } @@ -106,8 +111,18 @@ public function handleSubscriber(Varien_Event_Observer $observer) //@Todo Create Api/Subscriber class for subscriber functions $status = Mage::helper('mailchimp')->getStatus(); $api = new Ebizmarts_Mailchimp($apiKey); + $mergeVars = array(); + if($subscriber->getFirstName()){ + $mergeVars['FNAME'] = $subscriber->getFirstName(); + } + if($subscriber->getLastName()){ + $mergeVars['LNAME'] = $subscriber->getLastName(); + } try { - $api->lists->members->add($listId, null, $status, $subscriber->getEmail()); + $api->lists->members->add($listId, null, $status, $subscriber->getEmail(), $mergeVars); + }catch(Mailchimp_Error $e){ + Mage::helper('mailchimp')->logError($e->getFriendlyMessage()); + Mage::getSingleton('adminhtml/session')->addError($e->getFriendlyMessage()); }catch (Exception $e){ Mage::helper('mailchimp')->logError($e->getMessage()); } @@ -125,8 +140,14 @@ public function handleSubscriberDeletion(Varien_Event_Observer $observer) $listId = Mage::helper('mailchimp')->getConfigValue(Ebizmarts_MailChimp_Model_Config::GENERAL_LIST); $api = new Ebizmarts_Mailchimp($apiKey); try { - $api->lists->members->add($listId, null, 'unsubscribed', $subscriber->getEmail()); - }catch (Exception $e){ + $md5HashEmail = md5(strtolower($subscriber->getEmail())); + $api->lists->members->update($listId, $md5HashEmail, null, 'unsubscribed'); + } + catch(Mailchimp_Error $e){ + Mage::helper('mailchimp')->logError($e->getFriendlyMessage()); + Mage::getSingleton('adminhtml/session')->addError($e->getFriendlyMessage()); + } + catch (Exception $e){ Mage::helper('mailchimp')->logError($e->getMessage()); } } diff --git a/app/code/community/Ebizmarts/MailChimp/etc/config.xml b/app/code/community/Ebizmarts/MailChimp/etc/config.xml index bc718d17f..53a04de65 100755 --- a/app/code/community/Ebizmarts/MailChimp/etc/config.xml +++ b/app/code/community/Ebizmarts/MailChimp/etc/config.xml @@ -3,7 +3,7 @@ - 1.0.0 + 1.0.1 diff --git a/lib/Ebizmarts/Mailchimp.php b/lib/Ebizmarts/Mailchimp.php index a13166b72..6f4179a94 100755 --- a/lib/Ebizmarts/Mailchimp.php +++ b/lib/Ebizmarts/Mailchimp.php @@ -257,12 +257,7 @@ public function call($url,$params,$method=Ebizmarts_Mailchimp::GET,$encodeJson=t $info = curl_getinfo($ch); $result = json_decode($response_body, true); -// Mage::log('$info', null, 'ebizmarts.log', true); -// Mage::log($info, null, 'ebizmarts.log', true); -// Mage::log('$result', null, 'ebizmarts.log', true); -// Mage::log($result, null, 'ebizmarts.log', true); -// Mage::log('method, if null is GET', null, 'ebizmarts.log', true); -// Mage::log($method, null, 'ebizmarts.log', true); + if(curl_error($ch)) { @@ -272,7 +267,7 @@ public function call($url,$params,$method=Ebizmarts_Mailchimp::GET,$encodeJson=t if(floor($info['http_code'] / 100) >= 4) { $errors = (isset($result['errors'])) ? $result['errors'] : ''; - throw new Mailchimp_Error($result['title'],$result['detail'], $errors); + throw new Mailchimp_Error($url, $result['title'],$result['detail'], $errors); } return $result; diff --git a/lib/Ebizmarts/Mailchimp/Exceptions.php b/lib/Ebizmarts/Mailchimp/Exceptions.php index b574340bb..0b2160009 100644 --- a/lib/Ebizmarts/Mailchimp/Exceptions.php +++ b/lib/Ebizmarts/Mailchimp/Exceptions.php @@ -29,11 +29,11 @@ class Mailchimp_Error extends Exception */ protected $_mailchimp_details; - public function __construct($title = "", $details = "", $errors=null) + public function __construct($url = "", $title = "", $details = "", $errors = null) { - parent::__construct($title . " : " . $details); - - $this->_mailchimp_title = $title; + $titleComplete = $title . " for Api Call: " . $url; + parent::__construct($titleComplete . " - " . $details); + $this->_mailchimp_title = $titleComplete; $this->_mailchimp_details = $details; $this->_mailchimp_errors = $errors; } @@ -45,9 +45,12 @@ public function getFriendlyMessage() { foreach($this->_mailchimp_errors as $error) { - if(array_key_exists("field",$error) && array_key_exists("message",$error)){ + if(array_key_exists("message",$error)){ $error_details .= $error_details != "" ? " / " : ""; - $error_details .= $error["field"] . " : " . $error["message"]; + if(array_key_exists("field", $error) && $error['field']){ + $error_details .= $error["field"] . " : "; + } + $error_details .= $error["message"]; } } } diff --git a/lib/Ebizmarts/Mailchimp/ListsMembers.php b/lib/Ebizmarts/Mailchimp/ListsMembers.php index 8eeba2b99..d08aa2090 100644 --- a/lib/Ebizmarts/Mailchimp/ListsMembers.php +++ b/lib/Ebizmarts/Mailchimp/ListsMembers.php @@ -29,6 +29,7 @@ class Mailchimp_ListsMembers extends Mailchimp_Abstract * @param $listId The unique id for the list. * @param null $emailType Type of email this member asked to get (‘html’ or ‘text’). * @param $status Subscriber’s current status. (subscribed | unsubscribed | cleaned | pending) + * @param $email_address Subscriber's email address. * @param null $mergeFields An individual merge var and value for a member. * @param null $interest The key of this object’s properties is the ID of the interest in question. * @param null $language If set/detected, the subscriber’s language. @@ -39,9 +40,9 @@ class Mailchimp_ListsMembers extends Mailchimp_Abstract * @throws Mailchimp_Error * @throws Mailchimp_HttpError */ - public function add($listId,$emailType=null,$status, $mergeFields=null,$interests=null,$language=null,$vip=null,$location=null,$ipOpt=null) + public function add($listId,$emailType=null,$status, $email_address, $mergeFields=null,$interests=null,$language=null,$vip=null,$location=null,$ipOpt=null) { - $_params = array('status'=>$status); + $_params = array('status'=>$status, 'email_address' => $email_address); if($emailType) $_params['email_type'] = $emailType; if($mergeFields) $_params['merge_fields'] = $mergeFields; if($interests) $_params['interests'] = $interests;