Skip to content

Commit

Permalink
Remove debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzaloebiz committed Jun 5, 2017
1 parent 54b4341 commit 9f6a8d0
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 45 deletions.
72 changes: 45 additions & 27 deletions Cron/Ecommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ public function execute()
{
$this->_storeManager->setCurrentStore($storeId);
$listId = $this->_helper->getGeneralList($storeId);
if($this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_ACTIVE)) {
if($this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_ACTIVE,$storeId)) {
$mailchimpStoreId = $this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE,$storeId);
if ($mailchimpStoreId != -1)
{
$this->_apiResult->processResponses($storeId,true, $mailchimpStoreId);
$batchId =$this->_processStore($storeId, $mailchimpStoreId);
$batchId =$this->_processStore($storeId, $mailchimpStoreId, $listId);
if ($batchId) {
$connection->update($tableName, ['batch_id' => $batchId], "batch_id is null and mailchimp_store_id = '$mailchimpStoreId'");
$connection->update($tableName, ['batch_id' => $batchId], "batch_id is null and mailchimp_store_id = '$listId'");
Expand All @@ -118,46 +118,64 @@ public function execute()
}
}

protected function _processStore($storeId, $mailchimpStoreId)
protected function _processStore($storeId, $mailchimpStoreId, $listId)
{
$batchId = null;
$batchArray = array();
$results = array();
$batchArray = [];
$results = [];
if ($this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_ECOMMERCE_ACTIVE,$storeId)) {
$results = $this->_apiSubscribers->sendSubscribers($storeId);
$results = $this->_apiSubscribers->sendSubscribers($storeId,$listId);
$countSubscribers = count($results);
$products = $this->_apiProduct->_sendProducts($storeId);
$countProducts = count($products);
$results = array_merge($results, $products);
$customers = $this->_apiCustomer->sendCustomers($storeId);
$countCustomers = count($customers);
$results = array_merge($results,$customers);
$orders = $this->_apiOrder->sendOrders($storeId);
$countOrders = count($orders);
$results = array_merge($results,$orders);
$carts = $this->_apiCart->createBatchJson($storeId);
$results= array_merge($results,$carts);
}
if (!empty($results)) {
try {
$batchArray['operations'] = $results;
$batchJson = json_encode($batchArray);

if (!$batchJson || $batchJson == '') {
$this->_helper->log('An empty operation was detected');
} else {
$api = $this->_helper->getApi();
$batchResponse =$api->batchOperation->add($batchArray);
$this->_helper->log($results,null,$batchResponse['id']);
$this->_helper->log(var_export($batchResponse,true));
$this->_mailChimpSyncBatches->setStoreId($storeId);
$this->_mailChimpSyncBatches->setBatchId($batchResponse['id']);
$this->_mailChimpSyncBatches->setStatus($batchResponse['status']);
$this->_mailChimpSyncBatches->setMailchimpStoreId($mailchimpStoreId);
$this->_mailChimpSyncBatches->getResource()->save($this->_mailChimpSyncBatches);
$batchId = $batchResponse['id'];
if (!empty($results)) {
try {
$batchArray['operations'] = $results;
$batchJson = json_encode($batchArray);

if (!$batchJson || $batchJson == '') {
$this->_helper->log('An empty operation was detected');
} else {
$api = $this->_helper->getApi($storeId);
$batchResponse =$api->batchOperation->add($batchArray);
if (!isset($batchResponse['id'])) {
$this->_helper->log('error in the call to batch');
} else {
$this->_helper->log(var_export($batchResponse, true));
$this->_mailChimpSyncBatches->setStoreId($storeId);
$this->_mailChimpSyncBatches->setBatchId($batchResponse['id']);
$this->_mailChimpSyncBatches->setStatus($batchResponse['status']);
$this->_mailChimpSyncBatches->setMailchimpStoreId($mailchimpStoreId);
$this->_mailChimpSyncBatches->getResource()->save($this->_mailChimpSyncBatches);
$batchId = $batchResponse['id'];
}
}
} catch(\Mailchimp_Error $e) {
$this->_helper->log('error de mailchimp '.$e->getMessage());
} catch(\Exception $e) {
$this->_helper->log("Json encode fails");
$this->_helper->log(var_export($batchArray,true));
}
} catch(Exception $e) {
$this->_helper->log("Json encode fails");
$this->_helper->log(var_export($batchArray,true));
}
$countTotal = $countCustomers + $countProducts + $countOrders + $countSubscribers;
if($countTotal == 0 && $this->_helper->getMCMinSyncing($storeId))
{
$api = $this->_helper->getApi($storeId);
$api->ecommerce->stores->edit($mailchimpStoreId,null, null, null, null, null, null, null, null, null, null, false);
$this->_helper->saveConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_IS_SYNC,true,$storeId);
}
}

return $batchId;
}

