Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions src/app/code/community/Zendesk/Zendesk/Helper/Sync.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

class Zendesk_Zendesk_Helper_Sync extends Mage_Core_Helper_Abstract {

public function getCustomerData($customer){
if(!Mage::getStoreConfig('zendesk/general/customer_sync'))
return;

$user = null;
$email = $customer->getEmail();
$origEmail = $customer->getOrigData();
$origEmail = $origEmail['email'];
//Get Customer Group
$groupId = $customer->getGroupId();
$group = Mage::getModel('customer/group')->load($groupId);

//Get Customer Last Login Date
$logCustomer = Mage::getModel('log/customer')->loadByCustomer($customer);
if ($logCustomer->getLoginAt())
$loggedIn = date("Y-m-d\TH:i:s\Z",strtotime($logCustomer->getLoginAt()));
else
$loggedIn = "";

//Get Customer Sales Statistics
$orderTotals = Mage::getResourceModel('sales/order_collection');
$lifetimeSale = 0;
$averageSale = 0;

if (is_object($orderTotals)) {
$orderTotals
->addFieldToFilter('customer_id', $customer->getId())
->addFieldToFilter('status', Mage_Sales_Model_Order::STATE_COMPLETE);

$orderTotals->getSelect()
->reset(Zend_Db_Select::COLUMNS)
->columns(new Zend_Db_Expr("SUM(grand_total) as total"))
->columns(new Zend_Db_Expr("AVG(grand_total) as avg_total"))
->group('customer_id');

if (count($orderTotals) > 0) {
$sum = (float) $orderTotals->getFirstItem()->getTotal();
$avg = (float) $orderTotals->getFirstItem()->getAvgTotal();

$lifetimeSale = Mage::helper('core')->currency($sum, true, false);
$averageSale = Mage::helper('core')->currency($avg, true, false);
}
}

$info['user'] = array(
"name" => $customer->getFirstname() . " " . $customer->getLastname(),
"email" => $email,
"user_fields" => array(
"group" => $group->getCode(),
"name" => $customer->getFirstname() . " " . $customer->getLastname(),
"id" => $customer->getId(),
"logged_in" => $loggedIn,
"average_sale" => $averageSale,
"lifetime_sale" => $lifetimeSale
)
);

if($origEmail && $origEmail !== $email) {
$user = Mage::getModel('zendesk/api_users')->find($origEmail);

if(isset($user['id'])) {
$data['identity'] = array(
'type' => 'email',
'value' => $email,
'verified' => true
);
$identity = Mage::getModel('zendesk/api_users')->addIdentity($user['id'],$data);
if(isset($identity['id'])) {
Mage::getModel('zendesk/api_users')->setPrimaryIdentity($user['id'], $identity['id']);
}
}
}
if(!$user) {
$user = Mage::getModel('zendesk/api_users')->find($email);
}

if(isset($user['id'])) {
$this->syncData($info);
} else {
$info['user']['verified'] = true;
$user = Mage::getModel('zendesk/api_users')->create($info);
}
return $user;
}

private function syncData($info)
{
Mage::getModel('zendesk/api_users')->create($info);
}
}
32 changes: 32 additions & 0 deletions src/app/code/community/Zendesk/Zendesk/Model/Customer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

class Zendesk_Zendesk_Model_Customer extends Mage_Core_Model_Abstract{

public function syncronize(){
Mage::log('Cron Working', null, 'cron.log', true);
$customers = Mage::getModel('customer/customer')
->getCollection()->setPageSize(90)->setCurPage(1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would happen if a timeout is reached and not all customers have finished syncing? On the next cron run would the sync start from the beginning of the collection?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If an timeout is reached the syncronization will be made only with customers that doesn't have a zendesk_id (null), if the process is interrupted by any reason, then the syncronization will begin then with the first customer found with no zendesk_id. (zendesk_id = null)
It will not be necessary to start from zero this time.
If the process is interrupted will start again in 10 minutes (for example)

$customers->addAttributeToSelect(array('firstname', 'lastname', 'email'))
->addAttributeToFilter('zendesk_id', array('or'=> array(
0 => array('is' => new Zend_Db_Expr('null')))
), 'left');
foreach($customers as $customer){
Mage::log('Synchronization started', null, 'zendesk.log');
try {
Mage::log('Synchronizing customer with id '.$customer->getId(), null, 'zendesk.log');
$customerData = Mage::helper('zendesk/sync')->getCustomerData($customer);
$zendeskId = $customerData['id'];
$customer->setZendeskId($zendeskId);
$customer->save();
}
catch (Exception $ex) {
Mage::log('Synchronization failed: '.$ex->getMessage(), null, 'zendesk.log');

return;
}
Mage::log('Synchronization completed successfully', null, 'zendesk.log');


}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();
$installer->addAttribute("customer", "zendesk_id", array(
"type" => "varchar",
"backend" => "",
"label" => "Zendesk Id",
"input" => "text",
"source" => "",
"visible" => true,
"required" => false,
"default" => "",
"frontend" => "",
"unique" => false,
"note" => ""

));

$attribute = Mage::getSingleton("eav/config")->getAttribute("customer", "zendesk_id");
$used_in_forms=array();

$used_in_forms[]="adminhtml_customer";
$used_in_forms[]="checkout_register";
$used_in_forms[]="customer_account_create";
$used_in_forms[]="customer_account_edit";
$used_in_forms[]="adminhtml_checkout";
$attribute->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 1)
->setData("sort_order", 100);
$attribute->save();
$installer->endSetup();
13 changes: 13 additions & 0 deletions src/app/code/community/Zendesk/Zendesk/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@
</customer_login>
</events>
</frontend>
<crontab>
<jobs>
<zendesk_customer_sync>
<schedule>
<cron_expr>*/13 * * * * </cron_expr>
</schedule>

<run>
<model>zendesk/customer::syncronize</model>
</run>
</zendesk_customer_sync>
</jobs>
</crontab>
<admin>
<routers>
<adminhtml>
Expand Down