Skip to content

Commit

Permalink
sync carts
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzaloebiz committed Mar 14, 2017
1 parent 90894b1 commit 9683b0f
Show file tree
Hide file tree
Showing 21 changed files with 1,458 additions and 660 deletions.
14 changes: 12 additions & 2 deletions Controller/Adminhtml/Ecommerce/ResetLocalErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
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 ResetLocalErrors extends \Magento\Backend\App\Action
{
Expand All @@ -33,10 +35,18 @@ public function __construct(

public function execute()
{
$valid = 1;
$message = '';
$resultJson = $this->resultJsonFactory->create();
try {
$this->helper->resetErrors();
} catch(ValidatorException $e) {
$valid = 0;
$message = $e->getMessage();
}
return $resultJson->setData([
'valid' => (int)1,
'message' => 'OK',
'valid' => (int)$valid,
'message' => $message,
]);
}
protected function _isAllowed()
Expand Down
16 changes: 14 additions & 2 deletions Cron/Ecommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class Ecommerce
* @var \Ebizmarts\MailChimp\Model\Api\Order
*/
private $_apiOrder;
/**
* @var \Ebizmarts\MailChimp\Model\Api\Cart
*/
private $_apiCart;
/**
* @var \Ebizmarts\MailChimp\Model\MailChimpSyncBatches
*/
Expand All @@ -50,6 +54,8 @@ class Ecommerce
* @param \Ebizmarts\MailChimp\Model\Api\Product $apiProduct
* @param \Ebizmarts\MailChimp\Model\Api\Result $apiResult
* @param \Ebizmarts\MailChimp\Model\Api\Customer $apiCustomer
* @param \Ebizmarts\MailChimp\Model\Api\Order $apiOrder
* @param \Ebizmarts\MailChimp\Model\Api\Cart $apiCart
* @param \Ebizmarts\MailChimp\Model\MailChimpSyncBatches $mailChimpSyncBatches
*/
public function __construct(
Expand All @@ -59,6 +65,7 @@ public function __construct(
\Ebizmarts\MailChimp\Model\Api\Result $apiResult,
\Ebizmarts\MailChimp\Model\Api\Customer $apiCustomer,
\Ebizmarts\MailChimp\Model\Api\Order $apiOrder,
\Ebizmarts\MailChimp\Model\Api\Cart $apiCart,
\Ebizmarts\MailChimp\Model\MailChimpSyncBatches $mailChimpSyncBatches
)
{
Expand All @@ -69,10 +76,12 @@ public function __construct(
$this->_apiResult = $apiResult;
$this->_apiCustomer = $apiCustomer;
$this->_apiOrder = $apiOrder;
$this->_apiCart = $apiCart;
}

public function execute()
{
$this->_helper->log(__METHOD__);
foreach($this->_storeManager->getStores() as $storeId => $val)
{
$this->_storeManager->setCurrentStore($storeId);
Expand All @@ -85,14 +94,17 @@ public function execute()

protected function _processStore($storeId)
{
$this->_helper->log(__METHOD__);
$batchArray = array();
$results = array();
if ($this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_ECOMMERCE_ACTIVE)) {
if ($this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_ECOMMERCE_ACTIVE,$storeId)) {
$results = $this->_apiProduct->_sendProducts($storeId);
$customers = $this->_apiCustomer->sendCustomers($storeId);
$results = array_merge($results,$customers);
$orders = $this->_apiOrder->sendOrders($storeId);
$results = array_merge($results,$orders);
$carts = $this->_apiCart->createBatchJson($storeId);
$results= array_merge($results,$carts);
}
if (!empty($results)) {
try {
Expand All @@ -104,7 +116,7 @@ protected function _processStore($storeId)
} else {
$api = $this->_helper->getApi();
$batchResponse =$api->batchOperation->add($batchArray);
$this->_helper->log(var_export($results,true),null,$batchResponse['id']);
$this->_helper->log($results,null,$batchResponse['id']);
$this->_helper->log(var_export($batchResponse,true));
$this->_mailChimpSyncBatches->setStoreId($storeId);
$this->_mailChimpSyncBatches->setBatchId($batchResponse['id']);
Expand Down
82 changes: 79 additions & 3 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@

namespace Ebizmarts\MailChimp\Helper;

use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Framework\Exception\ValidatorException;
use Magento\Store\Model\Store;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Symfony\Component\Config\Definition\Exception\Exception;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
Expand All @@ -30,6 +29,9 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
const XML_PATH_SYNC_DATE = 'mailchimp/general/mcminsyncdateflag';
const XML_ECOMMERCE_OPTIN = 'mailchimp/ecommerce/customer_optin';
const XML_ECOMMERCE_FIRSTDATE = 'mailchimp/ecommerce/firstdate';
const XML_ABANDONEDCART_ACTIVE = 'mailchimp/abandonedcart/active';
const XML_ABANDONEDCART_FIRSTDATE = 'mailchimp/abandonedcart/firstdate';


const ORDER_STATE_OK = 'complete';

Expand Down Expand Up @@ -78,6 +80,18 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
* @var \Magento\Customer\Model\ResourceModel\Customer\CustomerRepository
*/
private $_customer;
/**
* @var \Ebizmarts\MailChimp\Model\MailChimpErrors
*/
private $_mailChimpErrors;
/**
* @var \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerceFactory
*/
private $_mailChimpSyncEcommerce;
/**
* @var \Magento\Catalog\Model\ResourceModel\Product\Collection
*/
private $_productCollection;

/**
* Data constructor.
Expand All @@ -89,6 +103,9 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
* @param \Magento\Framework\Module\ModuleList\Loader $loader
* @param \Mailchimp $api
* @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
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
Expand All @@ -98,7 +115,10 @@ public function __construct(
\Magento\Framework\App\State $state,
\Magento\Framework\Module\ModuleList\Loader $loader,
\Mailchimp $api,
\Magento\Customer\Model\ResourceModel\CustomerRepository $customer
\Magento\Customer\Model\ResourceModel\CustomerRepository $customer,
\Ebizmarts\MailChimp\Model\MailChimpErrors $mailChimpErrors,
\Ebizmarts\MailChimp\Model\MailChimpSyncEcommerceFactory $mailChimpSyncEcommerce,
\Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection
) {

$this->_storeManager = $storeManager;
Expand All @@ -110,6 +130,9 @@ public function __construct(
$this->_loader = $loader;
$this->_api = $api;
$this->_customer = $customer;
$this->_mailChimpErrors = $mailChimpErrors;
$this->_mailChimpSyncEcommerce = $mailChimpSyncEcommerce;
$this->_productCollection = $productCollection;
parent::__construct($context);
}

Expand Down Expand Up @@ -442,5 +465,58 @@ public function getDateMicrotime()
$date = date('Y-m-d-H-i-s') . '-' . $msecArray[1];
return $date;
}
public function resetErrors()
{
try {
// clean the errors table
$connection = $this->_mailChimpErrors->getResource()->getConnection();
$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->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');
// }
// clean the error in eav for customers
} catch(\Zend_Db_Exception $e) {
throw new ValidatorException(__($e->getMessage()));
}
}
public function resetEcommerce()
{
$this->resetErrors();
}
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);
$chimpSyncEcommerce->setRelatedId($entityId);
$chimpSyncEcommerce->setMailchimpSyncModified($modified);
$chimpSyncEcommerce->setMailchimpSyncDelta($date);
$chimpSyncEcommerce->setMailchimpSyncError($error);
$chimpSyncEcommerce->setMailchimpSyncDeleted($deleted);
$chimpSyncEcommerce->setMailchimpToken($token);
$chimpSyncEcommerce->getResource()->save($chimpSyncEcommerce);
}
public function getChimpSyncEcommerce($storeId,$id,$type)
{
$chimp = $this->_mailChimpSyncEcommerce->create();
return $chimp->getByStoreIdType($storeId,$id,$type);
}


}
Loading

0 comments on commit 9683b0f

Please sign in to comment.