From 05c6098842de2612af3069b92809f30bfd36af6e Mon Sep 17 00:00:00 2001 From: gonzalo Date: Thu, 16 Mar 2017 12:53:11 -0300 Subject: [PATCH 001/105] change name of package --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 32fa1b7a..be3f807f 100644 --- a/composer.json +++ b/composer.json @@ -1,12 +1,12 @@ { - "name": "ebizmarts/mc-magento2", + "name": "mailchimp/mc-magento2", "autoload": { "psr-4": {"Ebizmarts\\MailChimp\\":""}, "files": ["registration.php"] }, "description": "Connect MailChimp with Magento", "type": "magento2-module", - "version": "1.0.0", + "version": "1.0.1", "authors": [ { "name": "Ebizmarts Corp", From cd63e65725e615265348f46db2f8ce11fffc3f16 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Fri, 17 Mar 2017 17:14:13 -0300 Subject: [PATCH 002/105] fix the visibility issue --- Model/Api/Product.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Model/Api/Product.php b/Model/Api/Product.php index 1d923851..e182b5ea 100644 --- a/Model/Api/Product.php +++ b/Model/Api/Product.php @@ -251,7 +251,7 @@ protected function _buildProductData(\Magento\Catalog\Model\Product $product, $ $data["visibility"] = 'true'; } else { - $data["visibility"] = false; + $data["visibility"] = 'false'; } } else { From e001804c942107b70a6e8bd47f80451ea51546ff Mon Sep 17 00:00:00 2001 From: gonzalo Date: Fri, 17 Mar 2017 17:14:43 -0300 Subject: [PATCH 003/105] fix the reset button --- Helper/Data.php | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/Helper/Data.php b/Helper/Data.php index cac5f789..32ec42b4 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -476,21 +476,7 @@ public function resetErrors() // clean the syncecommerce table with errors $connection = $this->_mailChimpSyncEcommerce->getResource()->getConnection(); $tableName = $this->_mailChimpSyncEcommerce->getResource()->getMainTable(); - $connection->delete($tableName,['mailchimp_sync_error is not null']); - // clean the errors in eav for products -// $productCollection = $this->_productCollection; -// $productCollection->addAttributeToFilter( -// array( -// array('attribute' => 'mailchimp_sync_error', 'neq' => '') -// ), '', 'left' -// ); -// foreach ($productCollection as $product) { -// $product->setData("mailchimp_sync_delta", null); -// $product->setData("mailchimp_sync_error", ''); -// $resource = $product->getResource(); -// $resource->saveAttribute($product, 'mailchimp_sync_delta'); -// $resource->saveAttribute($product, 'mailchimp_sync_error'); -// } + $connection->truncateTable($tableName); // clean the error in eav for customers } catch(\Zend_Db_Exception $e) { throw new ValidatorException(__($e->getMessage())); From f30c78ef86a4d6fda531b6ff7e18fb8fbef7d816 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Fri, 17 Mar 2017 17:15:19 -0300 Subject: [PATCH 004/105] update the version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index be3f807f..f1e75712 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ }, "description": "Connect MailChimp with Magento", "type": "magento2-module", - "version": "1.0.1", + "version": "1.0.2", "authors": [ { "name": "Ebizmarts Corp", From b9eecb3bb02b4303ce9508cf47866814a3effeac Mon Sep 17 00:00:00 2001 From: gonzalo Date: Sat, 18 Mar 2017 00:10:54 -0300 Subject: [PATCH 005/105] closes #4 --- Helper/Data.php | 15 +++++++-------- composer.json | 2 +- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/Helper/Data.php b/Helper/Data.php index 32ec42b4..77151669 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -90,9 +90,9 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper */ private $_mailChimpSyncEcommerce; /** - * @var \Magento\Catalog\Model\ResourceModel\Product\Collection + * @var \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce */ - private $_productCollection; + private $_mailChimpSyncE; /** * Data constructor. @@ -106,7 +106,7 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper * @param \Magento\Customer\Model\ResourceModel\CustomerRepository $customer * @param \Ebizmarts\MailChimp\Model\MailChimpErrors $mailChimpErrors * @param \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerceFactory $mailChimpSyncEcommerce - * @param \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection + * @param \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce $mailChimpSyncE */ public function __construct( \Magento\Framework\App\Helper\Context $context, @@ -119,7 +119,7 @@ public function __construct( \Magento\Customer\Model\ResourceModel\CustomerRepository $customer, \Ebizmarts\MailChimp\Model\MailChimpErrors $mailChimpErrors, \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerceFactory $mailChimpSyncEcommerce, - \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection + \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce $mailChimpSyncE ) { $this->_storeManager = $storeManager; @@ -133,7 +133,7 @@ public function __construct( $this->_customer = $customer; $this->_mailChimpErrors = $mailChimpErrors; $this->_mailChimpSyncEcommerce = $mailChimpSyncEcommerce; - $this->_productCollection = $productCollection; + $this->_mailChimpSyncE = $mailChimpSyncE; parent::__construct($context); } @@ -474,10 +474,9 @@ public function resetErrors() $tableName = $this->_mailChimpErrors->getResource()->getMainTable(); $connection->truncateTable($tableName); // clean the syncecommerce table with errors - $connection = $this->_mailChimpSyncEcommerce->getResource()->getConnection(); - $tableName = $this->_mailChimpSyncEcommerce->getResource()->getMainTable(); + $connection = $this->_mailChimpSyncE->getResource()->getConnection(); + $tableName = $this->_mailChimpSyncE->getResource()->getMainTable(); $connection->truncateTable($tableName); - // clean the error in eav for customers } catch(\Zend_Db_Exception $e) { throw new ValidatorException(__($e->getMessage())); } diff --git a/composer.json b/composer.json index f1e75712..6135cd5e 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ }, "description": "Connect MailChimp with Magento", "type": "magento2-module", - "version": "1.0.2", + "version": "1.0.3", "authors": [ { "name": "Ebizmarts Corp", From 6d3d2da46aedb6df09e0292d27cad613ae4c3f28 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Tue, 21 Mar 2017 23:18:41 -0300 Subject: [PATCH 006/105] closes #5 --- Setup/InstallSchema.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Setup/InstallSchema.php b/Setup/InstallSchema.php index c0435a83..d5244fa2 100644 --- a/Setup/InstallSchema.php +++ b/Setup/InstallSchema.php @@ -52,6 +52,13 @@ public function install(SchemaSetupInterface $setup, ModuleContextInterface $con ['unsigned' => true, 'nullable' => false], 'Store Id' ) + ->addColumn( + 'mailchimp_store_id', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 50, + ['unsigned' => true, 'nullable' => false], + 'Store Id' + ) ->addColumn( 'batch_id', \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, @@ -79,6 +86,13 @@ public function install(SchemaSetupInterface $setup, ModuleContextInterface $con ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true], 'Batch Id' ) + ->addColumn( + 'mailchimp_store_id', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 50, + ['unsigned' => true, 'nullable' => false], + 'Store Id' + ) ->addColumn( 'type', \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, From 2a511d2d3d46e1b660596328139ab9a58fc7c8ef Mon Sep 17 00:00:00 2001 From: gonzalo Date: Tue, 21 Mar 2017 23:19:42 -0300 Subject: [PATCH 007/105] closes #6 --- Block/Adminhtml/System/Config/ResetErrors.php | 22 ++++------ .../Adminhtml/Ecommerce/ResetLocalErrors.php | 44 ++++++++++++++++++- 2 files changed, 51 insertions(+), 15 deletions(-) diff --git a/Block/Adminhtml/System/Config/ResetErrors.php b/Block/Adminhtml/System/Config/ResetErrors.php index f3d7d62d..2779a844 100644 --- a/Block/Adminhtml/System/Config/ResetErrors.php +++ b/Block/Adminhtml/System/Config/ResetErrors.php @@ -70,21 +70,17 @@ public function getButtonHtml(\Magento\Framework\Data\Form\Element\AbstractEleme 'html_id' => $element->getHtmlId(), ]); return $this->_toHtml(); - - -// $button = $this->getLayout()->createBlock('adminhtml/widget_button') -// ->setData( -// array( -// 'id' => 'reseterrors_button', -// 'label' => $this->_helper->__('Reset Local Errors'), -// 'onclick' => 'javascript:reseterrors(); return false;' -// ) -// ); -// -// return $button->toHtml(); } public function getAjaxCheckUrl() { - return $this->_urlBuilder->getUrl('mailchimp/ecommerce/ResetLocalErrors'); + $params = $this->getRequest()->getParams(); + $scope = array(); + if(isset($params['website'])) { + $scope = array('website'=>$params['website']); + } + elseif(isset($params['store'])) { + $scope = array('store'=>$params['store']); + } + return $this->_urlBuilder->getUrl('mailchimp/ecommerce/ResetLocalErrors',$scope); } } \ No newline at end of file diff --git a/Controller/Adminhtml/Ecommerce/ResetLocalErrors.php b/Controller/Adminhtml/Ecommerce/ResetLocalErrors.php index b9de815b..49f82cd7 100644 --- a/Controller/Adminhtml/Ecommerce/ResetLocalErrors.php +++ b/Controller/Adminhtml/Ecommerce/ResetLocalErrors.php @@ -19,27 +19,67 @@ class ResetLocalErrors extends \Magento\Backend\App\Action { + /** + * @var JsonFactory + */ protected $resultJsonFactory; + /** + * @var \Ebizmarts\MailChimp\Helper\Data + */ protected $helper; + /** + * @var \Magento\Store\Model\StoreManagerInterface + */ + protected $storeManager; + /** + * ResetLocalErrors constructor. + * @param \Magento\Backend\App\Action\Context $context + * @param JsonFactory $resultJsonFactory + * @param \Magento\Store\Model\StoreManagerInterface $storeManagerInterface + * @param \Ebizmarts\MailChimp\Helper\Data $helper + */ public function __construct( \Magento\Backend\App\Action\Context $context, JsonFactory $resultJsonFactory, + \Magento\Store\Model\StoreManagerInterface $storeManagerInterface, \Ebizmarts\MailChimp\Helper\Data $helper ) { parent::__construct($context); - $this->resultJsonFactory = $resultJsonFactory; + $this->resultJsonFactory = $resultJsonFactory; $this->helper = $helper; + $this->storeManager = $storeManagerInterface; } public function execute() { $valid = 1; $message = ''; + $params = $this->getRequest()->getParams(); + if (isset($params['website'])) { + $mailchimpStore = $this->helper->getConfigValue( + \Ebizmarts\MailChimp\Helper\Data::XML_PATH_STORE, + $params['website'], + 'website' + ); + } elseif (isset($params['store'])) { + $mailchimpStore = $this->helper->getConfigValue( + \Ebizmarts\MailChimp\Helper\Data::XML_PATH_STORE, + $params['store'], + 'store' + ); + } else { + $mailchimpStore = $this->helper->getConfigValue( + \Ebizmarts\MailChimp\Helper\Data::XML_PATH_STORE, + $this->storeManager->getStore() + ); + } +// $this->helper->log($mailchimpStore); + $resultJson = $this->resultJsonFactory->create(); try { - $this->helper->resetErrors(); + $this->helper->resetErrors($mailchimpStore); } catch(ValidatorException $e) { $valid = 0; $message = $e->getMessage(); From dbcb2a1d8f5ceedaadb6b3c522e96bbc166a2eac Mon Sep 17 00:00:00 2001 From: gonzalo Date: Tue, 21 Mar 2017 23:21:45 -0300 Subject: [PATCH 008/105] closes #5 --- Helper/Data.php | 97 +++++++++++++++++++++++----------------- Model/Api/Result.php | 102 ++++++++----------------------------------- 2 files changed, 73 insertions(+), 126 deletions(-) diff --git a/Helper/Data.php b/Helper/Data.php index 77151669..c45a8e90 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -181,46 +181,57 @@ public function getApi($store = null) * @return mixed * @throws \Magento\Framework\Exception\LocalizedException */ - public function getConfigValue($path, $storeId = null) + public function getConfigValue($path, $storeId = null, $scope = null) { $areaCode = $this->_state->getAreaCode(); - if ($storeId !== null) { - $configValue = $this->scopeConfig->getValue( - $path, - \Magento\Store\Model\ScopeInterface::SCOPE_STORE, - $storeId - ); - } elseif ($areaCode == 'frontend') { - $frontStoreId = $this->_storeManager->getStore()->getId(); - $configValue = $this->scopeConfig->getValue( - $path, - \Magento\Store\Model\ScopeInterface::SCOPE_STORE, - $frontStoreId - ); - } else { - $storeId = $this->_request->getParam(\Magento\Store\Model\ScopeInterface::SCOPE_STORE); - $websiteId = $this->_request->getParam(\Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE); - if (!empty($storeId)) { - $configValue = $this->scopeConfig->getValue( - $path, - \Magento\Store\Model\ScopeInterface::SCOPE_STORE, - $storeId - ); - } elseif (!empty($websiteId)) { - $configValue = $this->scopeConfig->getValue( - $path, - \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE, - $websiteId - ); - } else { - $configValue = $this->scopeConfig->getValue( - $path, - \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE, - 0 - ); - } + switch ($scope) { + case 'website': + $value = $this->_scopeConfig->getValue($path ,\Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE, $storeId); + break; + default: + $value = $this->_scopeConfig->getValue($path ,\Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId); + break; } - return $configValue; + return $value; + + +// if ($storeId !== null) { +// $configValue = $this->scopeConfig->getValue( +// $path, +// \Magento\Store\Model\ScopeInterface::SCOPE_STORE, +// $storeId +// ); +// } elseif ($areaCode == 'frontend') { +// $frontStoreId = $this->_storeManager->getStore()->getId(); +// $configValue = $this->scopeConfig->getValue( +// $path, +// \Magento\Store\Model\ScopeInterface::SCOPE_STORE, +// $frontStoreId +// ); +// } else { +// $storeId = $this->_request->getParam(\Magento\Store\Model\ScopeInterface::SCOPE_STORE); +// $websiteId = $this->_request->getParam(\Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE); +// if (!empty($storeId)) { +// $configValue = $this->scopeConfig->getValue( +// $path, +// \Magento\Store\Model\ScopeInterface::SCOPE_STORE, +// $storeId +// ); +// } elseif (!empty($websiteId)) { +// $configValue = $this->scopeConfig->getValue( +// $path, +// \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE, +// $websiteId +// ); +// } else { +// $configValue = $this->scopeConfig->getValue( +// $path, +// \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE, +// 0 +// ); +// } +// } +// return $configValue; } /** @@ -466,17 +477,19 @@ public function getDateMicrotime() $date = date('Y-m-d-H-i-s') . '-' . $msecArray[1]; return $date; } - public function resetErrors() + public function resetErrors($mailchimpStore) { try { // clean the errors table $connection = $this->_mailChimpErrors->getResource()->getConnection(); $tableName = $this->_mailChimpErrors->getResource()->getMainTable(); - $connection->truncateTable($tableName); + $connection->delete($tableName, "mailchimp_store_id = '".$mailchimpStore."'"); +// $connection->truncateTable($tableName); // clean the syncecommerce table with errors $connection = $this->_mailChimpSyncE->getResource()->getConnection(); $tableName = $this->_mailChimpSyncE->getResource()->getMainTable(); - $connection->truncateTable($tableName); + $connection->delete($tableName, "mailchimp_store_id = '".$mailchimpStore."' and mailchimp_sync_error is not null"); +// $connection->truncateTable($tableName); } catch(\Zend_Db_Exception $e) { throw new ValidatorException(__($e->getMessage())); } @@ -503,6 +516,8 @@ public function getChimpSyncEcommerce($storeId,$id,$type) $chimp = $this->_mailChimpSyncEcommerce->create(); return $chimp->getByStoreIdType($storeId,$id,$type); } - + public function getScope() + { + } } diff --git a/Model/Api/Result.php b/Model/Api/Result.php index a600830b..7f3e813d 100644 --- a/Model/Api/Result.php +++ b/Model/Api/Result.php @@ -29,54 +29,30 @@ class Result */ private $_archive; /** - * @var \Ebizmarts\MailChimp\Model\MailChimpErrors + * @var \Ebizmarts\MailChimp\Model\MailChimpErrorsFactory */ private $_chimpErrors; - /** - * @var \Magento\Catalog\Model\ProductRepository - */ - private $_productRepository; - /** - * @var \Magento\Customer\Api\CustomerRepositoryInterface - */ - private $_customerRepository; - /** - * @var \Magento\Sales\Model\OrderRepository - */ - private $_orderRepository; - - private $_chimpSyncEcommerce; - /** * Result constructor. * @param \Ebizmarts\MailChimp\Helper\Data $helper * @param \Ebizmarts\MailChimp\Model\ResourceModel\MailChimpSyncBatches\Collection $batchCollection - * @param \Ebizmarts\MailChimp\Model\MailChimpErrors $chimpErrors + * @param \Ebizmarts\MailChimp\Model\MailChimpErrorsFactory $chimpErrors * @param \Magento\Framework\Archive $archive - * @param \Magento\Catalog\Model\ProductRepository $productRepository */ public function __construct( \Ebizmarts\MailChimp\Helper\Data $helper, \Ebizmarts\MailChimp\Model\ResourceModel\MailChimpSyncBatches\Collection $batchCollection, - \Ebizmarts\MailChimp\Model\MailChimpErrors $chimpErrors, - \Magento\Framework\Archive $archive, - \Magento\Catalog\Model\ProductRepository $productRepository, - \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository, - \Magento\Sales\Model\OrderRepository $orderRepository, - \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce $chimpSyncEcommerce + \Ebizmarts\MailChimp\Model\MailChimpErrorsFactory $chimpErrors, + \Magento\Framework\Archive $archive ) { $this->_batchCollection = $batchCollection; $this->_helper = $helper; $this->_archive = $archive; $this->_chimpErrors = $chimpErrors; - $this->_productRepository = $productRepository; - $this->_customerRepository = $customerRepository; - $this->_orderRepository = $orderRepository; - $this->_chimpSyncEcommerce = $chimpSyncEcommerce; } - public function processResponses($storeId, $isMailChimpStoreId = false) + public function processResponses($storeId, $isMailChimpStoreId = false, $mailchimpStoreId) { $collection = $this->_batchCollection; $collection @@ -91,7 +67,7 @@ public function processResponses($storeId, $isMailChimpStoreId = false) $storeId = ($isMailChimpStoreId) ? 0 : $storeId; $files = $this->getBatchResponse($item->getBatchId(), $storeId); if (count($files)) { - $this->processEachResponseFile($files, $item->getBatchId()); + $this->processEachResponseFile($files, $item->getBatchId(), $mailchimpStoreId, $storeId); $item->setStatus('completed'); $item->getResource()->save($item); } @@ -145,7 +121,7 @@ protected function getBatchResponse($batchId, $storeId = 0) } return $files; } - protected function processEachResponseFile($files, $batchId) + protected function processEachResponseFile($files, $batchId, $mailchimpStoreId , $storeId) { foreach ($files as $file) { $items = json_decode(file_get_contents($file)); @@ -155,10 +131,12 @@ protected function processEachResponseFile($files, $batchId) $type = $line[0]; $id = $line[2]; - $mailchimpErrors = $this->_chimpErrors; + $mailchimpErrors = $this->_chimpErrors->create(); //parse error $response = json_decode($item->response); + $this->_helper->log($response); + $this->_helper->log("type [$type] id [$id]"); $errorDetails = ""; if (!empty($response->errors)) { foreach ($response->errors as $error) { @@ -173,59 +151,12 @@ protected function processEachResponseFile($files, $batchId) } $error = $response->title . " : " . $response->detail; - - switch ($type) { - case \Ebizmarts\MailChimp\Helper\Data::IS_PRODUCT: - $p = $this->_productRepository->getById($id); - if ($p->getId() == $id) { - $p->setData("mailchimp_sync_error", $error); - //$p->setMailchimpUpdateObserverRan(true); - $this->_productRepository->save($p); - } else { - $this->_helper->log("Error: product " . $id . " not found"); - } - break; - case \Ebizmarts\MailChimp\Helper\Data::IS_CUSTOMER: - $c = $this->_customerRepository->getById($id); - if ($c->getId() == $id) { -// $c->setCustomAttribute("mailchimp_sync_error", $error); - $this->_customerRepository->save($c); - } else { - $this->_helper->log("Error: customer " . $id . " not found"); - } - break; - case \Ebizmarts\MailChimp\Helper\Data::IS_ORDER: - $o = $this->_orderRepository->get($id); - if ($o->getId() == $id) { - $c = $this->_chimpSyncEcommerce->getByStoreIdType($o->getStoreId(),$id,$type); - $c->setData("mailchimp_sync_error", $error); - $c->getResource()->save($c); - } else { - $this->_helper->log("Error: order " . $id . " not found"); - } - break; -// case \Ebizmarts\MailChimp\Helper\Data::IS_QUOTE: -// $q = Mage::getModel('sales/quote')->load($id); -// if ($q->getId() == $id) { -// $q->setData("mailchimp_sync_error", $error); -// $q->save(); -// } else { -// $this->_helper->log("Error: quote " . $id . " not found"); -// } -// break; -// case \Ebizmarts\MailChimp\Helper\Data::IS_SUBSCRIBER: -// $s = Mage::getModel('newsletter/subscriber')->load($id); -// if ($s->getId() == $id) { -// $s->setData("mailchimp_sync_error", $error); -// $s->save(); -// } else { -// $this->_helper->log("Error: subscriber " . $id . " not found"); -// } -// break; - default: - $this->_helper->log("Error: no identification " . $type . " found"); - break; - } + /** + * @var \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce $chimpSync + */ + $chimpSync = $this->_helper->getChimpSyncEcommerce($storeId,$id,$type); + $chimpSync->setData("mailchimp_sync_error", $error); + $chimpSync->getResource()->save($chimpSync); $mailchimpErrors->setType($response->type); $mailchimpErrors->setTitle($response->title); $mailchimpErrors->setStatus($item->status_code); @@ -233,6 +164,7 @@ protected function processEachResponseFile($files, $batchId) $mailchimpErrors->setRegtype($type); $mailchimpErrors->setOriginalId($id); $mailchimpErrors->setBatchId($batchId); + $mailchimpErrors->setMailchimpStoreId($mailchimpStoreId); $mailchimpErrors->getResource()->save($mailchimpErrors); } } From b8cbd228a6ec4fd7bb86bc2b201f3f6e2ea0d0fb Mon Sep 17 00:00:00 2001 From: gonzalo Date: Wed, 22 Mar 2017 10:47:19 -0300 Subject: [PATCH 009/105] closes #9 --- .../layout/mailchimp_errors_grid.xml | 8 +++++ .../layout/mailchimp_errors_index.xml | 1 - .../ui_component/mailchimp_errors_grid.xml | 34 +++++++++++++++++-- 3 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 view/adminhtml/layout/mailchimp_errors_grid.xml diff --git a/view/adminhtml/layout/mailchimp_errors_grid.xml b/view/adminhtml/layout/mailchimp_errors_grid.xml new file mode 100644 index 00000000..bc86cafb --- /dev/null +++ b/view/adminhtml/layout/mailchimp_errors_grid.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/view/adminhtml/layout/mailchimp_errors_index.xml b/view/adminhtml/layout/mailchimp_errors_index.xml index 40a3a09d..83d43126 100644 --- a/view/adminhtml/layout/mailchimp_errors_index.xml +++ b/view/adminhtml/layout/mailchimp_errors_index.xml @@ -7,4 +7,3 @@ - diff --git a/view/adminhtml/ui_component/mailchimp_errors_grid.xml b/view/adminhtml/ui_component/mailchimp_errors_grid.xml index d9a1912a..084d235b 100644 --- a/view/adminhtml/ui_component/mailchimp_errors_grid.xml +++ b/view/adminhtml/ui_component/mailchimp_errors_grid.xml @@ -20,13 +20,41 @@ Magento_Ui/js/grid/provider - - id - + + + + + + + + true + + + + + + + + Magento\Store\Ui\Component\Listing\Column\Store\Options + + + + ${ $.parentName } + store_id + All Store Views + Store View + + + + + + + + From a336de75d9138d5633b0248b54663690c332069a Mon Sep 17 00:00:00 2001 From: gonzalo Date: Wed, 22 Mar 2017 10:48:02 -0300 Subject: [PATCH 010/105] change version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 6135cd5e..c2e61430 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ }, "description": "Connect MailChimp with Magento", "type": "magento2-module", - "version": "1.0.3", + "version": "1.0.4", "authors": [ { "name": "Ebizmarts Corp", From d7552ae8157449674c559679d8d91ca117623f53 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Fri, 21 Apr 2017 14:41:14 -0300 Subject: [PATCH 011/105] add stores --- Model/ResourceModel/MailChimpStores.php | 26 +++++++++++++++++++ .../MailChimpStores/Collection.php | 24 +++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 Model/ResourceModel/MailChimpStores.php create mode 100644 Model/ResourceModel/MailChimpStores/Collection.php diff --git a/Model/ResourceModel/MailChimpStores.php b/Model/ResourceModel/MailChimpStores.php new file mode 100644 index 00000000..b783b802 --- /dev/null +++ b/Model/ResourceModel/MailChimpStores.php @@ -0,0 +1,26 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 4/3/17 11:42 AM + * @file: MailChimpStores.php + */ + +namespace Ebizmarts\MailChimp\Model\ResourceModel; + +use Magento\Framework\DB\Select; +use Magento\Framework\Model\ResourceModel\Db\AbstractDb; + +class MailChimpStores extends AbstractDb +{ + protected function _construct() + { + $this->_init('mailchimp_stores', 'id'); + } + +} \ No newline at end of file diff --git a/Model/ResourceModel/MailChimpStores/Collection.php b/Model/ResourceModel/MailChimpStores/Collection.php new file mode 100644 index 00000000..cd293ae6 --- /dev/null +++ b/Model/ResourceModel/MailChimpStores/Collection.php @@ -0,0 +1,24 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 3/27/17 1:22 PM + * @file: Collection.php + */ +namespace Ebizmarts\MailChimp\Model\ResourceModel\MailChimpStores; + +class Collection extends \Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult +{ + protected function _construct() + { + $this->_init( + 'Ebizmarts\MailChimp\Model\MailChimpStores', + 'Ebizmarts\MailChimp\Model\ResourceModel\MailChimpStores' + ); + } +} \ No newline at end of file From d5d9b94bd84097c374c8682654772ebe5e5918ad Mon Sep 17 00:00:00 2001 From: gonzalo Date: Fri, 21 Apr 2017 14:41:40 -0300 Subject: [PATCH 012/105] add stores --- Model/MailChimpStores.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Model/MailChimpStores.php diff --git a/Model/MailChimpStores.php b/Model/MailChimpStores.php new file mode 100644 index 00000000..12f64bad --- /dev/null +++ b/Model/MailChimpStores.php @@ -0,0 +1,22 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 10/21/16 4:55 PM + * @file: MailChimpErrors.php + */ + +namespace Ebizmarts\MailChimp\Model; + +class MailChimpStores extends \Magento\Framework\Model\AbstractModel +{ + protected function _construct() + { + $this->_init('\Ebizmarts\MailChimp\Model\ResourceModel\MailChimpStores'); + } +} \ No newline at end of file From 8904790105e6d11d578bdec56738519f959b4141 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Fri, 21 Apr 2017 14:42:03 -0300 Subject: [PATCH 013/105] add stores --- Setup/InstallSchema.php | 2 + Setup/UpgradeSchema.php | 160 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+) diff --git a/Setup/InstallSchema.php b/Setup/InstallSchema.php index d5244fa2..f88984f1 100644 --- a/Setup/InstallSchema.php +++ b/Setup/InstallSchema.php @@ -199,6 +199,8 @@ public function install(SchemaSetupInterface $setup, ModuleContextInterface $con $installer->getConnection()->createTable($table); + + $connection->addColumn( $installer->getTable('newsletter_subscriber'), 'mailchimp_id', diff --git a/Setup/UpgradeSchema.php b/Setup/UpgradeSchema.php index 1b4113be..4309be13 100644 --- a/Setup/UpgradeSchema.php +++ b/Setup/UpgradeSchema.php @@ -22,5 +22,165 @@ class UpgradeSchema implements UpgradeSchemaInterface */ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context) { + $installer = $setup; + $installer->startSetup(); + if (version_compare($context->getVersion(), '1.0.5') < 0) { + $table = $installer->getConnection() + ->newTable($installer->getTable('mailchimp_stores')) + ->addColumn( + 'id', + \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, + null, + ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true], + 'Id' + ) + ->addColumn( + 'apikey', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 50, + ['unsigned' => true, 'nullable' => false], + 'mailchimp apikey' + ) + ->addColumn( + 'storeid', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 50, + ['unsigned' => true, 'nullable' => false], + 'mailchimp store id' + ) + ->addColumn( + 'list_id', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 50, + ['unsigned' => true, 'nullable' => false], + 'mailchimp store id' + ) + ->addColumn( + 'name', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 128, + ['unsigned' => true, 'nullable' => false], + 'store name' + ) + ->addColumn( + 'platform', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 50, + ['unsigned' => true, 'nullable' => false], + 'store platform' + ) + ->addColumn( + 'is_sync', + \Magento\Framework\DB\Ddl\Table::TYPE_BOOLEAN, + null, + [], + 'if the store is synced or not' + ) + ->addColumn( + 'email_address', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 128, + ['unsigned' => true, 'nullable' => false], + 'email associated to store' + ) + ->addColumn( + 'currency_code', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 3, + ['unsigned' => true, 'nullable' => false], + 'store currency code' + ) + ->addColumn( + 'money_format', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 10, + ['unsigned' => true, 'nullable' => false], + 'symbol of currency' + ) + ->addColumn( + 'primary_locale', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 5, + ['unsigned' => true, 'nullable' => false], + 'store locale' + ) + ->addColumn( + 'timezone', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 20, + ['unsigned' => true, 'nullable' => false], + 'store timezone' + ) + ->addColumn( + 'phone', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 50, + ['unsigned' => true, 'nullable' => false], + 'store phone number' + ) + ->addColumn( + 'address_address1', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 50, + ['unsigned' => true, 'nullable' => false], + 'store address1' + ) + ->addColumn( + 'address_address2', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 50, + ['unsigned' => true, 'nullable' => false], + 'store address2' + ) + ->addColumn( + 'address_city', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 50, + ['unsigned' => true, 'nullable' => false], + 'store city' + ) + ->addColumn( + 'address_province', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 50, + ['unsigned' => true, 'nullable' => false], + 'store province' + ) + ->addColumn( + 'address_province_code', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 2, + ['unsigned' => true, 'nullable' => false], + 'store province code' + ) + ->addColumn( + 'address_postal_code', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 50, + ['unsigned' => true, 'nullable' => false], + 'store postal code' + ) + ->addColumn( + 'address_country', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 50, + ['unsigned' => true, 'nullable' => false], + 'store country name' + ) + ->addColumn( + 'address_country_code', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 2, + ['unsigned' => true, 'nullable' => false], + 'store country code' + ); + + $installer->getConnection()->createTable($table); + } + $installer->endSetup(); + + + + } } \ No newline at end of file From 6f25fdb1d22df5d6b8f2d3fd85581eabb017fba4 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Fri, 21 Apr 2017 14:42:34 -0300 Subject: [PATCH 014/105] add stores management --- .../layout/mailchimp_stores_edit.xml | 22 ++++ ...ors_grid.xml => mailchimp_stores_grid.xml} | 2 +- .../layout/mailchimp_stores_index.xml | 20 +++ view/adminhtml/requirejs-config.js | 17 +++ .../templates/system/config/deletestore.phtml | 32 +++++ .../templates/system/config/js.phtml | 3 + .../templates/system/config/resetstore.phtml | 32 +++++ .../ui_component/mailchimp_errors_grid.xml | 11 ++ .../ui_component/mailchimp_stores_grid.xml | 114 ++++++++++++++++++ view/adminhtml/web/css/styles.css | 4 + view/adminhtml/web/js/apikey.js | 49 ++++++++ 11 files changed, 305 insertions(+), 1 deletion(-) create mode 100644 view/adminhtml/layout/mailchimp_stores_edit.xml rename view/adminhtml/layout/{mailchimp_errors_grid.xml => mailchimp_stores_grid.xml} (89%) create mode 100644 view/adminhtml/layout/mailchimp_stores_index.xml create mode 100644 view/adminhtml/requirejs-config.js create mode 100644 view/adminhtml/templates/system/config/deletestore.phtml create mode 100644 view/adminhtml/templates/system/config/js.phtml create mode 100644 view/adminhtml/templates/system/config/resetstore.phtml create mode 100644 view/adminhtml/ui_component/mailchimp_stores_grid.xml create mode 100644 view/adminhtml/web/js/apikey.js diff --git a/view/adminhtml/layout/mailchimp_stores_edit.xml b/view/adminhtml/layout/mailchimp_stores_edit.xml new file mode 100644 index 00000000..394e9734 --- /dev/null +++ b/view/adminhtml/layout/mailchimp_stores_edit.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + diff --git a/view/adminhtml/layout/mailchimp_errors_grid.xml b/view/adminhtml/layout/mailchimp_stores_grid.xml similarity index 89% rename from view/adminhtml/layout/mailchimp_errors_grid.xml rename to view/adminhtml/layout/mailchimp_stores_grid.xml index bc86cafb..f186bdf2 100644 --- a/view/adminhtml/layout/mailchimp_errors_grid.xml +++ b/view/adminhtml/layout/mailchimp_stores_grid.xml @@ -1,7 +1,7 @@ - + diff --git a/view/adminhtml/layout/mailchimp_stores_index.xml b/view/adminhtml/layout/mailchimp_stores_index.xml new file mode 100644 index 00000000..51684947 --- /dev/null +++ b/view/adminhtml/layout/mailchimp_stores_index.xml @@ -0,0 +1,20 @@ + + + + + + + Ebizmarts_MailChimp::stores + + + + + complex + + + + + + + + diff --git a/view/adminhtml/requirejs-config.js b/view/adminhtml/requirejs-config.js new file mode 100644 index 00000000..07315750 --- /dev/null +++ b/view/adminhtml/requirejs-config.js @@ -0,0 +1,17 @@ +/** + * Ebizmarts_MailChimp Magento JS component + * + * @category Ebizmarts + * @package Ebizmarts_MailChimp + * @author Ebizmarts Team + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + */ + +var config = { + "map": { + "*": { + monkeyapikey: 'Ebizmarts_MailChimp/js/apikey' + } + } +}; \ No newline at end of file diff --git a/view/adminhtml/templates/system/config/deletestore.phtml b/view/adminhtml/templates/system/config/deletestore.phtml new file mode 100644 index 00000000..634f421a --- /dev/null +++ b/view/adminhtml/templates/system/config/deletestore.phtml @@ -0,0 +1,32 @@ + + + +escapeHtml('Reset errors')); ?> + + +
+ + +
diff --git a/view/adminhtml/templates/system/config/js.phtml b/view/adminhtml/templates/system/config/js.phtml new file mode 100644 index 00000000..f52bbfde --- /dev/null +++ b/view/adminhtml/templates/system/config/js.phtml @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/view/adminhtml/templates/system/config/resetstore.phtml b/view/adminhtml/templates/system/config/resetstore.phtml new file mode 100644 index 00000000..2988d46c --- /dev/null +++ b/view/adminhtml/templates/system/config/resetstore.phtml @@ -0,0 +1,32 @@ + + + +escapeHtml('Reset errors')); ?> + + +
+ + +
diff --git a/view/adminhtml/ui_component/mailchimp_errors_grid.xml b/view/adminhtml/ui_component/mailchimp_errors_grid.xml index 084d235b..5e95bb5c 100644 --- a/view/adminhtml/ui_component/mailchimp_errors_grid.xml +++ b/view/adminhtml/ui_component/mailchimp_errors_grid.xml @@ -8,6 +8,8 @@
mailchimp_errors_columns + + @@ -28,6 +30,15 @@ + + + + ui/grid/toolbar + ui/grid/sticky/toolbar + + + + diff --git a/view/adminhtml/ui_component/mailchimp_stores_grid.xml b/view/adminhtml/ui_component/mailchimp_stores_grid.xml new file mode 100644 index 00000000..0d1b148d --- /dev/null +++ b/view/adminhtml/ui_component/mailchimp_stores_grid.xml @@ -0,0 +1,114 @@ + ++ + + mailchimp_stores_grid.mailchimp_stores_grid_data_source + mailchimp_stores_grid.mailchimp_stores_grid_data_source + + + mailchimp_stores_columns + + + add + Create New Mailchimp Store + primary + mailchimp/stores/edit + + + + + + + Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider + mailchimp_stores_grid_data_source + id + id + + + Magento_Ui/js/grid/provider + + + + + + + + + + + + + ui/grid/toolbar + ui/grid/sticky/toolbar + + + + + + + true + + + + + + + + Magento\Store\Ui\Component\Listing\Column\Store\Options + + + + ${ $.parentName } + store_id + All Store Views + Store View + + + + + + + + + + + + + text + asc + API Key + 20 + + + + + + + text + asc + Store name + 30 + + + + + + + text + asc + Email + 40 + + + + + + + id + + + + + + + diff --git a/view/adminhtml/web/css/styles.css b/view/adminhtml/web/css/styles.css index 067f5892..cab6882a 100644 --- a/view/adminhtml/web/css/styles.css +++ b/view/adminhtml/web/css/styles.css @@ -11,4 +11,8 @@ .admin__menu .item-mailchimp-menu.parent.level-0 > a:before { font-family: 'mailchimp'; content: "\e990" +} + +.admin__data-grid-wrap .data-grid .data-grid-draggable .data-row .data-grid-thumbnail-cell .admin__control-thumbnail > img:before { + border:none; } \ No newline at end of file diff --git a/view/adminhtml/web/js/apikey.js b/view/adminhtml/web/js/apikey.js new file mode 100644 index 00000000..2b4c2c11 --- /dev/null +++ b/view/adminhtml/web/js/apikey.js @@ -0,0 +1,49 @@ +/** + * Ebizmarts_MailChimp Magento JS component + * + * @category Ebizmarts + * @package Ebizmarts_MailChimp + * @author Ebizmarts Team + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + */ +define([ + 'jquery' +], + function($){ + "use strict"; + + $.widget('mage.monkeyapikey', { + "options": { + "apikeyUrl": "hola que tal" + }, + + _init: function () { + var apiUrl = this.options.apikeyUrl; + $('#stores_apikey').change(function() { + // remove all items in list combo + $('#stores_list_id').empty(); + // get the selected apikey + var apiKey = $('#stores_apikey').find(':selected').text(); + // get the list for this apikey via ajax + //var apiUrl = this.options.apikeyUrl; + $.ajax({ + url: apiUrl, + data: {'form_key': window.FORM_KEY, 'apikey': apiKey}, + type: 'POST', + dataType: 'json', + showLoader: true + } + ).done(function(data) { + $.each(data, function (i, item) { + $('#stores_list_id').append($(' + + + mailchimp_stores + Ebizmarts\MailChimp\Model\ResourceModel\MailChimpStores + + + + + + Ebizmarts\MailChimp\Model\ResourceModel\MailChimpStores\Collection + + + diff --git a/etc/module.xml b/etc/module.xml index f1ec7eb1..a889680e 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -11,7 +11,7 @@ */ --> - + From bbe27483f6f60305f0900fa06a3031ab6a9681dc Mon Sep 17 00:00:00 2001 From: gonzalo Date: Fri, 21 Apr 2017 15:09:54 -0300 Subject: [PATCH 018/105] add stores functions --- Helper/Data.php | 104 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 99 insertions(+), 5 deletions(-) diff --git a/Helper/Data.php b/Helper/Data.php index c45a8e90..30a1994b 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -19,10 +19,12 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper { const XML_PATH_ACTIVE = 'mailchimp/general/active'; const XML_PATH_APIKEY = 'mailchimp/general/apikey'; + const XML_PATH_APIKEY_LIST = 'mailchimp/general/apikeylist'; const XML_PATH_MAXLISTAMOUNT = 'mailchimp/general/maxlistamount'; const XML_PATH_LIST = 'mailchimp/general/list'; const XML_PATH_LOG = 'mailchimp/general/log'; const XML_PATH_MAPPING = 'mailchimp/general/mapping'; + const XML_MAILCHIMP_STORE = 'mailchimp/general/monkeystore'; const XML_PATH_CONFIRMATION_FLAG = 'newsletter/subscription/confirm'; const XML_PATH_STORE = 'mailchimp/ecommerce/store'; const XML_PATH_ECOMMERCE_ACTIVE = 'mailchimp/ecommerce/active'; @@ -44,6 +46,9 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper const IS_QUOTE = "QUO"; const IS_SUBSCRIBER = "SUB"; + const PLATFORM = 'Magento2'; + const MAXSTORES = 100; + /** * @var \Magento\Store\Model\StoreManagerInterface */ @@ -72,6 +77,10 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper * @var \Magento\Framework\Module\ModuleList\Loader */ private $_loader; + /** + * @var \Magento\Config\Model\ResourceModel\Config + */ + private $_config; /** * @var \Mailchimp */ @@ -93,6 +102,18 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper * @var \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce */ private $_mailChimpSyncE; + /** + * @var \Ebizmarts\MailChimp\Model\MailChimpSyncBatches + */ + private $_syncBatches; + /** + * @var \Ebizmarts\MailChimp\Model\MailChimpStoresFactory + */ + private $_mailChimpStoresFactory; + /** + * @var \Ebizmarts\MailChimp\Model\MailChimpStores + */ + private $_mailChimpStores; /** * Data constructor. @@ -102,11 +123,15 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper * @param \Magento\Customer\Model\GroupRegistry $groupRegistry * @param \Magento\Framework\App\State $state * @param \Magento\Framework\Module\ModuleList\Loader $loader + * @param \Magento\Config\Model\ResourceModel\Config $config * @param \Mailchimp $api * @param \Magento\Customer\Model\ResourceModel\CustomerRepository $customer * @param \Ebizmarts\MailChimp\Model\MailChimpErrors $mailChimpErrors * @param \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerceFactory $mailChimpSyncEcommerce * @param \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce $mailChimpSyncE + * @param \Ebizmarts\MailChimp\Model\MailChimpSyncBatches $syncBatches + * @param \Ebizmarts\MailChimp\Model\MailChimpStoresFactory $mailChimpStoresFactory + * @param \Ebizmarts\MailChimp\Model\MailChimpStores $mailChimpStores */ public function __construct( \Magento\Framework\App\Helper\Context $context, @@ -115,11 +140,15 @@ public function __construct( \Magento\Customer\Model\GroupRegistry $groupRegistry, \Magento\Framework\App\State $state, \Magento\Framework\Module\ModuleList\Loader $loader, + \Magento\Config\Model\ResourceModel\Config $config, \Mailchimp $api, \Magento\Customer\Model\ResourceModel\CustomerRepository $customer, \Ebizmarts\MailChimp\Model\MailChimpErrors $mailChimpErrors, \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerceFactory $mailChimpSyncEcommerce, - \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce $mailChimpSyncE + \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce $mailChimpSyncE, + \Ebizmarts\MailChimp\Model\MailChimpSyncBatches $syncBatches, + \Ebizmarts\MailChimp\Model\MailChimpStoresFactory $mailChimpStoresFactory, + \Ebizmarts\MailChimp\Model\MailChimpStores $mailChimpStores ) { $this->_storeManager = $storeManager; @@ -129,11 +158,15 @@ public function __construct( $this->_request = $context->getRequest(); $this->_state = $state; $this->_loader = $loader; + $this->_config = $config; $this->_api = $api; $this->_customer = $customer; $this->_mailChimpErrors = $mailChimpErrors; $this->_mailChimpSyncEcommerce = $mailChimpSyncEcommerce; $this->_mailChimpSyncE = $mailChimpSyncE; + $this->_syncBatches = $syncBatches; + $this->_mailChimpStores = $mailChimpStores; + $this->_mailChimpStoresFactory = $mailChimpStoresFactory; parent::__construct($context); } @@ -175,6 +208,17 @@ public function getApi($store = null) return $this->_api; } + /** + * @param $apiKey + * @return \Mailchimp + */ + public function getApiByApiKey($apiKey) + { + $this->_api->setApiKey($apiKey); + $this->_api->setUserAgent('Mailchimp4Magento' . (string)$this->getModuleVersion()); + return $this->_api; + } + /** * @param $path * @param null $storeId @@ -275,16 +319,26 @@ public function getModuleVersion() } return $v; } - public function deleteStore() + public function deleteStore($mailchimpStore) { try { - $storeId = $this->getConfigValue(self::XML_PATH_STORE); - $this->getApi()->ecommerce->stores->delete($storeId); +// $storeId = $this->getConfigValue(self::XML_MAILCHIMP_STORE); + $this->getApi()->ecommerce->stores->delete($mailchimpStore); + $this->markAllBatchesAs($mailchimpStore, 'canceled'); + $connection = $this->_syncBatches->getResource()->getConnection(); + $tableName = $this->_syncBatches->getResource()->getMainTable(); + $connection->update($tableName, ['status' => 'canceled'], "mailchimp_store_id = '".$mailchimpStore."'"); } catch (Exception $e) { } } + public function markAllBatchesAs($mailchimpStore, $status) + { + $connection = $this->_syncBatches->getResource()->getConnection(); + $tableName = $this->_syncBatches->getResource()->getMainTable(); + $connection->update($tableName, ['status' => $status], "mailchimp_store_id = '".$mailchimpStore."'"); + } public function getMCStoreName($storeId) { return $this->_storeManager->getStore($storeId)->getFrontendName(); @@ -519,5 +573,45 @@ public function getChimpSyncEcommerce($storeId,$id,$type) public function getScope() { } - + public function loadStores() + { + $connection = $this->_mailChimpStores->getResource()->getConnection(); + $tableName = $this->_mailChimpStores->getResource()->getMainTable(); + $connection->truncateTable($tableName); + $apiKeys = $this->getConfigValue(self::XML_PATH_APIKEY_LIST); + $keys = explode("\n", $apiKeys); + foreach($keys as $apiKey) { + $this->_api->setApiKey(trim($apiKey)); + $this->_api->setUserAgent('Mailchimp4Magento' . (string)$this->getModuleVersion()); + $apiStores = $this->_api->ecommerce->stores->get(null,null,null,self::MAXSTORES); + foreach($apiStores['stores'] as $store) { + if($store['platform']!=self::PLATFORM) { + continue; + } + $this->log($store); + $mstore = $this->_mailChimpStoresFactory->create(); + $mstore->setApikey(trim($apiKey)); + $mstore->setStoreid($store['id']); + $mstore->setListId($store['list_id']); + $mstore->setName($store['name']); + $mstore->setPlatform($store['platform']); + $mstore->setIsSync($store['is_syncing']); + $mstore->setEmailAddress($store['email_address']); + $mstore->setCurrencyCode($store['currency_code']); +// $mstore->setMoneyFormat($store['money_format']); + $mstore->setPrimaryLocale($store['primary_locale']); + $mstore->setTimezone($store['timezone']); + $mstore->setPhone($store['phone']); + $mstore->setAddressAddress1($store['address']['address1']); + $mstore->setAddressAddress2($store['address']['address2']); + $mstore->setAddressCity($store['address']['city']); + $mstore->setAddressProvince($store['address']['province']); + $mstore->setAddressProvinceCode($store['address']['province_code']); + $mstore->setAddressPostalCode($store['address']['postal_code']); + $mstore->setAddressCountry($store['address']['country']); + $mstore->setAddressCountryCode($store['address']['country_code']); + $mstore->getResource()->save($mstore); + } + } + } } From 44543b85c3dcebc972432c90f1a756ba7938498f Mon Sep 17 00:00:00 2001 From: gonzalo Date: Fri, 21 Apr 2017 15:11:39 -0300 Subject: [PATCH 019/105] controlles to retrieve the cart --- Controller/Cart/Loadquote.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Controller/Cart/Loadquote.php b/Controller/Cart/Loadquote.php index 1457e0b0..b1908863 100644 --- a/Controller/Cart/Loadquote.php +++ b/Controller/Cart/Loadquote.php @@ -91,7 +91,7 @@ public function execute() $quote->getResource()->load($quote,$params['id']); $magentoStoreId = $quote->getStoreId(); $mailchimpStoreId = $this->_helper->getConfigValue( - \Ebizmarts\MailChimp\Helper\Data::XML_PATH_STORE, + \Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE, $magentoStoreId ); $syncCommerce = $this->_helper->getChimpSyncEcommerce( From 81b30015f59d2d2288768bddf0a79afb96d21d56 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Fri, 21 Apr 2017 15:13:51 -0300 Subject: [PATCH 020/105] stores controllers --- Controller/Adminhtml/Stores.php | 72 ++++++++++++++++ Controller/Adminhtml/Stores/Delete.php | 38 +++++++++ Controller/Adminhtml/Stores/Edit.php | 47 +++++++++++ Controller/Adminhtml/Stores/Index.php | 26 ++++++ Controller/Adminhtml/Stores/Save.php | 112 +++++++++++++++++++++++++ 5 files changed, 295 insertions(+) create mode 100644 Controller/Adminhtml/Stores.php create mode 100644 Controller/Adminhtml/Stores/Delete.php create mode 100644 Controller/Adminhtml/Stores/Edit.php create mode 100644 Controller/Adminhtml/Stores/Index.php create mode 100644 Controller/Adminhtml/Stores/Save.php diff --git a/Controller/Adminhtml/Stores.php b/Controller/Adminhtml/Stores.php new file mode 100644 index 00000000..926006e1 --- /dev/null +++ b/Controller/Adminhtml/Stores.php @@ -0,0 +1,72 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 4/12/17 11:35 AM + * @file: Stores.php + */ + +namespace Ebizmarts\MailChimp\Controller\Adminhtml; + +use Magento\Backend\App\Action; +use Magento\Backend\App\Action\Context; +use Magento\Framework\Registry; +use Magento\Framework\View\Result\PageFactory; +use Ebizmarts\MailChimp\Model\MailChimpStoresFactory; + +class Stores extends Action +{ + /** + * @var Registry + */ + protected $_coreRegistry; + /** + * @var PageFactory + */ + protected $_resultPageFactory; + /** + * @var MailChimpStoresFactory + */ + protected $_mailchimpStoresFactory; + /** + * @var \Ebizmarts\MailChimp\Helper\Data + */ + protected $_helper; + + /** + * Stores constructor. + * @param Context $context + * @param Registry $registry + * @param PageFactory $resultPageFactory + * @param MailChimpStoresFactory $storesFactory + * @param \Ebizmarts\MailChimp\Helper\Data $helper + */ + public function __construct( + Context $context, + Registry $registry, + PageFactory $resultPageFactory, + MailChimpStoresFactory $storesFactory, + \Ebizmarts\MailChimp\Helper\Data $helper + ) + { + parent::__construct($context); + $this->_coreRegistry = $registry; + $this->_resultPageFactory = $resultPageFactory; + $this->_mailchimpStoresFactory = $storesFactory; + $this->_helper = $helper; + + } + public function execute() + { + + } + protected function _isAllowed() + { + return $this->_authorization->isAllowed('Ebizmarts_MailChimp::manage_stores'); + } +} \ No newline at end of file diff --git a/Controller/Adminhtml/Stores/Delete.php b/Controller/Adminhtml/Stores/Delete.php new file mode 100644 index 00000000..c89389d5 --- /dev/null +++ b/Controller/Adminhtml/Stores/Delete.php @@ -0,0 +1,38 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 4/17/17 12:03 PM + * @file: Delete.php + */ + +namespace Ebizmarts\MailChimp\Controller\Adminhtml\Stores; + +class Delete extends \Ebizmarts\MailChimp\Controller\Adminhtml\Stores +{ + public function execute() + { + $resultRedirect = $this->resultRedirectFactory->create(); + $storeId = (int) $this->getRequest()->getParam('id'); + $this->_helper->log("store [$storeId]"); + if ($storeId) + { + $storeModel = $this->_mailchimpStoresFactory->create(); + $storeModel->getResource()->load($storeModel,$storeId); + try { + $api = $this->_helper->getApiByApiKey($storeModel->getApikey()); + $api->ecommerce->stores->delete($storeModel->getStoreid()); + $this->messageManager->addSuccess(__('You deleted the store.')); + return $resultRedirect->setPath('mailchimp/stores'); + } catch(\Mailchimp_Error $e) { + $this->messageManager->addError(__('Store could not be deleted.'.$e->getMessage())); + return $resultRedirect->setPath('mailchimp/stores/edit', array('id'=>$storeId)); + } + } + } +} \ No newline at end of file diff --git a/Controller/Adminhtml/Stores/Edit.php b/Controller/Adminhtml/Stores/Edit.php new file mode 100644 index 00000000..50bfad71 --- /dev/null +++ b/Controller/Adminhtml/Stores/Edit.php @@ -0,0 +1,47 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 4/5/17 1:23 PM + * @file: Edit.php + */ + +namespace Ebizmarts\MailChimp\Controller\Adminhtml\Stores; + +class Edit extends \Ebizmarts\MailChimp\Controller\Adminhtml\Stores +{ + public function execute() + { + $storeId = $this->getRequest()->getParam('id'); + /** @var \Ebizmarts\MailChimp\Model\MailChimpStores $model */ + $model = $this->_mailchimpStoresFactory->create(); + + if ($storeId) { + $model->getResource()->load($model, $storeId); + if (!$model->getId()) { + $this->messageManager->addError(__('This store no longer exists.')); + $this->_redirect('*/*/'); + return; + } + } + + // Restore previously entered form data from session + $data = $this->_session->getStoreData(true); + if (!empty($data)) { + $model->setData($data); + } + $this->_coreRegistry->register('mailchimp_stores', $model); + + /** @var \Magento\Backend\Model\View\Result\Page $resultPage */ + $resultPage = $this->_resultPageFactory->create(); + $resultPage->setActiveMenu('Ebizmarts_MailChimp::main_menu'); + $resultPage->getConfig()->getTitle()->prepend(__('Mailchimp Store')); + + return $resultPage; + } +} \ No newline at end of file diff --git a/Controller/Adminhtml/Stores/Index.php b/Controller/Adminhtml/Stores/Index.php new file mode 100644 index 00000000..e6812090 --- /dev/null +++ b/Controller/Adminhtml/Stores/Index.php @@ -0,0 +1,26 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 3/24/17 10:38 AM + * @file: Index.php + */ +namespace Ebizmarts\MailChimp\Controller\Adminhtml\Stores; + +use Ebizmarts\MailChimp\Controller\Adminhtml\Stores; + +class Index extends Stores +{ + public function execute() + { + $this->_helper->loadStores(); + $page = $this->_resultPageFactory->create(); + $page->getConfig()->getTitle()->prepend(__('MailChimp Stores')); + return $page; + } +} \ No newline at end of file diff --git a/Controller/Adminhtml/Stores/Save.php b/Controller/Adminhtml/Stores/Save.php new file mode 100644 index 00000000..92a49785 --- /dev/null +++ b/Controller/Adminhtml/Stores/Save.php @@ -0,0 +1,112 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 4/17/17 11:05 AM + * @file: Save.php + */ + +namespace Ebizmarts\MailChimp\Controller\Adminhtml\Stores; + +class Save extends \Ebizmarts\MailChimp\Controller\Adminhtml\Stores +{ + public function execute() + { + $isPost = $this->getRequest()->getPost(); + if ($isPost) { + $resultRedirect = $this->resultRedirectFactory->create(); + $returnToEdit = (bool)$this->getRequest()->getParam('back', false); + $storeModel = $this->_mailchimpStoresFactory->create(); + $formData = $this->getRequest()->getParam('stores'); + $storeId = isset($formData['id']) ? $formData['id'] : null; + if($storeId) { + $storeModel->getResource()->load($storeModel,$storeId); + } + try { + $this->_helper->log($formData); + $formData['storeid'] = $this->_updateMailchimp($formData); + $formData['platform'] = \Ebizmarts\MailChimp\Helper\Data::PLATFORM; + $storeModel->setData($formData); + $storeModel->getResource()->save($storeModel); + if($returnToEdit) { + if(!$storeId) { + $storeId = $storeModel->getId(); + } + return $resultRedirect->setPath('mailchimp/stores/edit', ['id'=>$storeId]); + } + else { + return $resultRedirect->setPath('mailchimp/stores'); + } + } catch(\Mailchimp_Error $e) + { + $this->messageManager->addError(__('Store could not be saved.'.$e->getMessage())); + return $resultRedirect->setPath('mailchimp/stores/edit', ['id'=>$storeId]); + } + } + } + protected function _updateMailchimp($formData) + { + $api = $this->_helper->getApiByApiKey($formData['apikey']); + // set the address + $address = []; + $address['address1'] = $formData['address_address1']; + $address['address2'] = $formData['address_address2']; + $address['city'] = $formData['address_city']; + $address['province'] = ''; + $address['province_code'] = ''; + $address['postal_code'] = $formData['address_postal_code']; + $address['country'] = ''; + $address['country_code'] = $formData['address_country_code']; + // ***** + $emailAddress = $formData['email_address']; + $currencyCode = $formData['currency_code']; + $primaryLocale = $formData['primary_locale']; + $timeZone = $formData['timezone']; + $phone = $formData['phone']; + $name = $formData['name']; + $domain = 'domain'; + $storeId = isset($formData['storeid']) ? $formData['storeid'] : null; + + if ($storeId) { + $api->ecommerce->stores->edit( + $storeId, + \Ebizmarts\MailChimp\Helper\Data::PLATFORM, + $domain, + $name, + $emailAddress, + $currencyCode, + null, + $primaryLocale, + $timeZone, + $phone, + $address + ); + } else { + $date = $this->_helper->getDateMicrotime(); + $mailchimpStoreId = md5($name. '_' . $date); + $this->_helper->log("add new store with id $mailchimpStoreId"); + $ret =$api->ecommerce->stores->add( + $mailchimpStoreId, + $formData['list_id'], + $name, + $currencyCode, + \Ebizmarts\MailChimp\Helper\Data::PLATFORM, + $domain, + $emailAddress, + null, + $primaryLocale, + $timeZone, + $phone, + $address + ); + $this->_helper->log($ret); + $formData['storeid'] = $mailchimpStoreId; + } + return $formData['storeid']; + } +} \ No newline at end of file From 96410c13a113844dabfb884da56fead44c379c93 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Fri, 21 Apr 2017 15:16:47 -0300 Subject: [PATCH 021/105] controllers to manage stores in the config --- .../Adminhtml/Ecommerce/DeleteStore.php | 106 ++++++++++++++++++ .../Adminhtml/Ecommerce/ResetLocalErrors.php | 6 +- Controller/Adminhtml/Ecommerce/ResetStore.php | 96 ++++++++++++++++ 3 files changed, 205 insertions(+), 3 deletions(-) create mode 100644 Controller/Adminhtml/Ecommerce/DeleteStore.php create mode 100644 Controller/Adminhtml/Ecommerce/ResetStore.php diff --git a/Controller/Adminhtml/Ecommerce/DeleteStore.php b/Controller/Adminhtml/Ecommerce/DeleteStore.php new file mode 100644 index 00000000..f3dcd503 --- /dev/null +++ b/Controller/Adminhtml/Ecommerce/DeleteStore.php @@ -0,0 +1,106 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 2/21/17 5:07 PM + * @file: ResetLocalErrors.php + */ + +namespace Ebizmarts\MailChimp\Controller\Adminhtml\Ecommerce; + +use Magento\Framework\Controller\Result\JsonFactory; +use Magento\Framework\Exception\ValidatorException; +use Symfony\Component\Config\Definition\Exception\Exception; + +class DeleteStore extends \Magento\Backend\App\Action +{ + /** + * @var JsonFactory + */ + protected $resultJsonFactory; + /** + * @var \Ebizmarts\MailChimp\Helper\Data + */ + protected $helper; + /** + * @var \Magento\Store\Model\StoreManagerInterface + */ + protected $storeManager; + /** + * @var \Magento\Config\Model\ResourceModel\Config + */ + protected $_config; + + /** + * DeleteStore constructor. + * @param \Magento\Backend\App\Action\Context $context + * @param JsonFactory $resultJsonFactory + * @param \Magento\Store\Model\StoreManagerInterface $storeManagerInterface + * @param \Ebizmarts\MailChimp\Helper\Data $helper + * @param \Magento\Config\Model\ResourceModel\Config $config + */ + public function __construct( + \Magento\Backend\App\Action\Context $context, + JsonFactory $resultJsonFactory, + \Magento\Store\Model\StoreManagerInterface $storeManagerInterface, + \Ebizmarts\MailChimp\Helper\Data $helper, + \Magento\Config\Model\ResourceModel\Config $config + ) + { + parent::__construct($context); + $this->resultJsonFactory = $resultJsonFactory; + $this->helper = $helper; + $this->storeManager = $storeManagerInterface; + $this->_config = $config; + } + + public function execute() + { + $valid = 1; + $message = ''; + $params = $this->getRequest()->getParams(); + if (isset($params['website'])) { + $scope = 'website'; + $storeId = $params['website']; + } elseif (isset($params['store'])) { + $scope = 'store'; + $storeId = $params['store']; + } else { + //$storeId = $this->storeManager->getDefaultStoreView()->getWebsiteId(); + $storeId = 0; + $scope = 'default'; + } + + $mailchimpStore = $this->helper->getConfigValue( + \Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE, + $storeId, + $scope + ); + $resultJson = $this->resultJsonFactory->create(); + try { + $this->helper->deleteStore($mailchimpStore); + $this->helper->log('before deleteConfig'); + $this->helper->log(\Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE); + $this->helper->log($scope); + $this->helper->log($storeId); + $this->_config->deleteConfig(\Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE, $scope, $storeId); + $this->helper->log('after deleteConfig'); + } catch(ValidatorException $e) { + $valid = 0; + $message = $e->getMessage(); + } + return $resultJson->setData([ + 'valid' => (int)$valid, + 'message' => $message, + ]); + } + protected function _isAllowed() + { + return $this->_authorization->isAllowed('Magento_EncryptionKey::crypt_key'); + } +} \ No newline at end of file diff --git a/Controller/Adminhtml/Ecommerce/ResetLocalErrors.php b/Controller/Adminhtml/Ecommerce/ResetLocalErrors.php index 49f82cd7..e9ff9ec9 100644 --- a/Controller/Adminhtml/Ecommerce/ResetLocalErrors.php +++ b/Controller/Adminhtml/Ecommerce/ResetLocalErrors.php @@ -59,19 +59,19 @@ public function execute() $params = $this->getRequest()->getParams(); if (isset($params['website'])) { $mailchimpStore = $this->helper->getConfigValue( - \Ebizmarts\MailChimp\Helper\Data::XML_PATH_STORE, + \Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE, $params['website'], 'website' ); } elseif (isset($params['store'])) { $mailchimpStore = $this->helper->getConfigValue( - \Ebizmarts\MailChimp\Helper\Data::XML_PATH_STORE, + \Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE, $params['store'], 'store' ); } else { $mailchimpStore = $this->helper->getConfigValue( - \Ebizmarts\MailChimp\Helper\Data::XML_PATH_STORE, + \Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE, $this->storeManager->getStore() ); } diff --git a/Controller/Adminhtml/Ecommerce/ResetStore.php b/Controller/Adminhtml/Ecommerce/ResetStore.php new file mode 100644 index 00000000..0abc4ddc --- /dev/null +++ b/Controller/Adminhtml/Ecommerce/ResetStore.php @@ -0,0 +1,96 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 2/21/17 5:07 PM + * @file: ResetLocalErrors.php + */ + +namespace Ebizmarts\MailChimp\Controller\Adminhtml\Ecommerce; + +use Magento\Framework\Controller\Result\JsonFactory; +use Magento\Framework\Exception\ValidatorException; +use Symfony\Component\Config\Definition\Exception\Exception; + +class ResetStore extends \Magento\Backend\App\Action +{ + /** + * @var JsonFactory + */ + protected $resultJsonFactory; + /** + * @var \Ebizmarts\MailChimp\Helper\Data + */ + protected $helper; + /** + * @var \Magento\Store\Model\StoreManagerInterface + */ + protected $storeManager; + + /** + * ResetLocalErrors constructor. + * @param \Magento\Backend\App\Action\Context $context + * @param JsonFactory $resultJsonFactory + * @param \Magento\Store\Model\StoreManagerInterface $storeManagerInterface + * @param \Ebizmarts\MailChimp\Helper\Data $helper + */ + public function __construct( + \Magento\Backend\App\Action\Context $context, + JsonFactory $resultJsonFactory, + \Magento\Store\Model\StoreManagerInterface $storeManagerInterface, + \Ebizmarts\MailChimp\Helper\Data $helper + ) + { + parent::__construct($context); + $this->resultJsonFactory = $resultJsonFactory; + $this->helper = $helper; + $this->storeManager = $storeManagerInterface; + } + + public function execute() + { + $valid = 1; + $message = ''; + $params = $this->getRequest()->getParams(); + if (isset($params['website'])) { + $mailchimpStore = $this->helper->getConfigValue( + \Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE, + $params['website'], + 'website' + ); + } elseif (isset($params['store'])) { + $mailchimpStore = $this->helper->getConfigValue( + \Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE, + $params['store'], + 'store' + ); + } else { + $mailchimpStore = $this->helper->getConfigValue( + \Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE, + $this->storeManager->getStore() + ); + } +// $this->helper->log($mailchimpStore); + + $resultJson = $this->resultJsonFactory->create(); + try { +// $this->helper->resetErrors($mailchimpStore); + } catch(ValidatorException $e) { + $valid = 0; + $message = $e->getMessage(); + } + return $resultJson->setData([ + 'valid' => (int)$valid, + 'message' => $message, + ]); + } + protected function _isAllowed() + { + return $this->_authorization->isAllowed('Magento_EncryptionKey::crypt_key'); + } +} \ No newline at end of file From ba0e8d4beb617d6baf3255f3fea76be5968577ad Mon Sep 17 00:00:00 2001 From: gonzalo Date: Fri, 21 Apr 2017 15:20:52 -0300 Subject: [PATCH 022/105] manage stores --- .../Backend/{Monkeylist.php => ApiKey.php} | 41 ++++---- Model/Config/Backend/MonkeyStore.php | 95 +++++++++++++++++++ Model/Config/Source/ApiKey.php | 48 ++++++++++ Model/Config/Source/Details.php | 2 +- Model/Config/Source/MonkeyStore.php | 64 +++++++++++++ Model/Config/Source/Monkeylist.php | 60 ------------ 6 files changed, 229 insertions(+), 81 deletions(-) rename Model/Config/Backend/{Monkeylist.php => ApiKey.php} (69%) create mode 100644 Model/Config/Backend/MonkeyStore.php create mode 100644 Model/Config/Source/ApiKey.php create mode 100644 Model/Config/Source/MonkeyStore.php delete mode 100644 Model/Config/Source/Monkeylist.php diff --git a/Model/Config/Backend/Monkeylist.php b/Model/Config/Backend/ApiKey.php similarity index 69% rename from Model/Config/Backend/Monkeylist.php rename to Model/Config/Backend/ApiKey.php index a9cfb59b..0a202f78 100644 --- a/Model/Config/Backend/Monkeylist.php +++ b/Model/Config/Backend/ApiKey.php @@ -15,7 +15,7 @@ use Magento\Framework\App\Config\ScopeConfigInterface; -class Monkeylist extends \Magento\Framework\App\Config\Value +class ApiKey extends \Magento\Framework\App\Config\Value { /** * @var \Ebizmarts\MailChimp\Helper\Data @@ -29,9 +29,13 @@ class Monkeylist extends \Magento\Framework\App\Config\Value * @var \Magento\Framework\Stdlib\DateTime\DateTime */ private $_date; + /** + * @var \Magento\Store\Model\StoreManager + */ + private $_storeManager; /** - * Monkeylist constructor. + * ApiKey constructor. * @param \Magento\Framework\Model\Context $context * @param \Magento\Framework\Registry $registry * @param ScopeConfigInterface $config @@ -41,6 +45,7 @@ class Monkeylist extends \Magento\Framework\App\Config\Value * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection * @param \Magento\Framework\Stdlib\DateTime\DateTime $date * @param \Ebizmarts\MailChimp\Helper\Data $helper + * @param \Magento\Store\Model\StoreManager $storeManager * @param array $data */ public function __construct( @@ -53,11 +58,13 @@ public function __construct( \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null, \Magento\Framework\Stdlib\DateTime\DateTime $date, \Ebizmarts\MailChimp\Helper\Data $helper, + \Magento\Store\Model\StoreManager $storeManager, array $data = [] ) { - $this->_helper = $helper; - $this->resourceConfig = $resourceConfig; - $this->_date = $date; + $this->_helper = $helper; + $this->resourceConfig = $resourceConfig; + $this->_date = $date; + $this->_storeManager = $storeManager; parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data); } @@ -65,28 +72,22 @@ public function beforeSave() { $generalData = $this->getData(); $data = $this->getData('groups'); + $found = 0; if (isset($data['ecommerce']['fields']['active']['value'])) { $active = $data['ecommerce']['fields']['active']['value']; } elseif ($data['ecommerce']['fields']['active']['inherit']) { $active = $data['ecommerce']['fields']['active']['inherit']; } if ($active&&$this->isValueChanged()) { - if ($this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_STORE,$generalData['scope_id'])) { - $this->_helper->deleteStore(); + $mailchimpStore = $this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE, $generalData['scope_id']); + $this->resourceConfig->deleteConfig(\Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE, $generalData['scope'], $generalData['scope_id']); + foreach ($this->_storeManager->getStores() as $storeId => $val) { + if ($this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE, $storeId) == $mailchimpStore) { + $found++; + } } - $store = $this->_helper->createStore($this->getValue(), $generalData['scope_id']); - if ($store) { - $this->resourceConfig->saveConfig( - \Ebizmarts\MailChimp\Helper\Data::XML_PATH_STORE, $store, - $generalData['scope'], - $generalData['scope_id'] - ); - $this->resourceConfig->saveConfig( - \Ebizmarts\MailChimp\Helper\Data::XML_PATH_SYNC_DATE, - $this->_date->gmtDate(), - $generalData['scope'], - $generalData['scope_id'] - ); + if ($found==1) { + $this->_helper->markAllBatchesAs($mailchimpStore, 'canceled'); } } return parent::beforeSave(); diff --git a/Model/Config/Backend/MonkeyStore.php b/Model/Config/Backend/MonkeyStore.php new file mode 100644 index 00000000..7b47cf64 --- /dev/null +++ b/Model/Config/Backend/MonkeyStore.php @@ -0,0 +1,95 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 3/29/17 4:29 PM + * @file: MonkeyStore.php + */ + +namespace Ebizmarts\MailChimp\Model\Config\Backend; + +class MonkeyStore extends \Magento\Framework\App\Config\Value +{ + /** + * @var \Ebizmarts\MailChimp\Helper\Data + */ + private $_helper; + /** + * @var \Magento\Config\Model\ResourceModel\Config + */ + protected $resourceConfig; + /** + * @var \Magento\Framework\Stdlib\DateTime\DateTime + */ + private $_date; + /** + * @var \Magento\Store\Model\StoreManager + */ + private $_storeManager; + + /** + * ApiKey constructor. + * @param \Magento\Framework\Model\Context $context + * @param \Magento\Framework\Registry $registry + * @param ScopeConfigInterface $config + * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList + * @param \Magento\Config\Model\ResourceModel\Config $resourceConfig + * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource + * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection + * @param \Magento\Framework\Stdlib\DateTime\DateTime $date + * @param \Ebizmarts\MailChimp\Helper\Data $helper + * @param \Magento\Store\Model\StoreManager $storeManager + * @param array $data + */ + public function __construct( + \Magento\Framework\Model\Context $context, + \Magento\Framework\Registry $registry, + \Magento\Framework\App\Config\ScopeConfigInterface $config, + \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList, + \Magento\Config\Model\ResourceModel\Config $resourceConfig, + \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null, + \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null, + \Magento\Framework\Stdlib\DateTime\DateTime $date, + \Ebizmarts\MailChimp\Helper\Data $helper, + \Magento\Store\Model\StoreManager $storeManager, + array $data = [] + ) { + $this->_helper = $helper; + $this->resourceConfig = $resourceConfig; + $this->_date = $date; + $this->_storeManager = $storeManager; + parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data); + } + + + public function beforeSave() + { + $generalData = $this->getData(); + $data = $this->getData('groups'); + $found = 0; + if (isset($data['ecommerce']['fields']['active']['value'])) { + $active = $data['ecommerce']['fields']['active']['value']; + } elseif ($data['ecommerce']['fields']['active']['inherit']) { + $active = $data['ecommerce']['fields']['active']['inherit']; + } + if ($active&&$this->isValueChanged()) { + $mailchimpStore = $this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE, $generalData['scope_id']); +// $this->resourceConfig->deleteConfig(\Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE, $generalData['scope'], $generalData['scope_id']); + foreach ($this->_storeManager->getStores() as $storeId => $val) { + if ($this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE, $storeId) == $mailchimpStore) { + $found++; + } + } + if ($found==1) { + $this->_helper->markAllBatchesAs($mailchimpStore, 'canceled'); + } + } + return parent::beforeSave(); + } + +} \ No newline at end of file diff --git a/Model/Config/Source/ApiKey.php b/Model/Config/Source/ApiKey.php new file mode 100644 index 00000000..4657746c --- /dev/null +++ b/Model/Config/Source/ApiKey.php @@ -0,0 +1,48 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 3/30/17 1:36 PM + * @file: ApiKey.php + */ +namespace Ebizmarts\MailChimp\Model\Config\Source; + +class ApiKey implements \Magento\Framework\Option\ArrayInterface +{ + private $options = null; + /** + * MonkeyStore constructor. + * @param \Ebizmarts\MailChimp\Helper\Data $helper + */ + public function __construct( + \Ebizmarts\MailChimp\Helper\Data $helper + ) + { + $apiKeys = $helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_APIKEY_LIST); + if ($apiKeys) { + $this->options = explode("\n", $apiKeys); + } else { + $this->options = []; + } + $helper->log(count($this->options)); + } + public function toOptionArray() + { + if (is_array($this->options)&&count($this->options)) { + $rc = []; + $rc[] = ['value' => -1, 'label' => 'Select one ApiKey']; + foreach ($this->options as $apiKey) { + $rc[] = ['value'=> trim($apiKey), 'label' => trim($apiKey)]; + } + } else { + $rc[] = ['value' => 0, 'label' => __('---Enter first an APIKey list---')]; + } + return $rc; + + } +} \ No newline at end of file diff --git a/Model/Config/Source/Details.php b/Model/Config/Source/Details.php index f1f376ba..3b404528 100644 --- a/Model/Config/Source/Details.php +++ b/Model/Config/Source/Details.php @@ -43,7 +43,7 @@ public function __construct( $api = $this->_helper->getApi(); try { $this->_options = $api->root->info(); - $mailchimpStoreId = $this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_STORE); + $mailchimpStoreId = $this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE); if ($mailchimpStoreId && $this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_ECOMMERCE_ACTIVE)) { $this->_options['store_exists'] = true; $totalCustomers = $api->ecommerce->customers->getAll($mailchimpStoreId, 'total_items'); diff --git a/Model/Config/Source/MonkeyStore.php b/Model/Config/Source/MonkeyStore.php new file mode 100644 index 00000000..38b429b6 --- /dev/null +++ b/Model/Config/Source/MonkeyStore.php @@ -0,0 +1,64 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 3/28/17 10:57 AM + * @file: MonkeyStore.php + */ + +namespace Ebizmarts\MailChimp\Model\Config\Source; + +class MonkeyStore implements \Magento\Framework\Option\ArrayInterface +{ + private $options = null; + + /** + * MonkeyStore constructor. + * @param \Ebizmarts\MailChimp\Helper\Data $helper + * @param \Magento\Store\Model\StoreManagerInterface $storeManager + */ + public function __construct( + \Ebizmarts\MailChimp\Helper\Data $helper, + \Magento\Store\Model\StoreManagerInterface $storeManager + ) + { + $helper->log($helper->getApiKey($storeManager->getStore()->getId())); + if ($helper->getApiKey($storeManager->getStore()->getId())) { + try { + $this->options = $helper->getApi()->ecommerce->stores->get(null,null,null,\Ebizmarts\MailChimp\Helper\Data::MAXSTORES); + } catch(\Exception $e) { + $helper->log($e->getMessage()); + } + } + } + public function toOptionArray() + { + if (is_array($this->options)) { + $rc = []; + $rc[] = ['value' => -1, 'label' => 'Select one Mailchimp Store']; + foreach ($this->options['stores'] as $store) { + if ($store['platform'] == \Ebizmarts\MailChimp\Helper\Data::PLATFORM) { + $rc[] = ['value'=> $store['id'], 'label' => $store['name']]; + } + } + } else { + $rc[] = ['value' => 0, 'label' => __('---No Data---')]; + } + return $rc; + + } + public function toArray() + { + $rc = []; + foreach ($this->options['stores'] as $store) { + $rc[$store['id']] = $store['name']; + } + return $rc; + + } +} \ No newline at end of file diff --git a/Model/Config/Source/Monkeylist.php b/Model/Config/Source/Monkeylist.php deleted file mode 100644 index 1b12334e..00000000 --- a/Model/Config/Source/Monkeylist.php +++ /dev/null @@ -1,60 +0,0 @@ - - * @copyright Ebizmarts (http://ebizmarts.com) - * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) - * @date: 9/30/16 10:43 AM - * @file: Monkeylist.php - */ -namespace Ebizmarts\MailChimp\Model\Config\Source; - -class Monkeylist implements \Magento\Framework\Option\ArrayInterface -{ - /** - * @var \Ebizmarts\MailChimp\Helper\Data - */ - private $_helper = null; - private $_options = null; - /** - * Monkeylist constructor. - * @param \Ebizmarts\MailChimp\Helper\Data $helper - */ - public function __construct(\Ebizmarts\MailChimp\Helper\Data $helper) - { - $this->_helper = $helper; - if ($this->_helper->getApiKey()) { - try { - $this->_options = $this->_helper->getApi()->lists->getLists(null, 'lists', null, 100); - } catch (\Exception $e) { - $this->_helper->log($e->getMessage()); - } - } - } - public function toOptionArray() - { - if (is_array($this->_options)) { - $rc = []; - foreach ($this->_options['lists'] as $list) { - $memberCount = $list['stats']['member_count']; - $memberText = __('members'); - $label = $list['name'] . ' (' . $memberCount . ' ' . $memberText . ')'; - $rc[] = ['value' => $list['id'], 'label' => $label]; - } - } else { - $rc[] = ['value' => 0, 'label' => __('---No Data---')]; - } - return $rc; - } - public function toArray() - { - $rc = []; - foreach ($this->_options['lists'] as $list) { - $rc[$list['id']] = $list['name']; - } - return $rc; - } -} From 791a1d4d9e1a688b201409f5b76d3cd1e0cbc568 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Fri, 21 Apr 2017 15:21:38 -0300 Subject: [PATCH 023/105] add action to the store grid --- Ui/Component/Stores/Grid/Column/Actions.php | 73 +++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 Ui/Component/Stores/Grid/Column/Actions.php diff --git a/Ui/Component/Stores/Grid/Column/Actions.php b/Ui/Component/Stores/Grid/Column/Actions.php new file mode 100644 index 00000000..5b4da997 --- /dev/null +++ b/Ui/Component/Stores/Grid/Column/Actions.php @@ -0,0 +1,73 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 4/5/17 11:19 AM + * @file: Actions.php + */ +namespace Ebizmarts\MailChimp\Ui\Component\Stores\Grid\Column; + +use Magento\Framework\View\Element\UiComponent\ContextInterface; +use Magento\Framework\View\Element\UiComponentFactory; +use Magento\Ui\Component\Listing\Columns\Column; +use Magento\Framework\UrlInterface; + +/** + * Class ProductActions + */ +class Actions extends Column +{ + /** + * @var UrlInterface + */ + protected $urlBuilder; + + /** + * @param ContextInterface $context + * @param UiComponentFactory $uiComponentFactory + * @param UrlInterface $urlBuilder + * @param array $components + * @param array $data + */ + public function __construct( + ContextInterface $context, + UiComponentFactory $uiComponentFactory, + UrlInterface $urlBuilder, + array $components = [], + array $data = [] + ) { + $this->urlBuilder = $urlBuilder; + parent::__construct($context, $uiComponentFactory, $components, $data); + } + + /** + * Prepare Data Source + * + * @param array $dataSource + * @return array + */ + public function prepareDataSource(array $dataSource) + { + if (isset($dataSource['data']['items'])) { + $storeId = $this->context->getFilterParam('store_id'); + + foreach ($dataSource['data']['items'] as &$item) { + $item[$this->getData('name')]['edit'] = [ + 'href' => $this->urlBuilder->getUrl( + 'mailchimp/stores/edit', + ['id' => $item['id']] + ), + 'label' => __('Edit'), + 'hidden' => false, + ]; + } + } + + return $dataSource; + } +} From 6af85a68114a1561652704e70f090fa850f5eb5f Mon Sep 17 00:00:00 2001 From: gonzalo Date: Fri, 21 Apr 2017 15:23:02 -0300 Subject: [PATCH 024/105] controller to get the mailchimp list via ajax --- Controller/Adminhtml/Lists/Get.php | 61 ++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Controller/Adminhtml/Lists/Get.php diff --git a/Controller/Adminhtml/Lists/Get.php b/Controller/Adminhtml/Lists/Get.php new file mode 100644 index 00000000..3883a073 --- /dev/null +++ b/Controller/Adminhtml/Lists/Get.php @@ -0,0 +1,61 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 4/20/17 3:20 PM + * @file: Get.php + */ +namespace Ebizmarts\MailChimp\Controller\Adminhtml\Lists; + +use Magento\Backend\App\Action; +use Magento\Backend\App\Action\Context; +use Magento\Framework\Controller\ResultFactory; + +class Get extends Action +{ + /** + * @var \Ebizmarts\MailChimp\Helper\Data + */ + protected $_helper; + /** + * @var ResultFactory + */ + protected $_resultFactory; + + /** + * Get constructor. + * @param Context $context + * @param ResultFactory $resultFactory + * @param \Ebizmarts\MailChimp\Helper\Data $helper + */ + public function __construct( + Context $context, + ResultFactory $resultFactory, + \Ebizmarts\MailChimp\Helper\Data $helper + ) + { + parent::__construct($context); + $this->_resultFactory = $resultFactory; + $this->_helper = $helper; + + } + public function execute() + { + $param = $this->getRequest()->getParams(); + $apiKey = $param['apikey']; + $api = $this->_helper->getApiByApiKey($apiKey); + $lists = $api->lists->getLists(); + $result = []; + foreach($lists['lists'] as $list) { + $result[] = ['id'=> $list['id'], 'name'=> $list['name']]; + } + $resultJson = $this->_resultFactory->create(ResultFactory::TYPE_JSON); + $resultJson->setData($result); + return $resultJson; + } +} \ No newline at end of file From 9a47e7717fde7a6396d5a096ef2d472028268c86 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Fri, 21 Apr 2017 16:31:16 -0300 Subject: [PATCH 025/105] control if you select a store --- Cron/Ecommerce.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Cron/Ecommerce.php b/Cron/Ecommerce.php index 14033243..6e0b6d84 100644 --- a/Cron/Ecommerce.php +++ b/Cron/Ecommerce.php @@ -86,13 +86,17 @@ public function execute() { $this->_storeManager->setCurrentStore($storeId); if($this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_ACTIVE)) { - $this->_apiResult->processResponses($storeId,true); - $this->_processStore($storeId); + $mailchimpStoreId = $this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE,$storeId); + if ($mailchimpStoreId != -1) + { + $this->_apiResult->processResponses($storeId,true, $mailchimpStoreId); + $this->_processStore($storeId, $mailchimpStoreId); + } } } } - protected function _processStore($storeId) + protected function _processStore($storeId, $mailchimpStoreId) { $this->_helper->log(__METHOD__); $batchArray = array(); @@ -121,6 +125,7 @@ protected function _processStore($storeId) $this->_mailChimpSyncBatches->setStoreId($storeId); $this->_mailChimpSyncBatches->setBatchId($batchResponse['id']); $this->_mailChimpSyncBatches->setStatus($batchResponse['status']); + $this->_mailChimpSyncBatches->setMailchimpStoreId($mailchimpStoreId); $this->_mailChimpSyncBatches->getResource()->save($this->_mailChimpSyncBatches); } } catch(Exception $e) { From 6725f79cf67e358b6ef470508a10db005f997d63 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Mon, 24 Apr 2017 14:24:13 -0300 Subject: [PATCH 026/105] add control if you enter an apikey first --- Helper/Data.php | 3 +++ composer.json | 2 +- etc/adminhtml/system.xml | 4 ++-- etc/module.xml | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Helper/Data.php b/Helper/Data.php index 30a1994b..057d2860 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -581,6 +581,9 @@ public function loadStores() $apiKeys = $this->getConfigValue(self::XML_PATH_APIKEY_LIST); $keys = explode("\n", $apiKeys); foreach($keys as $apiKey) { + if(!$apiKey || $apiKey =='') { + continue; + } $this->_api->setApiKey(trim($apiKey)); $this->_api->setUserAgent('Mailchimp4Magento' . (string)$this->getModuleVersion()); $apiStores = $this->_api->ecommerce->stores->get(null,null,null,self::MAXSTORES); diff --git a/composer.json b/composer.json index d5855d73..db1518f0 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ }, "description": "Connect MailChimp with Magento", "type": "magento2-module", - "version": "1.0.5", + "version": "1.0.6", "authors": [ { "name": "Ebizmarts Corp", diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index fd9d5467..aa48f599 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -29,7 +29,7 @@ Magento\Config\Model\Config\Source\Yesno - + Ebizmarts\MailChimp\Model\Config\Backend\ApiKey Enterlist of API keys one per line @@ -54,7 +54,7 @@ 1 - + Ebizmarts\MailChimp\Model\Config\Source\MonkeyStore Ebizmarts\MailChimp\Model\Config\Backend\MonkeyStore diff --git a/etc/module.xml b/etc/module.xml index a889680e..9dd3ae6b 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -11,7 +11,7 @@ */ --> - + From bd9cdc5ac2606a43cef0462d60bde3aab0ea43d9 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Mon, 24 Apr 2017 18:17:34 -0300 Subject: [PATCH 027/105] closes #14 --- Model/Plugin/AccountManagement.php | 75 ++++++++++++++++++++++++++++++ etc/di.xml | 3 ++ 2 files changed, 78 insertions(+) create mode 100644 Model/Plugin/AccountManagement.php diff --git a/Model/Plugin/AccountManagement.php b/Model/Plugin/AccountManagement.php new file mode 100644 index 00000000..3c6111f8 --- /dev/null +++ b/Model/Plugin/AccountManagement.php @@ -0,0 +1,75 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 4/24/17 5:33 PM + * @file: AccountManagement.php + */ + +namespace Ebizmarts\MailChimp\Model\Plugin; + +class AccountManagement +{ + /** + * @var \Ebizmarts\MailChimp\Helper\Data + */ + protected $_helper; + /** + * @var \Magento\Checkout\Model\Session + */ + protected $_session; + /** + * @var \Magento\Quote\Model\QuoteFactory + */ + protected $_quote; + + /** + * AccountManagement constructor. + * @param \Ebizmarts\MailChimp\Helper\Data $helper + * @param \Magento\Checkout\Model\Session $customerSession + * @param \Magento\Quote\Model\QuoteFactory $quote + */ + public function __construct( + \Ebizmarts\MailChimp\Helper\Data $helper, + \Magento\Checkout\Model\Session $checkoutSession, + \Magento\Quote\Model\QuoteFactory $quote + ) + { + $this->_helper = $helper; + $this->_quote = $quote; + $this->_session = $checkoutSession; + } + + /** + * @param \Magento\Customer\Model\AccountManagement $accountManagement + * @param \Closure $proceed + * @param $customerEmail + * @param null $websiteId + */ + public function aroundIsEmailAvailable( + \Magento\Customer\Model\AccountManagement $accountManagement, + \Closure $proceed, + $customerEmail, + $websiteId=null) + { + $ret = $proceed($customerEmail,$websiteId); + if ($this->_session) + { + $quoteId = $this->_session->getQuoteId(); + if ($quoteId) + { + $quote = $this->_quote->create(); + $quote->getResource()->load($quote,$quoteId); + $quote->setCustomerEmail($customerEmail); + $quote->setUpdatedAt(date('Y-m-d H:i:s')); + $quote->getResource()->save($quote); + } + } + return$ret; + } +} \ No newline at end of file diff --git a/etc/di.xml b/etc/di.xml index f0f51bbf..9959a2eb 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -53,4 +53,7 @@ + + + \ No newline at end of file From c7c20f9a3bfc7f72771dc6a788f4b1db10c68d65 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Wed, 26 Apr 2017 16:17:51 -0300 Subject: [PATCH 028/105] closes #11 --- Model/Plugin/Quote.php | 51 +++++++++++++++++++ Observer/Sales/Order/SubmitBefore.php | 4 +- Setup/UpgradeSchema.php | 45 ++++++++++++++++ composer.json | 2 +- etc/events.xml | 3 ++ etc/fieldset.xml | 6 +++ etc/frontend/di.xml | 17 +++++++ etc/module.xml | 2 +- .../layout/mailchimp_stores_edit.xml | 9 +++- view/adminhtml/web/js/apikey.js | 2 +- view/frontend/templates/loadquote.phtml | 1 - 11 files changed, 135 insertions(+), 7 deletions(-) create mode 100644 Model/Plugin/Quote.php create mode 100644 etc/frontend/di.xml diff --git a/Model/Plugin/Quote.php b/Model/Plugin/Quote.php new file mode 100644 index 00000000..37d14313 --- /dev/null +++ b/Model/Plugin/Quote.php @@ -0,0 +1,51 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 4/25/17 7:40 PM + * @file: Quote.php + */ +namespace Ebizmarts\MailChimp\Model\Plugin; + +class Quote +{ + /** + * @var \Magento\Framework\Stdlib\CookieManagerInterface + */ + protected $_cookieManager; + /** + * @var \Ebizmarts\MailChimp\Helper\Data + */ + protected $_helper; + + /** + * Quote constructor. + * @param \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager + * @param \Ebizmarts\MailChimp\Helper\Data $helper + */ + public function __construct( + \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager, + \Ebizmarts\MailChimp\Helper\Data $helper + ) + { + $this->_cookieManager = $cookieManager; + $this->_helper = $helper; + } + + public function beforeBeforeSave(\Magento\Quote\Model\Quote $quote) + { + $mailchimp_campaign_id = $this->_cookieManager->getCookie('mailchimp_campaign_id'); + if ($mailchimp_campaign_id) { + $quote->setData('mailchimp_campaign_id',$mailchimp_campaign_id); + } + $mailchimp_landing_page = $this->_cookieManager->getCookie('mailchimp_landing_page'); + if ($mailchimp_landing_page) { + $quote->setData('mailchimp_landing_page',$mailchimp_landing_page); + } + } +} \ No newline at end of file diff --git a/Observer/Sales/Order/SubmitBefore.php b/Observer/Sales/Order/SubmitBefore.php index 34ffff81..d39c4162 100644 --- a/Observer/Sales/Order/SubmitBefore.php +++ b/Observer/Sales/Order/SubmitBefore.php @@ -17,7 +17,9 @@ class SubmitBefore implements \Magento\Framework\Event\ObserverInterface { private $attributes = [ - 'mailchimp_abandonedcart_flag' + 'mailchimp_abandonedcart_flag', + 'mailchimp_campaign_id', + 'mailchimp_landing_page' ]; public function execute(\Magento\Framework\Event\Observer $observer) { diff --git a/Setup/UpgradeSchema.php b/Setup/UpgradeSchema.php index 4309be13..8bf6e763 100644 --- a/Setup/UpgradeSchema.php +++ b/Setup/UpgradeSchema.php @@ -177,6 +177,51 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con $installer->getConnection()->createTable($table); } + if (version_compare($context->getVersion(), '1.0.7') < 0) { + $installer->getConnection()->addColumn( + $installer->getTable('quote'), + 'mailchimp_campaign_id', + [ + 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 'length' => 16, + 'default' => '', + 'comment' => 'Campaign' + ] + ); + + $installer->getConnection()->addColumn( + $installer->getTable('sales_order'), + 'mailchimp_campaign_id', + [ + 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 'length' => 16, + 'default' => '', + 'comment' => 'Campaign' + ] + ); + $installer->getConnection()->addColumn( + $installer->getTable('quote'), + 'mailchimp_landing_page', + [ + 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 'length' => 512, + 'default' => '', + 'comment' => 'Landing Page' + ] + ); + + $installer->getConnection()->addColumn( + $installer->getTable('sales_order'), + 'mailchimp_landing_page', + [ + 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 'length' => 512, + 'default' => '', + 'comment' => 'Landing Page' + ] + ); + + } $installer->endSetup(); diff --git a/composer.json b/composer.json index db1518f0..fda02129 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ }, "description": "Connect MailChimp with Magento", "type": "magento2-module", - "version": "1.0.6", + "version": "1.0.7", "authors": [ { "name": "Ebizmarts Corp", diff --git a/etc/events.xml b/etc/events.xml index 7abc804e..f02e5756 100644 --- a/etc/events.xml +++ b/etc/events.xml @@ -14,4 +14,7 @@ + + + \ No newline at end of file diff --git a/etc/fieldset.xml b/etc/fieldset.xml index ee5b2515..65631970 100644 --- a/etc/fieldset.xml +++ b/etc/fieldset.xml @@ -17,6 +17,12 @@ + + + + + + \ No newline at end of file diff --git a/etc/frontend/di.xml b/etc/frontend/di.xml new file mode 100644 index 00000000..41d00a62 --- /dev/null +++ b/etc/frontend/di.xml @@ -0,0 +1,17 @@ + + + + + + + \ No newline at end of file diff --git a/etc/module.xml b/etc/module.xml index 9dd3ae6b..ea2eb242 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -11,7 +11,7 @@ */ --> - + diff --git a/view/adminhtml/layout/mailchimp_stores_edit.xml b/view/adminhtml/layout/mailchimp_stores_edit.xml index 394e9734..342b7e8d 100644 --- a/view/adminhtml/layout/mailchimp_stores_edit.xml +++ b/view/adminhtml/layout/mailchimp_stores_edit.xml @@ -1,8 +1,13 @@ From 8e6c1ca2a55d724d16e3e26df2ce8b37ffbad12f Mon Sep 17 00:00:00 2001 From: gonzalo Date: Wed, 26 Apr 2017 16:56:29 -0300 Subject: [PATCH 029/105] closes #11 --- view/frontend/layout/default.xml | 20 +++++++++ view/frontend/requirejs-config.js | 17 ++++++++ view/frontend/templates/catcher.phtml | 2 + view/frontend/web/js/campaigncatcher.js | 57 +++++++++++++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 view/frontend/layout/default.xml create mode 100644 view/frontend/requirejs-config.js create mode 100644 view/frontend/templates/catcher.phtml create mode 100644 view/frontend/web/js/campaigncatcher.js diff --git a/view/frontend/layout/default.xml b/view/frontend/layout/default.xml new file mode 100644 index 00000000..fdb7bc80 --- /dev/null +++ b/view/frontend/layout/default.xml @@ -0,0 +1,20 @@ + + + + + + + + + diff --git a/view/frontend/requirejs-config.js b/view/frontend/requirejs-config.js new file mode 100644 index 00000000..3b146154 --- /dev/null +++ b/view/frontend/requirejs-config.js @@ -0,0 +1,17 @@ +/** + * Ebizmarts_MailChimp Magento JS component + * + * @category Ebizmarts + * @package Ebizmarts_MailChimp + * @author Ebizmarts Team + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + */ + +var config = { + "map": { + "*": { + campaigncatcher: 'Ebizmarts_MailChimp/js/campaigncatcher' + } + } +}; \ No newline at end of file diff --git a/view/frontend/templates/catcher.phtml b/view/frontend/templates/catcher.phtml new file mode 100644 index 00000000..fda59d60 --- /dev/null +++ b/view/frontend/templates/catcher.phtml @@ -0,0 +1,2 @@ + \ No newline at end of file diff --git a/view/frontend/web/js/campaigncatcher.js b/view/frontend/web/js/campaigncatcher.js new file mode 100644 index 00000000..bbc30eeb --- /dev/null +++ b/view/frontend/web/js/campaigncatcher.js @@ -0,0 +1,57 @@ +/** + * Ebizmarts_MailChimp Magento JS component + * + * @category Ebizmarts + * @package Ebizmarts_MailChimp + * @author Ebizmarts Team + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + */ +define([ + 'jquery', + 'mage/cookies' + ], + function($){ + "use strict"; + + $.widget('mage.campaigncatcher', { + _init: function() { + $(document).ready(function() { + var urlparams = location.search.substr(1).split('&'); + var params = new Array(); + var mc_cid = null; + var isMailchimp = false; + for (var i = 0; i < urlparams.length; i++) { + var param = urlparams[i].split('='); + var key = param[0]; + var val = param[1]; + if (key && val) { + params[key] = val; + } + + if(key=='utm_source') { + var reg = /^mailchimp$/; + if(reg.exec(val)) { + isMailchimp = true; + } + } + else { + if (key=='mc_cid') { + mc_cid = val; + } + } + } + if (mc_cid&&!isMailchimp) { + $.mage.cookies.set('mailchimp_campaign_id' , mc_cid); + $.mage.cookies.set('mailchimp_landing_page', location); + } + + if(isMailchimp) { + $.mage.cookies.clear('mailchimp_campaign_id'); + $.mage.cookies.set('mailchimp_landing_page', location); + } + }); + } + }); + return $.mage.campaigncatcher; +}); \ No newline at end of file From be84cd33f7f9e40d52eaa22249a87fe6d0e15a8c Mon Sep 17 00:00:00 2001 From: gonzalo Date: Wed, 26 Apr 2017 16:59:09 -0300 Subject: [PATCH 030/105] remove debug logging --- Model/Config/Source/ApiKey.php | 1 - 1 file changed, 1 deletion(-) diff --git a/Model/Config/Source/ApiKey.php b/Model/Config/Source/ApiKey.php index 4657746c..7bb1fbd7 100644 --- a/Model/Config/Source/ApiKey.php +++ b/Model/Config/Source/ApiKey.php @@ -29,7 +29,6 @@ public function __construct( } else { $this->options = []; } - $helper->log(count($this->options)); } public function toOptionArray() { From 3ed042ba3f0a2ee505e966d3dea1a7af26a68fc3 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Wed, 26 Apr 2017 17:03:30 -0300 Subject: [PATCH 031/105] remove debug logging --- Controller/Adminhtml/Ecommerce/DeleteStore.php | 5 ----- Controller/Adminhtml/Stores/Delete.php | 1 - Controller/Adminhtml/Stores/Save.php | 3 --- Model/Config/Source/MonkeyStore.php | 1 - 4 files changed, 10 deletions(-) diff --git a/Controller/Adminhtml/Ecommerce/DeleteStore.php b/Controller/Adminhtml/Ecommerce/DeleteStore.php index f3dcd503..55e6eb86 100644 --- a/Controller/Adminhtml/Ecommerce/DeleteStore.php +++ b/Controller/Adminhtml/Ecommerce/DeleteStore.php @@ -84,12 +84,7 @@ public function execute() $resultJson = $this->resultJsonFactory->create(); try { $this->helper->deleteStore($mailchimpStore); - $this->helper->log('before deleteConfig'); - $this->helper->log(\Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE); - $this->helper->log($scope); - $this->helper->log($storeId); $this->_config->deleteConfig(\Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE, $scope, $storeId); - $this->helper->log('after deleteConfig'); } catch(ValidatorException $e) { $valid = 0; $message = $e->getMessage(); diff --git a/Controller/Adminhtml/Stores/Delete.php b/Controller/Adminhtml/Stores/Delete.php index c89389d5..097b0b25 100644 --- a/Controller/Adminhtml/Stores/Delete.php +++ b/Controller/Adminhtml/Stores/Delete.php @@ -19,7 +19,6 @@ public function execute() { $resultRedirect = $this->resultRedirectFactory->create(); $storeId = (int) $this->getRequest()->getParam('id'); - $this->_helper->log("store [$storeId]"); if ($storeId) { $storeModel = $this->_mailchimpStoresFactory->create(); diff --git a/Controller/Adminhtml/Stores/Save.php b/Controller/Adminhtml/Stores/Save.php index 92a49785..ee4df697 100644 --- a/Controller/Adminhtml/Stores/Save.php +++ b/Controller/Adminhtml/Stores/Save.php @@ -28,7 +28,6 @@ public function execute() $storeModel->getResource()->load($storeModel,$storeId); } try { - $this->_helper->log($formData); $formData['storeid'] = $this->_updateMailchimp($formData); $formData['platform'] = \Ebizmarts\MailChimp\Helper\Data::PLATFORM; $storeModel->setData($formData); @@ -89,7 +88,6 @@ protected function _updateMailchimp($formData) } else { $date = $this->_helper->getDateMicrotime(); $mailchimpStoreId = md5($name. '_' . $date); - $this->_helper->log("add new store with id $mailchimpStoreId"); $ret =$api->ecommerce->stores->add( $mailchimpStoreId, $formData['list_id'], @@ -104,7 +102,6 @@ protected function _updateMailchimp($formData) $phone, $address ); - $this->_helper->log($ret); $formData['storeid'] = $mailchimpStoreId; } return $formData['storeid']; diff --git a/Model/Config/Source/MonkeyStore.php b/Model/Config/Source/MonkeyStore.php index 38b429b6..af91f58d 100644 --- a/Model/Config/Source/MonkeyStore.php +++ b/Model/Config/Source/MonkeyStore.php @@ -27,7 +27,6 @@ public function __construct( \Magento\Store\Model\StoreManagerInterface $storeManager ) { - $helper->log($helper->getApiKey($storeManager->getStore()->getId())); if ($helper->getApiKey($storeManager->getStore()->getId())) { try { $this->options = $helper->getApi()->ecommerce->stores->get(null,null,null,\Ebizmarts\MailChimp\Helper\Data::MAXSTORES); From e95e12b5c70b55539ef113edec5f1f80f8ec8598 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Mon, 1 May 2017 22:40:53 -0300 Subject: [PATCH 032/105] remove logs --- Cron/Ecommerce.php | 2 -- Helper/Data.php | 2 +- Model/Api/Cart.php | 22 ---------------------- Model/Api/Customer.php | 12 ------------ Model/Api/Order.php | 23 +---------------------- Model/Api/Product.php | 11 ----------- Model/Api/Result.php | 7 ++++--- 7 files changed, 6 insertions(+), 73 deletions(-) diff --git a/Cron/Ecommerce.php b/Cron/Ecommerce.php index 6e0b6d84..57bb6bfb 100644 --- a/Cron/Ecommerce.php +++ b/Cron/Ecommerce.php @@ -81,7 +81,6 @@ public function __construct( public function execute() { - $this->_helper->log(__METHOD__); foreach($this->_storeManager->getStores() as $storeId => $val) { $this->_storeManager->setCurrentStore($storeId); @@ -98,7 +97,6 @@ public function execute() protected function _processStore($storeId, $mailchimpStoreId) { - $this->_helper->log(__METHOD__); $batchArray = array(); $results = array(); if ($this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_ECOMMERCE_ACTIVE,$storeId)) { diff --git a/Helper/Data.php b/Helper/Data.php index 057d2860..ed42b187 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -554,6 +554,7 @@ public function resetEcommerce() } public function saveEcommerceData($storeId, $entityId , $date, $error, $modified, $type, $deleted = 0, $token = null) { + $chimpSyncEcommerce = $this->getChimpSyncEcommerce($storeId,$entityId,$type); $chimpSyncEcommerce->setMailchimpStoreId($storeId); $chimpSyncEcommerce->setType($type); @@ -591,7 +592,6 @@ public function loadStores() if($store['platform']!=self::PLATFORM) { continue; } - $this->log($store); $mstore = $this->_mailChimpStoresFactory->create(); $mstore->setApikey(trim($apiKey)); $mstore->setStoreid($store['id']); diff --git a/Model/Api/Cart.php b/Model/Api/Cart.php index c7938ad3..a45330ed 100644 --- a/Model/Api/Cart.php +++ b/Model/Api/Cart.php @@ -104,7 +104,6 @@ public function __construct( */ public function createBatchJson($magentoStoreId) { - $this->_helper->log(__METHOD__); $allCarts = array(); if (!$this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_ABANDONEDCART_ACTIVE,$magentoStoreId)) { return $allCarts; @@ -135,7 +134,6 @@ public function createBatchJson($magentoStoreId) */ protected function _getConvertedQuotes($mailchimpStoreId, $magentoStoreId) { - $this->_helper->log(__METHOD__); $allCarts = array(); $convertedCarts = $this->_getQuoteCollection(); // get only the converted quotes @@ -152,7 +150,6 @@ protected function _getConvertedQuotes($mailchimpStoreId, $magentoStoreId) $convertedCarts->getSelect()->where("m4m.mailchimp_sync_deleted = 0"); // limit the collection $convertedCarts->getSelect()->limit(self::BATCH_LIMIT); - $this->_helper->log((string)$convertedCarts->getSelect()); /** * @var $cart \Magento\Quote\Model\Quote */ @@ -194,7 +191,6 @@ protected function _getConvertedQuotes($mailchimpStoreId, $magentoStoreId) */ protected function _getModifiedQuotes($mailchimpStoreId, $magentoStoreId) { - $this->_helper->log(__METHOD__); $allCarts = array(); $modifiedCarts = $this->_getQuoteCollection(); // select carts with no orders @@ -213,7 +209,6 @@ protected function _getModifiedQuotes($mailchimpStoreId, $magentoStoreId) " AND m4m.mailchimp_sync_delta < main_table.updated_at"); // limit the collection $modifiedCarts->getSelect()->limit(self::BATCH_LIMIT); - $this->_helper->log((string)$modifiedCarts->getSelect()); /** * @var $cart \Magento\Quote\Model\Quote */ @@ -288,7 +283,6 @@ protected function _getModifiedQuotes($mailchimpStoreId, $magentoStoreId) */ protected function _getNewQuotes($mailchimpStoreId, $magentoStoreId) { - $this->_helper->log(__METHOD__); $allCarts = array(); $newCarts = $this->_getQuoteCollection(); $newCarts->addFieldToFilter('is_active', array('eq'=>1)); @@ -311,32 +305,24 @@ protected function _getNewQuotes($mailchimpStoreId, $magentoStoreId) $newCarts->getSelect()->where("m4m.mailchimp_sync_delta IS NULL"); // limit the collection $newCarts->getSelect()->limit(self::BATCH_LIMIT); - $this->_helper->log((string)$newCarts->getSelect()); /** * @var $cart \Magento\Quote\Model\Quote */ foreach ($newCarts as $cart) { - $this->_helper->log('each cart'); $cartId = $cart->getEntityId(); $orderCollection = $this->_getOrderCollection(); $orderCollection->addFieldToFilter('main_table.customer_email', array('eq' => $cart->getCustomerEmail())) ->addFieldToFilter('main_table.updated_at', array('from' => $cart->getUpdatedAt())); //if cart is empty or customer has an order made after the abandonment skip current cart. - $this->_helper->log((string)$orderCollection->getSelect()); if (!count($cart->getAllVisibleItems()) || $orderCollection->getSize()) { - $this->_helper->log("cart is empty, leave"); $this->_updateQuote($mailchimpStoreId, $cartId, $this->_date->gmtDate()); continue; } - $this->_helper->log('before get the customer by email'); $customer = $this->_customerFactory->create(); $customer->setWebsiteId($magentoStoreId); $customer->loadByEmail($cart->getCustomerEmail()); - $this->_helper->log('after get customer by email'); if ($customer->getEmail() != $cart->getCustomerEmail()) { - $this->_helper->log('not same email'); - $this->_helper->log('before get all carts'); $allCartsForEmail = $this->_getAllCartsByEmail($cart->getCustomerEmail(), $mailchimpStoreId, $magentoStoreId); foreach ($allCartsForEmail as $cartForEmail) { $alreadySentCartId = $cartForEmail->getEntityId(); @@ -358,7 +344,6 @@ protected function _getNewQuotes($mailchimpStoreId, $magentoStoreId) } // send the products that not already sent - $this->_helper->log('before send modified products'); $productData = $this->_apiProduct->sendQuoteModifiedProduct($cart, $mailchimpStoreId, $magentoStoreId); if (count($productData)) { foreach($productData as $p) { @@ -366,7 +351,6 @@ protected function _getNewQuotes($mailchimpStoreId, $magentoStoreId) $this->_counter += 1; } } - $this->_helper->log('after send modified products'); $cartJson = $this->_makeCart($cart, $mailchimpStoreId, $magentoStoreId); if ($cartJson!="") { @@ -392,7 +376,6 @@ protected function _getNewQuotes($mailchimpStoreId, $magentoStoreId) */ protected function _getAllCartsByEmail($email, $mailchimpStoreId, $magentoStoreId) { - $this->_helper->log(__METHOD__); $allCartsForEmail = $this->_getQuoteCollection(); $allCartsForEmail->addFieldToFilter('is_active', array('eq' => 1)); $allCartsForEmail->addFieldToFilter('store_id', array('eq' => $magentoStoreId)); @@ -405,7 +388,6 @@ protected function _getAllCartsByEmail($email, $mailchimpStoreId, $magentoStoreI ); // be sure that the quotes are already in mailchimp and not deleted $allCartsForEmail->getSelect()->where("m4m.mailchimp_sync_deleted = 0"); - $this->_helper->log((string)$allCartsForEmail->getSelect()); return $allCartsForEmail; } @@ -417,17 +399,14 @@ protected function _getAllCartsByEmail($email, $mailchimpStoreId, $magentoStoreI */ protected function _makeCart(\Magento\Quote\Model\Quote $cart, $mailchimpStoreId, $magentoStoreId) { - $this->_helper->log(__METHOD__); $this->_token = null; $campaignId = $cart->getMailchimpCampaignId(); $oneCart = array(); $oneCart['id'] = $cart->getEntityId(); - $this->_helper->log('before get customer'); $oneCart['customer'] = $this->_getCustomer($cart, $mailchimpStoreId, $magentoStoreId); if ($campaignId) { $oneCart['campaign_id'] = $campaignId; } - $this->_helper->log('after get customer'); $oneCart['checkout_url'] = $this->_getCheckoutUrl($cart); $oneCart['currency_code'] = $cart->getQuoteCurrencyCode(); @@ -618,7 +597,6 @@ protected function _getOrderCollection() */ protected function _updateQuote($storeId, $entityId, $sync_delta, $sync_error='', $sync_modified=0, $sync_deleted=0) { - $this->_helper->log(__METHOD__); $this->_helper->saveEcommerceData($storeId, $entityId, $sync_delta, $sync_error, $sync_modified, \Ebizmarts\MailChimp\Helper\Data::IS_QUOTE, $sync_deleted, $this->_token); } diff --git a/Model/Api/Customer.php b/Model/Api/Customer.php index 95fe37ac..2f9359ea 100644 --- a/Model/Api/Customer.php +++ b/Model/Api/Customer.php @@ -94,13 +94,11 @@ public function sendCustomers($storeId) "OR (m4m.mailchimp_sync_delta > '".$this->_helper->getMCMinSyncDateFlag(). "' and m4m.mailchimp_sync_modified = 1)"); $collection->getSelect()->limit(self::MAX); - $this->_helper->log((string)$collection->getSelect()); $counter = 0; $customerArray = array(); foreach($collection as $item) { - $this->_helper->log('process each customer'); $customer = $this->_customerFactory->create(); $customer->getResource()->load($customer,$item->getId()); @@ -135,7 +133,6 @@ public function sendCustomers($storeId) */ protected function _buildCustomerData(\Magento\Customer\Model\Customer $customer) { - $this->_helper->log(__METHOD__); $point = 0; $data = array(); $data["id"] = $customer->getId(); @@ -149,7 +146,6 @@ protected function _buildCustomerData(\Magento\Customer\Model\Customer $customer array('neq',\Magento\Sales\Model\Order::STATE_CANCELED), array('neq',\Magento\Sales\Model\Order::STATE_CLOSED))) ->addAttributeToFilter('customer_id', array('eq' => $customer->getId())); - $this->_helper->log((string)$orderCollection->getSelect()); $totalOrders = 0; $totalAmountSpent = 0; /** @@ -163,7 +159,6 @@ protected function _buildCustomerData(\Magento\Customer\Model\Customer $customer $data['orders_count'] = $totalOrders; $data['total_spent'] = $totalAmountSpent; $address = $customer->getDefaultBillingAddress(); - $this->_helper->log('after get billing address'); if($address) { $customerAddress = array(); if ($street = $address->getStreet()) { @@ -175,30 +170,24 @@ protected function _buildCustomerData(\Magento\Customer\Model\Customer $customer $customerAddress["address2"] = $street[1]; } } - $this->_helper->log('after street'); if ($address->getCity()) { $customerAddress["city"] = $address->getCity(); } - $this->_helper->log('after city'); if ($address->getRegion()) { $customerAddress["province"] = $address->getRegion(); } - $this->_helper->log('after region'); if ($address->getRegionCode()) { $customerAddress["province_code"] = $address->getRegionCode(); } - $this->_helper->log('after region code'); if ($address->getPostcode()) { $customerAddress["postal_code"] = $address->getPostcode(); } - $this->_helper->log('after post code'); if ($address->getCountry()) { $country = $this->_countryInformation->getCountryInfo($address->getCountryId()); $countryName = $country->getFullNameLocale(); $customerAddress["country"] = $countryName; $customerAddress["country_code"] = $country->getTwoLetterAbbreviation(); } - $this->_helper->log('after country'); if (count($customerAddress)) { $data["address"] = $customerAddress; } @@ -210,7 +199,6 @@ protected function _buildCustomerData(\Magento\Customer\Model\Customer $customer // } // } } - $this->_helper->log('before return'); return $data; } diff --git a/Model/Api/Order.php b/Model/Api/Order.php index f5ca3182..1983a34c 100644 --- a/Model/Api/Order.php +++ b/Model/Api/Order.php @@ -128,7 +128,6 @@ public function __construct( */ public function sendOrders($magentoStoreId) { - $this->_helper->log(__METHOD__); $batchArray = array(); // get all the orders modified @@ -144,7 +143,6 @@ protected function _getCollection() } protected function _getModifiedOrders($magentoStoreId) { - $this->_helper->log(__METHOD__); $batchArray = array(); $mailchimpStoreId = $this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE,$magentoStoreId); $modifiedOrders = $this->_getCollection(); @@ -161,12 +159,10 @@ protected function _getModifiedOrders($magentoStoreId) $modifiedOrders->getSelect()->where("m4m.mailchimp_sync_modified = 1 AND m4m.mailchimp_store_id = '".$mailchimpStoreId."'"); // limit the collection $modifiedOrders->getSelect()->limit(self::BATCH_LIMIT); - $this->_helper->log((string)$modifiedOrders->getSelect()); /** * @var $order \Magento\Sales\Model\Order */ foreach ($modifiedOrders as $item) { - $this->_helper->log('inside the foreach'); try { $error = ''; $orderId = $item->getEntityId(); @@ -205,7 +201,6 @@ protected function _getModifiedOrders($magentoStoreId) protected function _getNewOrders($magentoStoreId) { - $this->_helper->log(__METHOD__); $batchArray = array(); $mailchimpStoreId = $this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE,$magentoStoreId); $newOrders = $this->_getCollection(); @@ -225,20 +220,15 @@ protected function _getNewOrders($magentoStoreId) $newOrders->getSelect()->where("m4m.mailchimp_sync_delta IS NULL"); // limit the collection $newOrders->getSelect()->limit(self::BATCH_LIMIT); - $this->_helper->log((string)$newOrders->getSelect()); /** * @var $order \Magento\Sales\Model\Order */ foreach ($newOrders as $item) { - $this->_helper->log('inside foreach'); try { $error = ''; - $this->_helper->log('before get id'); $orderId = $item->getEntityId(); - $this->_helper->log('before get the order'); - $order = $this->_order->get($orderId);; + $order = $this->_order->get($orderId); //create missing products first - $this->_helper->log('before send products'); $productData = $this->_apiProduct->sendModifiedProduct($order, $mailchimpStoreId); if (count($productData)) { foreach ($productData as $p) { @@ -246,7 +236,6 @@ protected function _getNewOrders($magentoStoreId) $this->_counter++; } } - $this->_helper->log('before generate order post'); $orderJson = $this->GeneratePOSTPayload($order, $mailchimpStoreId, $magentoStoreId); if (!empty($orderJson)) { $batchArray[$this->_counter]['method'] = "POST"; @@ -280,7 +269,6 @@ protected function _getNewOrders($magentoStoreId) */ protected function GeneratePOSTPayload(\Magento\Sales\Model\Order $order, $mailchimpStoreId, $magentoStoreId, $isModifiedOrder = false) { - $this->_helper->log(__METHOD__); $data = array(); $data['id'] = $order->getEntityId(); if ($order->getMailchimpCampaignId()) { @@ -290,14 +278,12 @@ protected function GeneratePOSTPayload(\Magento\Sales\Model\Order $order, $mailc if ($order->getMailchimpLandingPage()) { $data['landing_site'] = $order->getMailchimpLandingPage(); } - $this->_helper->log('before charge currency'); $data['currency_code'] = $order->getOrderCurrencyCode(); $data['order_total'] = $order->getGrandTotal(); $data['tax_total'] = $order->getTaxAmount(); $data['discount_total'] = abs($order->getDiscountAmount()); $data['shipping_total'] = $order->getShippingAmount(); $statusArray = $this->_getMailChimpStatus($order); - $this->_helper->log('before charge status'); if (isset($statusArray['financial_status'])) { $data['financial_status'] = $statusArray['financial_status']; } @@ -329,7 +315,6 @@ protected function GeneratePOSTPayload(\Magento\Sales\Model\Order $order, $mailc //order lines $items = $order->getAllVisibleItems(); - $this->_helper->log('before processing items'); $itemCount = 0; /** * @var $item \Magento\Sales\Model\Order\Item @@ -365,7 +350,6 @@ protected function GeneratePOSTPayload(\Magento\Sales\Model\Order $order, $mailc } //customer data - $this->_helper->log('before get the customer'); $api = $this->_helper->getApi(); $customers = array(); @@ -374,7 +358,6 @@ protected function GeneratePOSTPayload(\Magento\Sales\Model\Order $order, $mailc } catch (\Mailchimp_Error $e) { $this->_helper->log($e->getMessage()); } - $this->_helper->log('after get the customer'); if (!$isModifiedOrder) { if (isset($customers['total_items']) && $customers['total_items'] > 0) { @@ -416,7 +399,6 @@ protected function GeneratePOSTPayload(\Magento\Sales\Model\Order $order, $mailc } } // $store = Mage::getModel('core/store')->load($magentoStoreId); - $this->_helper->log('before charge customer data'); $data['order_url'] = $this->_urlHelper->getUrl( 'sales/order/view/', array( @@ -528,7 +510,6 @@ protected function GeneratePOSTPayload(\Magento\Sales\Model\Order $order, $mailc // } } //customer orders data - $this->_helper->log('before charge totals'); $orderCollection = $this->_orderCollectionFactory->create(); $orderCollection->addFieldToFilter('state', array( @@ -564,7 +545,6 @@ protected function GeneratePOSTPayload(\Magento\Sales\Model\Order $order, $mailc protected function _getMailChimpStatus(\Magento\Sales\Model\Order $order) { - $this->_helper->log(__METHOD__); $mailChimpFinancialStatus = null; $mailChimpFulfillmentStatus = null; $totalItemsOrdered = $order->getData('total_qty_ordered'); @@ -656,7 +636,6 @@ protected function _getMailChimpStatus(\Magento\Sales\Model\Order $order) protected function _updateOrder($storeId, $entityId, $sync_delta, $sync_error, $sync_modified) { - $this->_helper->log(__METHOD__); $this->_helper->saveEcommerceData($storeId, $entityId, $sync_delta, $sync_error, $sync_modified, \Ebizmarts\MailChimp\Helper\Data::IS_ORDER); } diff --git a/Model/Api/Product.php b/Model/Api/Product.php index 4f2116d9..227e1327 100644 --- a/Model/Api/Product.php +++ b/Model/Api/Product.php @@ -103,7 +103,6 @@ public function _sendProducts($storeId) $collection->getSelect()->where("m4m.mailchimp_sync_delta IS null OR (m4m.mailchimp_sync_delta > '".$this->_helper->getMCMinSyncDateFlag(). "' and m4m.mailchimp_sync_modified = 1)"); $collection->getSelect()->limit(self::MAX); - $this->_helper->log((string)$collection->getSelect()); foreach($collection as $item) { /** @@ -289,31 +288,26 @@ protected function _buildProductData(\Magento\Catalog\Model\Product $product, $ */ public function sendModifiedProduct(\Magento\Sales\Model\Order $order,$mailchimpStoreId) { - $this->_helper->log(__METHOD__); $data = array(); $batchId = \Ebizmarts\MailChimp\Helper\Data::IS_PRODUCT . '_' . $this->_date->gmtTimestamp(); $items = $order->getAllVisibleItems(); foreach ($items as $item) { - $this->_helper->log($item->getProductId()); //@todo get from the store not the default $product = $this->_productRepository->getById($item->getProductId()); $productSyncData = $this->_chimpSyncEcommerce->getByStoreIdType($mailchimpStoreId,$product->getId(), \Ebizmarts\MailChimp\Helper\Data::IS_PRODUCT); if ($product->getId()!=$item->getProductId()||$product->getTypeId()=='bundle'||$product->getTypeId()=='grouped') { - $this->_helper->log('continue is bundle/grouped'); continue; } if ($productSyncData->getMailchimpSyncModified() && $productSyncData->getMailchimpSyncDelta() > $this->_helper->getMCMinSyncDateFlag()) { - $this->_helper->log('is an old product'); $data[] = $this->_buildOldProductRequest($product,$batchId); $this->_updateProduct($mailchimpStoreId, $product->getId(), $this->_date->gmtDate(),'',0); } elseif (!$productSyncData->getMailchimpSyncDelta() || $productSyncData->getMailchimpSyncDelta() < $this->_helper->getMCMinSyncDateFlag()) { - $this->_helper->log('is a new product'); $data[] = $this->_buildNewProductRequest($product, $mailchimpStoreId); $this->_updateProduct($mailchimpStoreId, $product->getId(), $this->_date->gmtDate(),'',0); } @@ -323,7 +317,6 @@ public function sendModifiedProduct(\Magento\Sales\Model\Order $order,$mailchimp public function sendQuoteModifiedProduct(\Magento\Quote\Model\Quote $quote,$mailchimpStoreId) { - $this->_helper->log(__METHOD__); $data = array(); $batchId = \Ebizmarts\MailChimp\Helper\Data::IS_PRODUCT . '_' . $this->_date->gmtTimestamp(); $items = $quote->getAllVisibleItems(); @@ -332,25 +325,21 @@ public function sendQuoteModifiedProduct(\Magento\Quote\Model\Quote $quote,$mail */ foreach ($items as $item) { - $this->_helper->log($item->getProductId()); //@todo get from the store not the default $product = $this->_productRepository->getById($item->getProductId()); $productSyncData = $this->_chimpSyncEcommerce->getByStoreIdType($mailchimpStoreId,$product->getId(), \Ebizmarts\MailChimp\Helper\Data::IS_PRODUCT); if ($product->getId()!=$item->getProductId()||$product->getTypeId()=='bundle'||$product->getTypeId()=='grouped') { - $this->_helper->log('continue is bundle/grouped'); continue; } if ($productSyncData->getMailchimpSyncModified() && $productSyncData->getMailchimpSyncDelta() > $this->_helper->getMCMinSyncDateFlag()) { - $this->_helper->log('is an old product'); $data[] = $this->_buildOldProductRequest($product,$batchId); $this->_updateProduct($mailchimpStoreId, $product->getId(), $this->_date->gmtDate(),'',0); } elseif (!$productSyncData->getMailchimpSyncDelta() || $productSyncData->getMailchimpSyncDelta() < $this->_helper->getMCMinSyncDateFlag()) { - $this->_helper->log('is a new product'); $data[] = $this->_buildNewProductRequest($product, $mailchimpStoreId); $this->_updateProduct($mailchimpStoreId, $product->getId(), $this->_date->gmtDate(),'',0); } diff --git a/Model/Api/Result.php b/Model/Api/Result.php index 7f3e813d..d228313d 100644 --- a/Model/Api/Result.php +++ b/Model/Api/Result.php @@ -64,7 +64,7 @@ public function processResponses($storeId, $isMailChimpStoreId = false, $mailchi $item = null; foreach ($collection as $item) { try { - $storeId = ($isMailChimpStoreId) ? 0 : $storeId; +// $storeId = ($isMailChimpStoreId) ? 0 : $storeId; $files = $this->getBatchResponse($item->getBatchId(), $storeId); if (count($files)) { $this->processEachResponseFile($files, $item->getBatchId(), $mailchimpStoreId, $storeId); @@ -130,13 +130,13 @@ protected function processEachResponseFile($files, $batchId, $mailchimpStoreId , $line = explode('_', $item->operation_id); $type = $line[0]; $id = $line[2]; + $this->_helper->log("type [$type] id [$id] store[$storeId]"); $mailchimpErrors = $this->_chimpErrors->create(); //parse error $response = json_decode($item->response); $this->_helper->log($response); - $this->_helper->log("type [$type] id [$id]"); $errorDetails = ""; if (!empty($response->errors)) { foreach ($response->errors as $error) { @@ -154,7 +154,8 @@ protected function processEachResponseFile($files, $batchId, $mailchimpStoreId , /** * @var \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce $chimpSync */ - $chimpSync = $this->_helper->getChimpSyncEcommerce($storeId,$id,$type); + $chimpSync = $this->_helper->getChimpSyncEcommerce($mailchimpStoreId,$id,$type); + $this->_helper->log($chimpSync); $chimpSync->setData("mailchimp_sync_error", $error); $chimpSync->getResource()->save($chimpSync); $mailchimpErrors->setType($response->type); From 00026b459ac63e079324d6357b154c4e98295f24 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Tue, 2 May 2017 12:57:03 -0300 Subject: [PATCH 033/105] closes #11 --- Observer/Sales/Order/SubmitAfter.php | 75 ++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 Observer/Sales/Order/SubmitAfter.php diff --git a/Observer/Sales/Order/SubmitAfter.php b/Observer/Sales/Order/SubmitAfter.php new file mode 100644 index 00000000..24512281 --- /dev/null +++ b/Observer/Sales/Order/SubmitAfter.php @@ -0,0 +1,75 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 4/26/17 12:36 PM + * @file: SubmitAfter.php + */ +namespace Ebizmarts\MailChimp\Observer\Sales\Order; + +use Magento\Framework\Event\Observer; + +class SubmitAfter implements \Magento\Framework\Event\ObserverInterface +{ + /** + * @var \Magento\Framework\Stdlib\CookieManagerInterface + */ + protected $_cookieManager; + /** + * @var \Ebizmarts\MailChimp\Helper\Data + */ + protected $_helper; + /** + * @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory + */ + protected $_cookieMetadataFactory; + /** + * @var \Magento\Framework\Session\SessionManagerInterface + */ + protected $_sessionManager; + + /** + * SubmitAfter constructor. + * @param \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager + * @param \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $metadataFactory + * @param \Magento\Framework\Session\SessionManagerInterface $sessionManager + * @param \Ebizmarts\MailChimp\Helper\Data $helper + */ + public function __construct( + \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager, + \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $metadataFactory, + \Magento\Framework\Session\SessionManagerInterface $sessionManager, + \Ebizmarts\MailChimp\Helper\Data $helper + ) + { + $this->_cookieManager = $cookieManager; + $this->_helper = $helper; + $this->_cookieMetadataFactory = $metadataFactory; + $this->_sessionManager = $sessionManager; + } + + public function execute(\Magento\Framework\Event\Observer $observer) + { + $this->_cookieManager->deleteCookie( + 'mailchimp_campaign_id', + $this->_cookieMetadataFactory + ->createCookieMetadata() + ->setPath($this->_sessionManager->getCookiePath()) + ->setDomain($this->_sessionManager->getCookieDomain()) + ); + $this->_cookieManager->deleteCookie( + 'mailchimp_landing_page', + $this->_cookieMetadataFactory + ->createCookieMetadata() + ->setPath($this->_sessionManager->getCookiePath()) + ->setDomain($this->_sessionManager->getCookieDomain()) + ); + + return $this; + } +} \ No newline at end of file From 30c87ede23bf5942875b13dbcba746821a5c888c Mon Sep 17 00:00:00 2001 From: gonzalo Date: Tue, 2 May 2017 12:58:04 -0300 Subject: [PATCH 034/105] closes #16 --- Observer/Sales/Order/SaveAfter.php | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/Observer/Sales/Order/SaveAfter.php b/Observer/Sales/Order/SaveAfter.php index 62242453..05977956 100644 --- a/Observer/Sales/Order/SaveAfter.php +++ b/Observer/Sales/Order/SaveAfter.php @@ -17,20 +17,34 @@ class SaveAfter implements \Magento\Framework\Event\ObserverInterface { + /** + * @var \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce + */ protected $_ecommerce; + /** + * @var \Ebizmarts\MailChimp\Helper\Data + */ + protected $_helper; + /** * SaveAfter constructor. * @param \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce $ecommerce + * @param \Ebizmarts\MailChimp\Helper\Data $helper */ - public function __construct(\Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce $ecommerce) + public function __construct( + \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce $ecommerce, + \Ebizmarts\MailChimp\Helper\Data $helper + ) { - $this->_ecommerce = $ecommerce; + $this->_ecommerce = $ecommerce; + $this->_helper = $helper; } public function execute(\Magento\Framework\Event\Observer $observer) { $order = $observer->getEvent()->getOrder(); - $ecom = $this->_ecommerce->getByStoreIdType($order->getStoreId(),$order->getId(),'ORD'); + $mailchimpStoreId = $mailchimpStoreId = $this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE,$order->getStoreId()); + $ecom = $this->_ecommerce->getByStoreIdType($mailchimpStoreId,$order->getId(),'ORD'); if ($ecom) { $ecom->setMailchimpSyncModified(1); $ecom->getResource()->save($ecom); From f74e3ae88ff3f3797e0a240a2633163ca541b7b4 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Tue, 2 May 2017 13:28:57 -0300 Subject: [PATCH 035/105] Show the monkey in the order grid when comes from a campaign --- Ui/Component/Listing/Column/Monkey.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Ui/Component/Listing/Column/Monkey.php b/Ui/Component/Listing/Column/Monkey.php index e4bed59d..cff5ce0e 100644 --- a/Ui/Component/Listing/Column/Monkey.php +++ b/Ui/Component/Listing/Column/Monkey.php @@ -74,6 +74,9 @@ public function prepareDataSource(array $dataSource) $order = $this->_orderRepository->get($item["entity_id"]); $status = $order->getData("mailchimp_abandonedcart_flag"); + if ($order->getMailchimpCampaignId() || $order->getMailchimpLandingPage() ) { + $status = 1; + } $fieldName = $this->getData('name'); switch ($status) { From fa420abaec7a0446cc4fc1f00aa6dc057e2a7789 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Wed, 3 May 2017 11:22:57 -0300 Subject: [PATCH 036/105] closes #13 --- Model/Api/Order.php | 4 ++-- Model/Api/Result.php | 5 ++--- Setup/UpgradeSchema.php | 23 +++++++++++++++++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/Model/Api/Order.php b/Model/Api/Order.php index 1983a34c..5ed54b9e 100644 --- a/Model/Api/Order.php +++ b/Model/Api/Order.php @@ -168,7 +168,7 @@ protected function _getModifiedOrders($magentoStoreId) $orderId = $item->getEntityId(); $order = $this->_order->get($orderId); //create missing products first - $productData = $this->_apiProduct->sendModifiedProduct($order, $mailchimpStoreId); + $productData = $this->_apiProduct->sendModifiedProduct($order, $mailchimpStoreId,$magentoStoreId); if (count($productData)) { foreach ($productData as $p) { $batchArray[$this->_counter] = $p; @@ -229,7 +229,7 @@ protected function _getNewOrders($magentoStoreId) $orderId = $item->getEntityId(); $order = $this->_order->get($orderId); //create missing products first - $productData = $this->_apiProduct->sendModifiedProduct($order, $mailchimpStoreId); + $productData = $this->_apiProduct->sendModifiedProduct($order, $mailchimpStoreId,$magentoStoreId); if (count($productData)) { foreach ($productData as $p) { $batchArray[$this->_counter] = $p; diff --git a/Model/Api/Result.php b/Model/Api/Result.php index d228313d..4f93005c 100644 --- a/Model/Api/Result.php +++ b/Model/Api/Result.php @@ -130,13 +130,11 @@ protected function processEachResponseFile($files, $batchId, $mailchimpStoreId , $line = explode('_', $item->operation_id); $type = $line[0]; $id = $line[2]; - $this->_helper->log("type [$type] id [$id] store[$storeId]"); $mailchimpErrors = $this->_chimpErrors->create(); //parse error $response = json_decode($item->response); - $this->_helper->log($response); $errorDetails = ""; if (!empty($response->errors)) { foreach ($response->errors as $error) { @@ -155,7 +153,6 @@ protected function processEachResponseFile($files, $batchId, $mailchimpStoreId , * @var \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce $chimpSync */ $chimpSync = $this->_helper->getChimpSyncEcommerce($mailchimpStoreId,$id,$type); - $this->_helper->log($chimpSync); $chimpSync->setData("mailchimp_sync_error", $error); $chimpSync->getResource()->save($chimpSync); $mailchimpErrors->setType($response->type); @@ -166,6 +163,8 @@ protected function processEachResponseFile($files, $batchId, $mailchimpStoreId , $mailchimpErrors->setOriginalId($id); $mailchimpErrors->setBatchId($batchId); $mailchimpErrors->setMailchimpStoreId($mailchimpStoreId); + $mailchimpErrors->setOriginalId($id); + $mailchimpErrors->setBatchId($batchId); $mailchimpErrors->getResource()->save($mailchimpErrors); } } diff --git a/Setup/UpgradeSchema.php b/Setup/UpgradeSchema.php index 8bf6e763..bce527e8 100644 --- a/Setup/UpgradeSchema.php +++ b/Setup/UpgradeSchema.php @@ -221,6 +221,29 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con ] ); + } + if (version_compare($context->getVersion(), '1.0.8') < 0) { + $installer->getConnection()->addColumn( + $installer->getTable('mailchimp_errors'), + 'original_id', + [ + 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, + 'length' => 11, + 'default' => null, + 'comment' => 'Associated object ID' + ] + ); + $installer->getConnection()->addColumn( + $installer->getTable('mailchimp_errors'), + 'batch_id', + [ + 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 'length' => 64, + 'default' => null, + 'comment' => 'Mailchimp Batch ID' + ] + ); + } $installer->endSetup(); From 07ad74a11f242c525099dfa158fce212e883d386 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Wed, 3 May 2017 11:34:26 -0300 Subject: [PATCH 037/105] closes #13 closes #17 --- Model/Api/Product.php | 62 +++++++++++++++++++++++++++---------------- composer.json | 2 +- etc/module.xml | 2 +- 3 files changed, 41 insertions(+), 25 deletions(-) diff --git a/Model/Api/Product.php b/Model/Api/Product.php index 227e1327..b9af9114 100644 --- a/Model/Api/Product.php +++ b/Model/Api/Product.php @@ -39,9 +39,9 @@ class Product */ protected $_imageHelper; /** - * @var \Magento\CatalogInventory\Model\Stock\StockItemRepository + * @var \Magento\CatalogInventory\Api\StockRegistryInterface */ - protected $_stockItemRepository; + protected $_stockRegistry; /** * @var \Magento\Catalog\Model\CategoryRepository */ @@ -62,7 +62,7 @@ class Product * @param \Magento\Framework\Stdlib\DateTime\DateTime $date * @param \Ebizmarts\MailChimp\Helper\Data $helper * @param \Magento\Catalog\Helper\Image $imageHelper - * @param \Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItemRepository + * @param \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry * @param \Magento\Catalog\Model\CategoryRepository $categoryRepository * @param \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce $chimpSyncEcommerce */ @@ -72,7 +72,7 @@ public function __construct( \Magento\Framework\Stdlib\DateTime\DateTime $date, \Ebizmarts\MailChimp\Helper\Data $helper, \Magento\Catalog\Helper\Image $imageHelper, - \Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItemRepository, + \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry, \Magento\Catalog\Model\CategoryRepository $categoryRepository, \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce $chimpSyncEcommerce ) @@ -82,18 +82,18 @@ public function __construct( $this->_date = $date; $this->_productCollection = $productCollection; $this->_imageHelper = $imageHelper; - $this->_stockItemRepository = $stockItemRepository; + $this->_stockRegistry = $stockRegistry; $this->_categoryRepository = $categoryRepository; $this->_chimpSyncEcommerce = $chimpSyncEcommerce; $this->_batchId = \Ebizmarts\MailChimp\Helper\Data::IS_PRODUCT. '_' . $this->_date->gmtTimestamp(); } - public function _sendProducts($storeId) + public function _sendProducts($magentoStoreId) { $batchArray = array(); $counter = 0; - $mailchimpStoreId = $this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE,$storeId); + $mailchimpStoreId = $this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE,$magentoStoreId); $collection = $this->_getCollection(); - $collection->setStoreId($storeId); + $collection->setStoreId($magentoStoreId); $collection->getSelect()->joinLeft( ['m4m' => 'mailchimp_sync_ecommerce'], "m4m.related_id = e.entity_id and m4m.type = '".\Ebizmarts\MailChimp\Helper\Data::IS_PRODUCT. @@ -116,7 +116,7 @@ public function _sendProducts($storeId) $this->_updateProduct($mailchimpStoreId,$product->getId(),$this->_date->gmtDate(),"",0); continue; } else { - $data = $this->_buildNewProductRequest($product, $mailchimpStoreId); + $data = $this->_buildNewProductRequest($product, $mailchimpStoreId,$magentoStoreId); } if (!empty($data)) { $batchArray[$counter] = $data; @@ -139,7 +139,11 @@ protected function _getCollection() { return $this->_productCollection->create(); } - protected function _buildNewProductRequest(\Magento\Catalog\Model\Product $product, $mailchimpStoreId) + protected function _buildNewProductRequest( + \Magento\Catalog\Model\Product $product, + $mailchimpStoreId, + $magentoStoreId + ) { $variantProducts = array(); switch($product->getTypeId()) { @@ -164,7 +168,7 @@ protected function _buildNewProductRequest(\Magento\Catalog\Model\Product $produ default: return array(); } - $bodyData = $this->_buildProductData($product, false, $variantProducts); + $bodyData = $this->_buildProductData($product, $magentoStoreId, false, $variantProducts); try { $body = json_encode($bodyData,JSON_HEX_APOS|JSON_HEX_QUOT); @@ -182,13 +186,18 @@ protected function _buildNewProductRequest(\Magento\Catalog\Model\Product $produ $data['body'] = $body; return $data; } - protected function _buildOldProductRequest(\Magento\Catalog\Model\Product $product, $batchId, $mailchimpStoreId) + protected function _buildOldProductRequest( + \Magento\Catalog\Model\Product $product, + $batchId, + $mailchimpStoreId, + $magentoStoreId + ) { $operations = array(); if ($product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE || $product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_VIRTUAL || $product->getTypeId() == "downloadable") { - $data = $this->_buildProductData($product); + $data = $this->_buildProductData($product,$magentoStoreId); $parentIds = $product->getTypeInstance()->getParentIdsByChild($product->getId()); //$parentIds = Mage::getResourceSingleton('catalog/product_type_configurable')->getParentIdsByChild($product->getId()); @@ -227,7 +236,12 @@ protected function _buildOldProductRequest(\Magento\Catalog\Model\Product $produ } return $operations; } - protected function _buildProductData(\Magento\Catalog\Model\Product $product, $isVarient = true, $variants = null) + protected function _buildProductData( + \Magento\Catalog\Model\Product $product, + $magentoStoreId, + $isVarient = true, + $variants = null + ) { $data = array(); @@ -235,7 +249,9 @@ protected function _buildProductData(\Magento\Catalog\Model\Product $product, $ $data["id"] = $product->getId(); $data["title"] = $product->getName(); $data["url"] = $product->getProductUrl(); - $data["image_url"] =$this->_imageHelper->init($product,self::PRODUCTIMAGE)->setImageFile($product->getImage())->getUrl(); + if ($product->getImage()) { + $data["image_url"] = $this->_imageHelper->init($product,self::PRODUCTIMAGE)->setImageFile($product->getImage())->getUrl(); + } $data["published_at_foreign"] = ""; if ($isVarient) { //this is for a varient product @@ -243,7 +259,7 @@ protected function _buildProductData(\Magento\Catalog\Model\Product $product, $ $data["price"] = $product->getPrice(); //stock - $stock =$this->_stockItemRepository->get($product->getId()); + $stock = $this->_stockRegistry->getStockItem($product->getId(),$magentoStoreId); $data["inventory_quantity"] = (int)$stock->getQty(); $data["backorders"] = (string)$stock->getBackorders(); if ($product->getVisibility() != \Magento\Catalog\Model\Product\Visibility::VISIBILITY_NOT_VISIBLE) { @@ -273,7 +289,7 @@ protected function _buildProductData(\Magento\Catalog\Model\Product $product, $ //variants $data["variants"] = array(); foreach ($variants as $variant) { - $data["variants"][] = $this->_buildProductData($variant); + $data["variants"][] = $this->_buildProductData($variant,$magentoStoreId); } } @@ -286,7 +302,7 @@ protected function _buildProductData(\Magento\Catalog\Model\Product $product, $ * @return array * @throws \Magento\Framework\Exception\NoSuchEntityException */ - public function sendModifiedProduct(\Magento\Sales\Model\Order $order,$mailchimpStoreId) + public function sendModifiedProduct(\Magento\Sales\Model\Order $order,$mailchimpStoreId,$magentoStoreId) { $data = array(); $batchId = \Ebizmarts\MailChimp\Helper\Data::IS_PRODUCT . '_' . $this->_date->gmtTimestamp(); @@ -304,18 +320,18 @@ public function sendModifiedProduct(\Magento\Sales\Model\Order $order,$mailchimp if ($productSyncData->getMailchimpSyncModified() && $productSyncData->getMailchimpSyncDelta() > $this->_helper->getMCMinSyncDateFlag()) { - $data[] = $this->_buildOldProductRequest($product,$batchId); + $data[] = $this->_buildOldProductRequest($product,$batchId,$mailchimpStoreId,$magentoStoreId); $this->_updateProduct($mailchimpStoreId, $product->getId(), $this->_date->gmtDate(),'',0); } elseif (!$productSyncData->getMailchimpSyncDelta() || $productSyncData->getMailchimpSyncDelta() < $this->_helper->getMCMinSyncDateFlag()) { - $data[] = $this->_buildNewProductRequest($product, $mailchimpStoreId); + $data[] = $this->_buildNewProductRequest($product, $mailchimpStoreId,$magentoStoreId); $this->_updateProduct($mailchimpStoreId, $product->getId(), $this->_date->gmtDate(),'',0); } } return $data; } - public function sendQuoteModifiedProduct(\Magento\Quote\Model\Quote $quote,$mailchimpStoreId) + public function sendQuoteModifiedProduct(\Magento\Quote\Model\Quote $quote,$mailchimpStoreId,$magentoStoreId) { $data = array(); $batchId = \Ebizmarts\MailChimp\Helper\Data::IS_PRODUCT . '_' . $this->_date->gmtTimestamp(); @@ -336,11 +352,11 @@ public function sendQuoteModifiedProduct(\Magento\Quote\Model\Quote $quote,$mail if ($productSyncData->getMailchimpSyncModified() && $productSyncData->getMailchimpSyncDelta() > $this->_helper->getMCMinSyncDateFlag()) { - $data[] = $this->_buildOldProductRequest($product,$batchId); + $data[] = $this->_buildOldProductRequest($product,$batchId,$mailchimpStoreId,$magentoStoreId); $this->_updateProduct($mailchimpStoreId, $product->getId(), $this->_date->gmtDate(),'',0); } elseif (!$productSyncData->getMailchimpSyncDelta() || $productSyncData->getMailchimpSyncDelta() < $this->_helper->getMCMinSyncDateFlag()) { - $data[] = $this->_buildNewProductRequest($product, $mailchimpStoreId); + $data[] = $this->_buildNewProductRequest($product, $mailchimpStoreId,$magentoStoreId); $this->_updateProduct($mailchimpStoreId, $product->getId(), $this->_date->gmtDate(),'',0); } } diff --git a/composer.json b/composer.json index fda02129..c02489fb 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ }, "description": "Connect MailChimp with Magento", "type": "magento2-module", - "version": "1.0.7", + "version": "1.0.8", "authors": [ { "name": "Ebizmarts Corp", diff --git a/etc/module.xml b/etc/module.xml index ea2eb242..77971f73 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -11,7 +11,7 @@ */ --> - + From 251adfeb8e664e4118dcbeadd0dc630f0f140287 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Wed, 3 May 2017 12:20:45 -0300 Subject: [PATCH 038/105] add catcher block --- Block/Catcher.php | 25 +++++++++++++++++++++++++ composer.json | 2 +- etc/module.xml | 2 +- 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 Block/Catcher.php diff --git a/Block/Catcher.php b/Block/Catcher.php new file mode 100644 index 00000000..b60db9bb --- /dev/null +++ b/Block/Catcher.php @@ -0,0 +1,25 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 4/25/17 3:35 PM + * @file: Catcher.php + */ +namespace Ebizmarts\MailChimp\Block; + +use Magento\Framework\View\Element\Template; + +class Catcher extends Template +{ + /** + * @var string $_template + */ + protected $_template = "catcher.phtml"; + + // write your methods here... +} \ No newline at end of file diff --git a/composer.json b/composer.json index c02489fb..75b482e9 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ }, "description": "Connect MailChimp with Magento", "type": "magento2-module", - "version": "1.0.8", + "version": "1.0.9", "authors": [ { "name": "Ebizmarts Corp", diff --git a/etc/module.xml b/etc/module.xml index 77971f73..ed4df02a 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -11,7 +11,7 @@ */ --> - + From d013f84087e67ebdc2d7cbf4c5d227e794e2a63c Mon Sep 17 00:00:00 2001 From: gonzalo Date: Thu, 4 May 2017 15:41:23 -0300 Subject: [PATCH 039/105] closes #8 --- Controller/Adminhtml/Errors/Getresponse.php | 97 +++++++++++++++++++ Model/Api/Result.php | 5 +- Setup/UpgradeSchema.php | 16 ++- Ui/Component/Errors/Grid/Column/Batch.php | 66 +++++++++++++ composer.json | 2 +- etc/module.xml | 2 +- .../ui_component/mailchimp_errors_grid.xml | 9 ++ 7 files changed, 189 insertions(+), 8 deletions(-) create mode 100644 Controller/Adminhtml/Errors/Getresponse.php create mode 100644 Ui/Component/Errors/Grid/Column/Batch.php diff --git a/Controller/Adminhtml/Errors/Getresponse.php b/Controller/Adminhtml/Errors/Getresponse.php new file mode 100644 index 00000000..4d2b4f7f --- /dev/null +++ b/Controller/Adminhtml/Errors/Getresponse.php @@ -0,0 +1,97 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 5/3/17 3:28 PM + * @file: Getresponse.php + */ + +namespace Ebizmarts\MailChimp\Controller\Adminhtml\Errors; + +use Magento\Framework\Controller\ResultFactory; + +class Getresponse extends \Magento\Backend\App\Action +{ + /** + * @var ResultFactory + */ + protected $_resultFactory; + /** + * @var \Ebizmarts\MailChimp\Model\MailChimpErrorsFactory + */ + protected $_errorsFactory; + /** + * @var \Ebizmarts\MailChimp\Model\Api\Result + */ + protected $_result; + /** + * @var \Ebizmarts\MailChimp\Helper\Data + */ + protected $_helper; + + /** + * Getresponse constructor. + * @param \Magento\Backend\App\Action\Context $context + * @param ResultFactory $resultFactory + * @param \Ebizmarts\MailChimp\Model\MailChimpErrorsFactory $errorsFactory + * @param \Ebizmarts\MailChimp\Helper\Data $helper + * @param \Ebizmarts\MailChimp\Model\Api\Result $result + */ + public function __construct( + \Magento\Backend\App\Action\Context $context, + \Magento\Framework\Controller\ResultFactory $resultFactory, + \Ebizmarts\MailChimp\Model\MailChimpErrorsFactory $errorsFactory, + \Ebizmarts\MailChimp\Helper\Data $helper, + \Ebizmarts\MailChimp\Model\Api\Result $result + ) { + parent::__construct($context); + $this->_resultFactory = $resultFactory; + $this->_errorsFactory = $errorsFactory; + $this->_result = $result; + $this->_helper = $helper; + } + + public function execute() + { + $errorId = $this->getRequest()->getParam('id'); + $errors = $this->_errorsFactory->create(); + $errors->getResource()->load($errors, $errorId); + $batchId = $errors->getBatchId(); + $files = $this->_result->getBatchResponse($batchId,$errors->getStoreId()); + $fileContent = []; + foreach ($files as $file) + { + $items = json_decode(file_get_contents($file)); + foreach ($items as $item) + { + $content = array( + 'status_code'=>$item->status_code, + 'operation_id'=>$item->operation_id, + 'response'=>json_decode($item->response) + ); + $fileContent[] = $content; + } + unlink($file); + } + $resultJson =$this->_resultFactory->create(ResultFactory::TYPE_JSON); + $resultJson->setHeader('Content-disposition', 'attachment; filename='.$batchId.'.json'); + $resultJson->setHeader('Content-type', 'application/json'); + $data = json_encode($fileContent, JSON_PRETTY_PRINT); + $resultJson->setJsonData($data); + $baseDir = $this->_helper->getBaseDir(); + if (is_dir($baseDir . DIRECTORY_SEPARATOR . 'var' . DIRECTORY_SEPARATOR . \Ebizmarts\MailChimp\Model\Api\Result::MAILCHIMP_TEMP_DIR . DIRECTORY_SEPARATOR . $batchId)) { + rmdir($baseDir . DIRECTORY_SEPARATOR . 'var' . DIRECTORY_SEPARATOR . \Ebizmarts\MailChimp\Model\Api\Result::MAILCHIMP_TEMP_DIR . DIRECTORY_SEPARATOR . $batchId); + } + return $resultJson; + } + protected function _isAllowed() + { + return $this->_authorization->isAllowed('Ebizmarts_MailChimp::errors_admin_view'); + } + +} \ No newline at end of file diff --git a/Model/Api/Result.php b/Model/Api/Result.php index 4f93005c..30ecaa3f 100644 --- a/Model/Api/Result.php +++ b/Model/Api/Result.php @@ -80,12 +80,12 @@ public function processResponses($storeId, $isMailChimpStoreId = false, $mailchi } } } - protected function getBatchResponse($batchId, $storeId = 0) + public function getBatchResponse($batchId, $storeId = null) { $files = array(); try { $baseDir = $this->_helper->getBaseDir(); - $api = $this->_helper->getApi(); + $api = $this->_helper->getApi($storeId); // check the status of the job $response = $api->batchOperation->status($batchId); if (isset($response['status']) && $response['status'] == 'finished') { @@ -165,6 +165,7 @@ protected function processEachResponseFile($files, $batchId, $mailchimpStoreId , $mailchimpErrors->setMailchimpStoreId($mailchimpStoreId); $mailchimpErrors->setOriginalId($id); $mailchimpErrors->setBatchId($batchId); + $mailchimpStoreId->setStoreId($storeId); $mailchimpErrors->getResource()->save($mailchimpErrors); } } diff --git a/Setup/UpgradeSchema.php b/Setup/UpgradeSchema.php index bce527e8..d5dccdd6 100644 --- a/Setup/UpgradeSchema.php +++ b/Setup/UpgradeSchema.php @@ -245,10 +245,18 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con ); } + if (version_compare($context->getVersion(), '1.0.10') < 0) { + $installer->getConnection()->addColumn( + $installer->getTable('mailchimp_errors'), + 'store_id', + [ + 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, + 'length' => 11, + 'default' => null, + 'comment' => 'Magento Store Id' + ] + ); + } $installer->endSetup(); - - - - } } \ No newline at end of file diff --git a/Ui/Component/Errors/Grid/Column/Batch.php b/Ui/Component/Errors/Grid/Column/Batch.php new file mode 100644 index 00000000..30cb263f --- /dev/null +++ b/Ui/Component/Errors/Grid/Column/Batch.php @@ -0,0 +1,66 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 5/3/17 3:10 PM + * @file: Batch.php + */ + +namespace Ebizmarts\MailChimp\Ui\Component\Errors\Grid\Column; + +use Magento\Framework\View\Element\UiComponent\ContextInterface; +use Magento\Framework\View\Element\UiComponentFactory; +use Magento\Ui\Component\Listing\Columns\Column; +use Magento\Framework\UrlInterface; + +class Batch extends Column +{ + /** + * @var UrlInterface + */ + protected $urlBuilder; + + /** + * Batch constructor. + * @param ContextInterface $context + * @param UiComponentFactory $uiComponentFactory + * @param UrlInterface $urlBuilder + * @param array $components + * @param array $data + */ + public function __construct( + ContextInterface $context, + UiComponentFactory $uiComponentFactory, + UrlInterface $urlBuilder, + array $components = [], + array $data = [] + ) { + $this->urlBuilder = $urlBuilder; + parent::__construct($context, $uiComponentFactory, $components, $data); + } + public function prepareDataSource(array $dataSource) + { + if (isset($dataSource['data']['items'])) { + + foreach ($dataSource['data']['items'] as &$item) { + $item[$this->getData('name')]['batch_id'] = [ + 'href' => $this->urlBuilder->getUrl( + 'mailchimp/errors/getresponse', + ['id' => $item['id']] + ), + 'label' => $item['batch_id'], + 'hidden' => false, + ]; + } + } + + return $dataSource; + } + + +} diff --git a/composer.json b/composer.json index 75b482e9..f945ded3 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ }, "description": "Connect MailChimp with Magento", "type": "magento2-module", - "version": "1.0.9", + "version": "1.0.10", "authors": [ { "name": "Ebizmarts Corp", diff --git a/etc/module.xml b/etc/module.xml index ed4df02a..6746a632 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -11,7 +11,7 @@ */ --> - + diff --git a/view/adminhtml/ui_component/mailchimp_errors_grid.xml b/view/adminhtml/ui_component/mailchimp_errors_grid.xml index 5e95bb5c..c0aa49cb 100644 --- a/view/adminhtml/ui_component/mailchimp_errors_grid.xml +++ b/view/adminhtml/ui_component/mailchimp_errors_grid.xml @@ -113,6 +113,15 @@ + + + + id + Download Response + + + + From db213552fa4be786f668724a60784845bf08628e Mon Sep 17 00:00:00 2001 From: gonzalo Date: Thu, 4 May 2017 15:58:10 -0300 Subject: [PATCH 040/105] closes #19 --- Block/Adminhtml/Stores/Edit/Tabs.php | 2 +- Controller/Adminhtml/Stores/Edit.php | 2 +- view/adminhtml/ui_component/mailchimp_stores_grid.xml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Block/Adminhtml/Stores/Edit/Tabs.php b/Block/Adminhtml/Stores/Edit/Tabs.php index d14d13bb..7a643809 100644 --- a/Block/Adminhtml/Stores/Edit/Tabs.php +++ b/Block/Adminhtml/Stores/Edit/Tabs.php @@ -27,7 +27,7 @@ protected function _construct() parent::_construct(); $this->setId('stores_edit_tabs'); $this->setDestElementId('edit_form'); - $this->setTitle(__('Mailchimp Store Information')); + $this->setTitle(__('MailChimp Store Information')); } /** diff --git a/Controller/Adminhtml/Stores/Edit.php b/Controller/Adminhtml/Stores/Edit.php index 50bfad71..8a2ed95c 100644 --- a/Controller/Adminhtml/Stores/Edit.php +++ b/Controller/Adminhtml/Stores/Edit.php @@ -40,7 +40,7 @@ public function execute() /** @var \Magento\Backend\Model\View\Result\Page $resultPage */ $resultPage = $this->_resultPageFactory->create(); $resultPage->setActiveMenu('Ebizmarts_MailChimp::main_menu'); - $resultPage->getConfig()->getTitle()->prepend(__('Mailchimp Store')); + $resultPage->getConfig()->getTitle()->prepend(__('MailChimp Store')); return $resultPage; } diff --git a/view/adminhtml/ui_component/mailchimp_stores_grid.xml b/view/adminhtml/ui_component/mailchimp_stores_grid.xml index 0d1b148d..7e714c38 100644 --- a/view/adminhtml/ui_component/mailchimp_stores_grid.xml +++ b/view/adminhtml/ui_component/mailchimp_stores_grid.xml @@ -10,7 +10,7 @@ add - Create New Mailchimp Store + Create New MailChimp Store primary mailchimp/stores/edit From a11cf8fa756825fa40a9c97ae47fda6a46e2bc5c Mon Sep 17 00:00:00 2001 From: gonzalo Date: Thu, 4 May 2017 16:02:35 -0300 Subject: [PATCH 041/105] closes #20 --- Controller/Adminhtml/Lists/Get.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Controller/Adminhtml/Lists/Get.php b/Controller/Adminhtml/Lists/Get.php index 3883a073..040fc43e 100644 --- a/Controller/Adminhtml/Lists/Get.php +++ b/Controller/Adminhtml/Lists/Get.php @@ -18,6 +18,8 @@ class Get extends Action { + const MAX_LISTS = 200; + /** * @var \Ebizmarts\MailChimp\Helper\Data */ @@ -49,7 +51,7 @@ public function execute() $param = $this->getRequest()->getParams(); $apiKey = $param['apikey']; $api = $this->_helper->getApiByApiKey($apiKey); - $lists = $api->lists->getLists(); + $lists = $api->lists->getLists(null,null,null,self::MAX_LISTS); $result = []; foreach($lists['lists'] as $list) { $result[] = ['id'=> $list['id'], 'name'=> $list['name']]; From ba7c4c03c6f734fe54268d0e9928b17f6f655b76 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Fri, 5 May 2017 10:37:58 -0300 Subject: [PATCH 042/105] remove referral from admin --- view/adminhtml/templates/system/config/fieldset/hint.phtml | 1 - 1 file changed, 1 deletion(-) diff --git a/view/adminhtml/templates/system/config/fieldset/hint.phtml b/view/adminhtml/templates/system/config/fieldset/hint.phtml index b09583c8..e31a105e 100644 --- a/view/adminhtml/templates/system/config/fieldset/hint.phtml +++ b/view/adminhtml/templates/system/config/fieldset/hint.phtml @@ -16,7 +16,6 @@

getModuleVersion() ?> ebizmarts

-

\ No newline at end of file From bdd3d8b1b3325855219fa7a8e3e60769343cee36 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Fri, 5 May 2017 11:35:56 -0300 Subject: [PATCH 043/105] closes #21 --- Block/Adminhtml/Stores/Edit/Tab/Address.php | 8 ++--- Block/Adminhtml/Stores/Edit/Tab/Info.php | 13 ++++++-- Controller/Adminhtml/Stores/Save.php | 6 ++-- Helper/Data.php | 5 +-- Setup/UpgradeSchema.php | 36 +++++++++++++++++++++ composer.json | 2 +- etc/module.xml | 2 +- 7 files changed, 59 insertions(+), 13 deletions(-) diff --git a/Block/Adminhtml/Stores/Edit/Tab/Address.php b/Block/Adminhtml/Stores/Edit/Tab/Address.php index 9ae1b7c6..4d036e43 100644 --- a/Block/Adminhtml/Stores/Edit/Tab/Address.php +++ b/Block/Adminhtml/Stores/Edit/Tab/Address.php @@ -64,19 +64,19 @@ protected function _prepareForm() ); } $fieldset->addField( - 'address_address1', + 'address_address_one', 'text', [ - 'name' => 'address_address1', + 'name' => 'address_address_one', 'label' => __('Street'), 'required' => true ] ); $fieldset->addField( - 'address_address2', + 'address_address_two', 'text', [ - 'name' => 'address_address2', + 'name' => 'address_address_two', 'label' => __('Street'), 'required' => false ] diff --git a/Block/Adminhtml/Stores/Edit/Tab/Info.php b/Block/Adminhtml/Stores/Edit/Tab/Info.php index 8a3e5653..f091a4b6 100644 --- a/Block/Adminhtml/Stores/Edit/Tab/Info.php +++ b/Block/Adminhtml/Stores/Edit/Tab/Info.php @@ -135,8 +135,6 @@ protected function _prepareForm() } - - $fieldset->addField( 'name', 'text', @@ -146,6 +144,7 @@ protected function _prepareForm() 'required' => true ] ); + $fieldset->addField( 'is_sync', 'select', @@ -157,6 +156,16 @@ protected function _prepareForm() ] ); + $fieldset->addField( + 'domain', + 'text', + [ + 'name' => 'domain', + 'label' => __('Domain'), + 'required' => true + ] + ); + $fieldset->addField( 'email_address', 'text', diff --git a/Controller/Adminhtml/Stores/Save.php b/Controller/Adminhtml/Stores/Save.php index ee4df697..b676dc23 100644 --- a/Controller/Adminhtml/Stores/Save.php +++ b/Controller/Adminhtml/Stores/Save.php @@ -53,8 +53,8 @@ protected function _updateMailchimp($formData) $api = $this->_helper->getApiByApiKey($formData['apikey']); // set the address $address = []; - $address['address1'] = $formData['address_address1']; - $address['address2'] = $formData['address_address2']; + $address['address1'] = $formData['address_address_one']; + $address['address2'] = $formData['address_address_two']; $address['city'] = $formData['address_city']; $address['province'] = ''; $address['province_code'] = ''; @@ -68,7 +68,7 @@ protected function _updateMailchimp($formData) $timeZone = $formData['timezone']; $phone = $formData['phone']; $name = $formData['name']; - $domain = 'domain'; + $domain = $formData['domain']; $storeId = isset($formData['storeid']) ? $formData['storeid'] : null; if ($storeId) { diff --git a/Helper/Data.php b/Helper/Data.php index ed42b187..dc4f3c8e 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -600,13 +600,14 @@ public function loadStores() $mstore->setPlatform($store['platform']); $mstore->setIsSync($store['is_syncing']); $mstore->setEmailAddress($store['email_address']); + $mstore->setDomain($store['domain']); $mstore->setCurrencyCode($store['currency_code']); // $mstore->setMoneyFormat($store['money_format']); $mstore->setPrimaryLocale($store['primary_locale']); $mstore->setTimezone($store['timezone']); $mstore->setPhone($store['phone']); - $mstore->setAddressAddress1($store['address']['address1']); - $mstore->setAddressAddress2($store['address']['address2']); + $mstore->setAddressAddressOne($store['address']['address1']); + $mstore->setAddressAddressTwo($store['address']['address2']); $mstore->setAddressCity($store['address']['city']); $mstore->setAddressProvince($store['address']['province']); $mstore->setAddressProvinceCode($store['address']['province_code']); diff --git a/Setup/UpgradeSchema.php b/Setup/UpgradeSchema.php index d5dccdd6..67b3eb50 100644 --- a/Setup/UpgradeSchema.php +++ b/Setup/UpgradeSchema.php @@ -257,6 +257,42 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con ] ); } + if (version_compare($context->getVersion(), '1.0.11') < 0) { + $installer->getConnection()->addColumn( + $installer->getTable('mailchimp_stores'), + 'domain', + [ + 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 'length' => 512, + 'default' => null, + 'comment' => 'Domain' + ] + ); + } + if (version_compare($context->getVersion(), '1.0.12') < 0) { + $installer->getConnection()->changecolumn( + $installer->getTable('mailchimp_stores'), + 'address_address1', + 'address_address_one', + [ + 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 'length' => 255, + 'default' => null, + 'comment' => 'first street address' + ] + ); + $installer->getConnection()->changecolumn( + $installer->getTable('mailchimp_stores'), + 'address_address2', + 'address_address_two', + [ + 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 'length' => 255, + 'default' => null, + 'comment' => 'second street address' + ] + ); + } $installer->endSetup(); } } \ No newline at end of file diff --git a/composer.json b/composer.json index f945ded3..93b06469 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ }, "description": "Connect MailChimp with Magento", "type": "magento2-module", - "version": "1.0.10", + "version": "1.0.12", "authors": [ { "name": "Ebizmarts Corp", diff --git a/etc/module.xml b/etc/module.xml index 6746a632..405d7c81 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -11,7 +11,7 @@ */ --> - + From fefe5c27729d640b02cb5d113922a16f937787fd Mon Sep 17 00:00:00 2001 From: gonzalo Date: Fri, 5 May 2017 12:09:59 -0300 Subject: [PATCH 044/105] closes #23 --- etc/adminhtml/system.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index aa48f599..7b56595e 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -41,7 +41,6 @@ Ebizmarts\MailChimp\Model\Config\Source\ApiKey Ebizmarts\MailChimp\Model\Config\Backend\ApiKey - Caution, if you change the API Key the old mailchimp store will be deleted 1 From 7a7d095ffea4c381bb931eb4d44ef0afda565d25 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Fri, 5 May 2017 21:05:09 -0300 Subject: [PATCH 045/105] closes #27 --- Block/Adminhtml/System/Config/Account.php | 5 +- .../Adminhtml/Ecommerce/Getaccountdetails.php | 82 ++++++++++++ Controller/Adminhtml/Stores/Get.php | 65 ++++++++++ view/adminhtml/requirejs-config.js | 3 +- .../system/config/fieldset/hint.phtml | 3 + view/adminhtml/web/js/configapikey.js | 120 ++++++++++++++++++ 6 files changed, 275 insertions(+), 3 deletions(-) create mode 100644 Controller/Adminhtml/Ecommerce/Getaccountdetails.php create mode 100644 Controller/Adminhtml/Stores/Get.php create mode 100644 view/adminhtml/web/js/configapikey.js diff --git a/Block/Adminhtml/System/Config/Account.php b/Block/Adminhtml/System/Config/Account.php index e16fd31b..7791dd14 100644 --- a/Block/Adminhtml/System/Config/Account.php +++ b/Block/Adminhtml/System/Config/Account.php @@ -16,8 +16,8 @@ class Account extends \Magento\Config\Block\System\Config\Form\Field protected function _getElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element) { $values = $element->getValues(); - - $html = '
    '; + $html = '
    '; + $html .= '
      '; if ($values) { foreach ($values as $dat) { $html .= "
    • {$dat['value']}: {$dat['label']}
    • "; @@ -25,6 +25,7 @@ protected function _getElementHtml(\Magento\Framework\Data\Form\Element\Abstract } $html .= '
    '; + $html .= '
    '; return $html; } diff --git a/Controller/Adminhtml/Ecommerce/Getaccountdetails.php b/Controller/Adminhtml/Ecommerce/Getaccountdetails.php new file mode 100644 index 00000000..ebbf1657 --- /dev/null +++ b/Controller/Adminhtml/Ecommerce/Getaccountdetails.php @@ -0,0 +1,82 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 5/5/17 3:40 PM + * @file: GetAccountDetails.php + */ + +namespace Ebizmarts\MailChimp\Controller\Adminhtml\Ecommerce; + +use Magento\Backend\App\Action; +use Magento\Backend\App\Action\Context; +use Magento\Framework\Controller\ResultFactory; + +class Getaccountdetails extends Action +{ + /** + * @var \Ebizmarts\MailChimp\Helper\Data + */ + protected $_helper; + /** + * @var ResultFactory + */ + protected $_resultFactory; + + /** + * GetAccountDetails constructor. + * @param Context $context + * @param ResultFactory $resultFactory + * @param \Ebizmarts\MailChimp\Helper\Data $helper + */ + public function __construct( + Context $context, + ResultFactory $resultFactory, + \Ebizmarts\MailChimp\Helper\Data $helper + ) + { + parent::__construct($context); + $this->_resultFactory = $resultFactory; + $this->_helper = $helper; + } + public function execute() + { + $param = $this->getRequest()->getParams(); + $apiKey = $param['apikey']; + $store = $param['store']; + $this->_helper->log($apiKey); + $this->_helper->log($store); + + $api = $this->_helper->getApiByApiKey($apiKey); + $apiInfo = $api->root->info(); + $options = []; + if(isset($apiInfo['account_name'])) { + $options['username'] = ['label' => __('User name:'), 'value' => $apiInfo['account_name']]; + $options['total_subscribers'] = ['label'=> __('Total Subscribers:'), 'value' => $apiInfo['total_subscribers']]; + if($store != -1) { + $options['subtitle'] = ['label'=> __('Ecommerce Data uploaded to MailChimp:'), 'value' =>'']; + $totalCustomers = $api->ecommerce->customers->getAll($store, 'total_items'); + $options['total_customers'] = ['label' => __('Total customers:'), 'value' => $totalCustomers['total_items']]; + $totalProducts = $api->ecommerce->products->getAll($store, 'total_items'); + $options['total_products'] = ['label'=> __('Total products:'), 'value' =>$totalProducts['total_items']]; + $totalOrders = $api->ecommerce->orders->getAll($store, 'total_items'); + $options['total_orders'] = ['label'=> __('Total orders:'), 'value' =>$totalOrders['total_items']]; + $totalCarts = $api->ecommerce->carts->getAll($store, 'total_items'); + $options['total_carts'] = ['label'=> __('Total Carts:'), 'value' =>$totalCarts['total_items']]; + + } + } + + + $resultJson = $this->_resultFactory->create(ResultFactory::TYPE_JSON); + $resultJson->setData($options); + return $resultJson; + } + + +} \ No newline at end of file diff --git a/Controller/Adminhtml/Stores/Get.php b/Controller/Adminhtml/Stores/Get.php new file mode 100644 index 00000000..0eb1a18d --- /dev/null +++ b/Controller/Adminhtml/Stores/Get.php @@ -0,0 +1,65 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 4/20/17 3:20 PM + * @file: Get.php + */ +namespace Ebizmarts\MailChimp\Controller\Adminhtml\Stores; + +use Magento\Backend\App\Action; +use Magento\Backend\App\Action\Context; +use Magento\Framework\Controller\ResultFactory; + +class Get extends Action +{ + const MAX_STORES = 200; + + /** + * @var \Ebizmarts\MailChimp\Helper\Data + */ + protected $_helper; + /** + * @var ResultFactory + */ + protected $_resultFactory; + + /** + * Get constructor. + * @param Context $context + * @param ResultFactory $resultFactory + * @param \Ebizmarts\MailChimp\Helper\Data $helper + */ + public function __construct( + Context $context, + ResultFactory $resultFactory, + \Ebizmarts\MailChimp\Helper\Data $helper + ) + { + parent::__construct($context); + $this->_resultFactory = $resultFactory; + $this->_helper = $helper; + + } + public function execute() + { + $param = $this->getRequest()->getParams(); + $apiKey = $param['apikey']; + $api = $this->_helper->getApiByApiKey($apiKey); + $stores = $api->ecommerce->stores->get(null,null,null,self::MAX_STORES); + $result = []; + foreach($stores['stores'] as $store) { + if($store['platform']==\Ebizmarts\MailChimp\Helper\Data::PLATFORM) { + $result[] = ['id' => $store['id'], 'name' => $store['name']]; + } + } + $resultJson = $this->_resultFactory->create(ResultFactory::TYPE_JSON); + $resultJson->setData($result); + return $resultJson; + } +} \ No newline at end of file diff --git a/view/adminhtml/requirejs-config.js b/view/adminhtml/requirejs-config.js index 07315750..86b953a3 100644 --- a/view/adminhtml/requirejs-config.js +++ b/view/adminhtml/requirejs-config.js @@ -11,7 +11,8 @@ var config = { "map": { "*": { - monkeyapikey: 'Ebizmarts_MailChimp/js/apikey' + monkeyapikey: 'Ebizmarts_MailChimp/js/apikey', + configmonkeyapikey: 'Ebizmarts_MailChimp/js/configapikey' } } }; \ No newline at end of file diff --git a/view/adminhtml/templates/system/config/fieldset/hint.phtml b/view/adminhtml/templates/system/config/fieldset/hint.phtml index e31a105e..5254b26d 100644 --- a/view/adminhtml/templates/system/config/fieldset/hint.phtml +++ b/view/adminhtml/templates/system/config/fieldset/hint.phtml @@ -9,6 +9,9 @@ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) */ ?> +



    diff --git a/view/adminhtml/web/js/configapikey.js b/view/adminhtml/web/js/configapikey.js new file mode 100644 index 00000000..b8173bb2 --- /dev/null +++ b/view/adminhtml/web/js/configapikey.js @@ -0,0 +1,120 @@ +/** + * Ebizmarts_MailChimp Magento JS component + * + * @category Ebizmarts + * @package Ebizmarts_MailChimp + * @author Ebizmarts Team + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + */ +define([ + 'jquery' +], + function($){ + "use strict"; + + $.widget('mage.configmonkeyapikey', { + "options": { + "storeUrl": "", + "detailsUrl": "" + }, + + _init: function () { + var self = this; + $('#mailchimp_general_apikey').change(function() { + var apiKey = $('#mailchimp_general_apikey').find(':selected').val(); + self._loadStores(apiKey); + }); + $('#mailchimp_general_apikeylist').change(function() { + var oldApiKey = $('#mailchimp_general_apikey').find(':selected').val(); + self._loadApiKeys(oldApiKey); + }); + $('#mailchimp_general_monkeystore').change(function() { + self._loadDetails(); + }); + }, + + _loadStores: function (apiKey) { + var storeUrl = this.options.storeUrl; + // remove all items in list combo + $('#mailchimp_general_monkeystore').empty(); + // get the selected apikey + $('#mailchimp_general_monkeystore').append($('