-
Notifications
You must be signed in to change notification settings - Fork 64
[A] Adding process schedulized of syncronization #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
72c3b08
[A] Adding process schedulized of syncronization
Oscar-Wagento c38e17c
Removing code commented, blocks commented, camelCode to some variables
Oscar-Wagento 4180b6b
[M] Change of label attribute for customer
Oscar-Wagento 259f2dd
[M] Code review and fixing spaces
Oscar-Wagento File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| 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); | ||
| } | ||
| } |
This file contains hidden or 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
| 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); | ||
| $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'); | ||
|
|
||
|
|
||
| } | ||
| } | ||
| } | ||
35 changes: 35 additions & 0 deletions
35
src/app/code/community/Zendesk/Zendesk/data/zendesk_setup/data-upgrade-2.0.6-2.1.13.php
This file contains hidden or 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
| 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(); |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)