Expand Down
1 change: 0 additions & 1 deletion Cron/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public function processWebhooks()
{
$collection = $this->_webhookCollection->create();
$collection->addFieldToFilter('processed',['eq'=>0]);
$this->_helper->log((string)$collection->getSelect());
/**
* @var $item \Ebizmarts\MailChimp\Model\MailChimpWebhookRequest
*/
Expand Down
38 changes: 30 additions & 8 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
const XML_ABANDONEDCART_ACTIVE = 'mailchimp/abandonedcart/active';
const XML_ABANDONEDCART_FIRSTDATE = 'mailchimp/abandonedcart/firstdate';
const XML_ABANDONEDCART_PAGE = 'mailchimp/abandonedcart/page';
const XML_PATH_IS_SYNC = 'mailchimp/general/issync';


const ORDER_STATE_OK = 'complete';
Expand Down Expand Up @@ -227,7 +228,8 @@ public function getApiKey($store = null)
*/
public function getApi($store = null)
{
$this->_api->setApiKey($this->getApiKey($store));
$apiKey = $this->getApiKey($store);
$this->_api->setApiKey($apiKey);
$this->_api->setUserAgent('Mailchimp4Magento' . (string)$this->getModuleVersion());
return $this->_api;
}
Expand Down Expand Up @@ -262,6 +264,24 @@ public function getConfigValue($path, $storeId = null, $scope = null)
return $value;
}

public function saveConfigValue($path, $value, $storeId = null, $scope = null)
{
switch ($scope) {
case 'website':
$this->_config->saveConfig($path,$value,\Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE,$storeId);
break;
default:
$this->_config->saveConfig($path,$value,\Magento\Store\Model\ScopeInterface::SCOPE_STORE,$storeId);
break;
}

}
public function getMCMinSyncing($storeId)
{
$ret = $this->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_IS_SYNC,$storeId);
return !$ret;

}
/**
* @param null $store
* @return mixed
Expand Down Expand Up @@ -460,13 +480,15 @@ protected function _getCustomerMergeVarsValues($map, $customer, $merge_vars)
}
public function getGeneralList($storeId)
{
$mailchimpStoreId = $this->getConfigValue(self::XML_MAILCHIMP_STORE,$storeId);
$api = $this->getApi($storeId);
$store =$api->ecommerce->stores->get($mailchimpStoreId);
if (isset($store['list_id'])) {
return $store['list_id'];
}
return null;
return $this->getConfigValue(self::XML_PATH_LIST,$storeId);
// $this->log($mailchimpStoreId);
// $api = $this->getApi($storeId);
// $store =$api->ecommerce->stores->get($mailchimpStoreId);
// $this->log($store);
// if (isset($store['list_id'])) {
// return $store['list_id'];
// }
// return null;
}

public function getListForMailChimpStore($mailchimpStoreId,$apiKey)
Expand Down
2 changes: 1 addition & 1 deletion Model/Api/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ public function sendOrders($magentoStoreId)
$batchArray = array_merge($batchArray, $this->_getModifiedOrders($magentoStoreId));
// get new orders
$batchArray = array_merge($batchArray, $this->_getNewOrders($magentoStoreId));

return $batchArray;
}
protected function _getCollection()
Expand Down Expand Up @@ -220,6 +219,7 @@ protected function _getNewOrders($magentoStoreId)
$newOrders->getSelect()->where("m4m.mailchimp_sync_delta IS NULL");
// limit the collection
$newOrders->getSelect()->limit(self::BATCH_LIMIT);

/**
* @var $order \Magento\Sales\Model\Order
*/
Expand Down
11 changes: 9 additions & 2 deletions Model/Api/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Product
const DOWNLOADABLE = 'downloadable';
const PRODUCTIMAGE = 'product_small_image';
const MAX = 100;

protected $_parentImage = null;
/**
* @var \Ebizmarts\MailChimp\Helper\Data
*/
Expand Down Expand Up @@ -197,7 +199,7 @@ protected function _buildOldProductRequest(
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,$magentoStoreId);
$data = $this-> _buildProductData($product,$magentoStoreId);

