Skip to content

Commit

Permalink
Changed declaration of arrays.
Browse files Browse the repository at this point in the history
Added better display on error messages for APICalls.
Updated module version.
  • Loading branch information
Santiagoebizmarts committed Jun 8, 2016
1 parent f1ebb44 commit 745e782
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 22 deletions.
6 changes: 3 additions & 3 deletions app/code/community/Ebizmarts/MailChimp/Model/Api/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 24 additions & 3 deletions app/code/community/Ebizmarts/MailChimp/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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());
}
Expand All @@ -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());
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/code/community/Ebizmarts/MailChimp/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<config>
<modules>
<Ebizmarts_MailChimp>
<version>1.0.0</version>
<version>1.0.1</version>
</Ebizmarts_MailChimp>
</modules>
<global>
Expand Down
9 changes: 2 additions & 7 deletions lib/Ebizmarts/Mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand All @@ -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;
Expand Down
15 changes: 9 additions & 6 deletions lib/Ebizmarts/Mailchimp/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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"];
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions lib/Ebizmarts/Mailchimp/ListsMembers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand Down

0 comments on commit 745e782

Please sign in to comment.