Skip to content

Commit

Permalink
Merge pull request #390 from mailchimp/Issue309
Browse files Browse the repository at this point in the history
closes #309
  • Loading branch information
gonzaloebiz authored Sep 12, 2018
2 parents 9bd68d5 + f65d7b2 commit 6aabf03
Showing 1 changed file with 26 additions and 34 deletions.
60 changes: 26 additions & 34 deletions Model/Api/Product.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
<?php
/**
* mc-magento2 Magento Component
*
* @category Ebizmarts
* @package mc-magento2
* @author Ebizmarts Team <info@ebizmarts.com>
* @copyright Ebizmarts (http://ebizmarts.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @date: 10/10/16 5:22 PM
* @file: Product.php
*/
mc-magento2 Magento Component
@category Ebizmarts
@Package mc-magento2
@author Ebizmarts Team info@ebizmarts.com
@copyright Ebizmarts (http://ebizmarts.com)
@license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
@Date: 10/10/16 5:22 PM
@file: Product.php
*/
namespace Ebizmarts\MailChimp\Model\Api;



class Product
{
const DOWNLOADABLE = 'downloadable';
const PRODUCTIMAGE = 'product_small_image';
const MAX = 100;
const DOWNLOADABLE = 'downloadable';
const PRODUCTIMAGE = 'product_small_image';
const MAX = 100;

protected $_parentImage = null;
protected $_childtUrl = null;
Expand Down Expand Up @@ -93,7 +90,7 @@ public function __construct(
\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollection,
\Magento\Catalog\Model\Product\Option $option
) {

$this->_productRepository = $productRepository;
$this->_helper = $helper;
$this->_productCollection = $productCollection;
Expand All @@ -117,7 +114,7 @@ public function _sendProducts($magentoStoreId)
$collection->getSelect()->joinLeft(
['m4m' => $this->_helper->getTableName('mailchimp_sync_ecommerce')],
"m4m.related_id = e.entity_id and m4m.type = '".\Ebizmarts\MailChimp\Helper\Data::IS_PRODUCT.
"' and m4m.mailchimp_store_id = '".$mailchimpStoreId."'",
"' and m4m.mailchimp_store_id = '".$mailchimpStoreId."'",
['m4m.*']
);
$collection->getSelect()->where("m4m.mailchimp_sync_delta IS null OR (m4m.mailchimp_sync_delta > '".$this->_helper->getMCMinSyncDateFlag().
Expand All @@ -128,8 +125,6 @@ public function _sendProducts($magentoStoreId)
* @var $product \Magento\Catalog\Model\Product
*/
$product = $this->_productRepository->get($item->getSku());
// $productSyncData = $this->_chimpSyncEcommerce->getByStoreIdType($mailchimpStoreId,$product->getId(),
// \Ebizmarts\MailChimp\Helper\Data::IS_PRODUCT);
if ($item->getMailchimpSyncModified() && $item->getMailchimpSyncDelta() &&
$item->getMailchimpSyncDelta() > $this->_helper->getMCMinSyncDateFlag()) {
$batchArray = array_merge($this->_buildOldProductRequest($product,$this->_batchId,$mailchimpStoreId, $magentoStoreId),$batchArray);
Expand Down Expand Up @@ -180,7 +175,7 @@ protected function _markSpecialPrices($magentoStoreId, $mailchimpStoreId)
)],]], 'left'
);
$collection->getSelect()->joinLeft(['mc' => $collection->getTable('mailchimp_sync_ecommerce')],
"mc.type = 'PRO' AND mc.related_id = e.entity_id AND mc.mailchimp_sync_modified = 0 ".$collection->getConnection()->quoteInto(" AND mc.mailchimp_store_id = ?",$mailchimpStoreId) ." and mc.mailchimp_sync_delta < (IF(at_special_from_date.value_id > 0, at_special_from_date.value, at_special_from_date_default.value))");
"mc.type = 'PRO' AND mc.related_id = e.entity_id AND mc.mailchimp_sync_modified = 0 ".$collection->getConnection()->quoteInto(" AND mc.mailchimp_store_id = ?",$mailchimpStoreId) ." and mc.mailchimp_sync_delta < at_special_from_date.value");
$collection->getSelect()->where('mc.mailchimp_sync_delta is not null');
foreach ($collection as $item) {
$this->_updateProduct($mailchimpStoreId, $item->getEntityId(),null,null, 1);
Expand All @@ -196,10 +191,10 @@ protected function _markSpecialPrices($magentoStoreId, $mailchimpStoreId)
)->addAttributeToFilter(
'special_to_date', ['or' => [ 0 => ['date' => true,
'to' => date('Y-m-d',time()).' 00:00:00'],
]], 'left'
]], 'left'
);
$collection2->getSelect()->joinLeft(['mc' => $collection2->getTable('mailchimp_sync_ecommerce')],
"mc.type = 'PRO' and mc.related_id = e.entity_id and mc.mailchimp_sync_modified = 0 ".$collection->getConnection()->quoteInto(" AND mc.mailchimp_store_id = ?",$mailchimpStoreId) ." and mc.mailchimp_sync_delta < (IF(at_special_to_date.value_id > 0, at_special_to_date.value, at_special_to_date_default.value))",[]);
"mc.type = 'PRO' and mc.related_id = e.entity_id and mc.mailchimp_sync_modified = 0 ".$collection->getConnection()->quoteInto(" AND mc.mailchimp_store_id = ?",$mailchimpStoreId) ." and mc.mailchimp_sync_delta < at_special_to_date.value",[]);
$collection2->getSelect()->where('mc.mailchimp_sync_delta is not null');
foreach ($collection2 as $item) {
$this->_updateProduct($mailchimpStoreId, $item->getEntityId(),null,null, 1);
Expand All @@ -218,7 +213,7 @@ protected function _buildNewProductRequest(
$mailchimpStoreId,
$magentoStoreId
) {

$variantProducts = [];
switch ($product->getTypeId()) {
case \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE:
Expand Down Expand Up @@ -251,7 +246,6 @@ protected function _buildNewProductRequest(
$this->_helper->log("Product " . $product->getId() . " json encode failed");
return [];
}
$this->_helper->modifyCounter(\Ebizmarts\MailChimp\Helper\Data::PRO_NEW);
$data = [];
$data['method'] = "POST";
$data['path'] = "/ecommerce/stores/" . $mailchimpStoreId . "/products";
Expand All @@ -272,15 +266,14 @@ protected function _buildOldProductRequest(
$product->getTypeId() == "downloadable") {
$data = $this-> _buildProductData($product, $magentoStoreId);
$variantProducts [] = $product;

// $parentIds = $product->getTypeInstance()->getParentIdsByChild($product->getId());
$parentIds = $this->_configurable->getParentIdsByChild($product->getId());
// $parentIds = $product->getTypeInstance()->getParentIdsByChild($product->getId());
$parentIds = $this->_configurable->getParentIdsByChild($product->getId());

//add or update variant
foreach ($parentIds as $parentId) {
$productSync = $this->_chimpSyncEcommerce->create()->getByStoreIdType($mailchimpStoreId,
$parentId,
\Ebizmarts\MailChimp\Helper\Data::IS_PRODUCT);
$parentId,
\Ebizmarts\MailChimp\Helper\Data::IS_PRODUCT);
if($productSync->getMailchimpSyncDelta()) {
$variendata = [];
$variendata["id"] = $data["id"];
Expand Down Expand Up @@ -329,7 +322,6 @@ protected function _buildOldProductRequest(
$this->_helper->log("Product " . $product->getId() . " json encode failed");
return [];
}
$this->_helper->modifyCounter(\Ebizmarts\MailChimp\Helper\Data::PRO_MOD);
$data = [];
$data['method'] = "PATCH";
$data['path'] = "/ecommerce/stores/" . $mailchimpStoreId . "/products/".$product->getId();
Expand All @@ -344,7 +336,7 @@ protected function _buildProductData(
$isVarient = true,
$variants = null
) {

$data = [];

//data applied for both root and varient products
Expand Down Expand Up @@ -498,7 +490,7 @@ public function sendModifiedProduct(\Magento\Sales\Model\Order $order, $mailchim
}
if ($productSyncData->getMailchimpSyncModified() &&
$productSyncData->getMailchimpSyncDelta() > $this->_helper->getMCMinSyncDateFlag()) {
$data = array_merge($this->_buildOldProductRequest($product, $batchId, $mailchimpStoreId, $magentoStoreId),$data);
$data[] = $this->_buildOldProductRequest($product, $batchId, $mailchimpStoreId, $magentoStoreId);
$this->_updateProduct($mailchimpStoreId, $product->getId());
} elseif (!$productSyncData->getMailchimpSyncDelta() ||
$productSyncData->getMailchimpSyncDelta() < $this->_helper->getMCMinSyncDateFlag()) {
Expand Down Expand Up @@ -532,7 +524,7 @@ public function sendQuoteModifiedProduct(\Magento\Quote\Model\Quote $quote, $mai

if ($productSyncData->getMailchimpSyncModified() &&
$productSyncData->getMailchimpSyncDelta() > $this->_helper->getMCMinSyncDateFlag()) {
$data = array_merge($this->_buildOldProductRequest($product, $batchId, $mailchimpStoreId, $magentoStoreId),$data);
$data[] = $this->_buildOldProductRequest($product, $batchId, $mailchimpStoreId, $magentoStoreId);
$this->_updateProduct($mailchimpStoreId, $product->getId());
} elseif (!$productSyncData->getMailchimpSyncDelta() ||
$productSyncData->getMailchimpSyncDelta() < $this->_helper->getMCMinSyncDateFlag()) {
Expand Down Expand Up @@ -560,4 +552,4 @@ protected function _updateProduct($storeId, $entityId, $sync_delta=null, $sync_e
$sync_modified
);
}
}
}

0 comments on commit 6aabf03

Please sign in to comment.