$parentIds = $product->getTypeInstance()->getParentIdsByChild($product->getId());
//$parentIds = Mage::getResourceSingleton('catalog/product_type_configurable')->getParentIdsByChild($product->getId());
Expand Down Expand Up @@ -251,6 +253,8 @@ protected function _buildProductData(
$data["url"] = $product->getProductUrl();
if ($product->getImage()) {
$data["image_url"] = $this->_imageHelper->init($product,self::PRODUCTIMAGE)->setImageFile($product->getImage())->getUrl();
} elseif ($this->_parentImage) {
$data['image_url'] = $this->_parentImage;
}
$data["published_at_foreign"] = "";
if ($isVarient) {
Expand Down Expand Up @@ -285,12 +289,15 @@ protected function _buildProductData(
//missing data
$data["vendor"] = "";
$data["handle"] = "";

if(isset($data['image_url'])) {
$this->_parentImage = $data['image_url'];
}
//variants
$data["variants"] = array();
foreach ($variants as $variant) {
$data["variants"][] = $this->_buildProductData($variant,$magentoStoreId);
}
$this->_parentImage = null;
}

return $data;
Expand Down
8 changes: 4 additions & 4 deletions Model/Api/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Result
{
const MAILCHIMP_TEMP_DIR = 'Mailchimp';
/**
* @var \Ebizmarts\MailChimp\Model\ResourceModel\MailChimpSyncBatches\Collection
* @var \Ebizmarts\MailChimp\Model\ResourceModel\MailChimpSyncBatches\CollectionFactory
*/
private $_batchCollection;
/**
Expand All @@ -42,7 +42,7 @@ class Result
*/
public function __construct(
\Ebizmarts\MailChimp\Helper\Data $helper,
\Ebizmarts\MailChimp\Model\ResourceModel\MailChimpSyncBatches\Collection $batchCollection,
\Ebizmarts\MailChimp\Model\ResourceModel\MailChimpSyncBatches\CollectionFactory $batchCollection,
\Ebizmarts\MailChimp\Model\MailChimpErrorsFactory $chimpErrors,
\Magento\Framework\Archive $archive
)
Expand All @@ -54,7 +54,7 @@ public function __construct(
}
public function processResponses($storeId, $isMailChimpStoreId = false, $mailchimpStoreId)
{
$collection = $this->_batchCollection;
$collection = $this->_batchCollection->create();
$collection
->addFieldToFilter('store_id', array('eq' => $storeId))
->addFieldToFilter('status', array('eq' => 'pending'));
Expand All @@ -64,7 +64,6 @@ public function processResponses($storeId, $isMailChimpStoreId = false, $mailchi
$item = null;
foreach ($collection as $item) {
try {
// $storeId = ($isMailChimpStoreId) ? 0 : $storeId;
$files = $this->getBatchResponse($item->getBatchId(), $storeId);
if (count($files)) {
$this->processEachResponseFile($files, $item->getBatchId(), $mailchimpStoreId, $storeId);
Expand All @@ -89,6 +88,7 @@ public function getBatchResponse($batchId, $storeId = null)
$api = $this->_helper->getApi($storeId);
// check the status of the job
$response = $api->batchOperation->status($batchId);

if (isset($response['status']) && $response['status'] == 'finished') {
// get the tar.gz file with the results
$fileUrl = urldecode($response['response_body_url']);
Expand Down
5 changes: 3 additions & 2 deletions Model/Api/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ public function __construct(
$this->_subscriberFactory = $subscriberFactory;
}

public function sendSubscribers($storeId)
public function sendSubscribers($storeId, $listId)
{
//get subscribers
$listId = $this->_helper->getGeneralList($storeId);
// $listId = $this->_helper->getGeneralList($storeId);
$collection = $this->_subscriberCollection->create();
$collection->addFieldToFilter('subscriber_status', array('eq' => 1))
->addFieldToFilter('store_id', array('eq' => $storeId));
Expand Down Expand Up @@ -110,6 +110,7 @@ public function sendSubscribers($storeId)

protected function _buildSubscriberData(\Magento\Newsletter\Model\Subscriber $subscriber)
{
$this->_helper->log(__METHOD__);
$storeId = $subscriber->getStoreId();
$data = array();
$data["email_address"] = $subscriber->getSubscriberEmail();
Expand Down

0 comments on commit 9f6a8d0

Please sign in to comment.