Skip to content

Commit

Permalink
Fix promo code errors for delete Magento stores.
Browse files Browse the repository at this point in the history
  • Loading branch information
Santiagoebizmarts committed Jul 12, 2019
1 parent 7d3f6fe commit 3fdd46f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 74 deletions.
3 changes: 2 additions & 1 deletion app/code/community/Ebizmarts/MailChimp/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -3606,13 +3606,14 @@ public function wasProductImageCacheFlushed()
*/
public function getApiByMailChimpStoreId($mailchimpStoreId)
{
$api = null;
$scopeArray = $this->getFirstScopeFromConfig(Ebizmarts_MailChimp_Model_Config::GENERAL_MCSTOREID, $mailchimpStoreId);
try {
$api = $this->getApi($scopeArray['scope_id'], $scopeArray['scope']);
return $api;
} catch (Ebizmarts_MailChimp_Helper_Data_ApiKeyException $e) {
$this->logError($e->getMessage());
}
return $api;
}

/**
Expand Down
46 changes: 17 additions & 29 deletions app/code/community/Ebizmarts/MailChimp/Model/Api/PromoCodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,46 +313,34 @@ protected function _setDeleted($codeId, $promoRuleId)

/**
* @param $promoRule
* @throws Exception
*/
public function deletePromoCodesSyncDataByRule($promoRule)
{
$promoCodeIds = $this->getPromoCodesForRule($promoRule->getRelatedId());
foreach ($promoCodeIds as $promoCodeId) {
$promoCodeSyncDataItems = $this->getMailChimpHelper()->getAllEcommerceSyncDataItemsPerId($promoCodeId, Ebizmarts_MailChimp_Model_Config::IS_PROMO_CODE);
foreach ($promoCodeSyncDataItems as $promoCodeSyncDataItem) {
$promoCodeSyncDataItem->delete();
}
}
}

public function deletePromoCodeSyncData($promoCodeId, $mailchimpStoreId)
{
$promoCodeSyncDataItem = $this->getMailChimpHelper()->getEcommerceSyncDataItem($promoCodeId, Ebizmarts_MailChimp_Model_Config::IS_PROMO_CODE, $mailchimpStoreId);
$promoCodeSyncDataItem->delete();
}

/**
* @param $promoRuleId
* @return array
*/
protected function getPromoCodesForRule($promoRuleId)
{
$promoCodes = array();
$promoRuleId = $promoRule->getRelatedId();
$helper = $this->getMailChimpHelper();
$promoRules = $helper->getAllEcommerceSyncDataItemsPerId($promoRuleId, Ebizmarts_MailChimp_Model_Config::IS_PROMO_RULE);
foreach ($promoRules as $promoRule) {
$mailchimpStoreId = $promoRule->getMailchimpStoreId();
$api = $helper->getApiByMailChimpStoreId($mailchimpStoreId);
try {
$mailChimpPromoCodes = $api->ecommerce->promoRules->promoCodes->getAll($mailchimpStoreId, $promoRuleId);
foreach ($mailChimpPromoCodes['promo_codes'] as $promoCode) {
$this->deletePromoCodeSyncData($promoCode['id'], $mailchimpStoreId);
if ($api !== null) {
try {
$mailChimpPromoCodes = $api->ecommerce->promoRules->promoCodes->getAll($mailchimpStoreId, $promoRuleId);
foreach ($mailChimpPromoCodes['promo_codes'] as $promoCode) {
$this->deletePromoCodeSyncData($promoCode['id'], $mailchimpStoreId);
}
} catch (MailChimp_Error $e) {
$helper->logError($e->getFriendlyMessage());
}
} catch (MailChimp_Error $e) {
$helper->logError($e->getFriendlyMessage());
}
}
return $promoCodes;

}

public function deletePromoCodeSyncData($promoCodeId, $mailchimpStoreId)
{
$promoCodeSyncDataItem = $this->getMailChimpHelper()->getEcommerceSyncDataItem($promoCodeId, Ebizmarts_MailChimp_Model_Config::IS_PROMO_CODE, $mailchimpStoreId);
$promoCodeSyncDataItem->delete();
}

protected function getPromoRuleIdByCouponId($promoCodeId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,50 +98,6 @@ public function testMarkAsDeleted()
$promoCodesApiMock->markAsDeleted(self::PROMOCODE_ID, $promoRuleId);
}

public function testDeletePromoCodesSyncDataByRule()
{
$promoRuleId = 1;
$promoCodesIds = array();
$syncDataItems = array();

$promoCodesApiMock = $this->promoCodesApiMock
->setMethods(array('getPromoCodesForRule', 'getMailChimpHelper'))
->getMock();

$promoRuleMock = $this->getMockBuilder(Mage_SalesRule_Model_Rule::class)
->disableOriginalConstructor()
->setMethods(array('getRelatedId'))
->getMock();

$mailChimpHelperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class)
->disableOriginalConstructor()
->setMethods(array('getAllEcommerceSyncDataItemsPerId'))
->getMock();

$syncDataItemCollectionMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Mysql4_Ecommercesyncdata_Collection::class)
->disableOriginalConstructor()
->getMock();

$syncDataItemMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Ecommercesyncdata::class)
->disableOriginalConstructor()
->setMethods(array('delete'))
->getMock();

$promoRuleMock->expects($this->once())->method('getRelatedId')->willReturn($promoRuleId);

$promoCodesIds[] = self::PROMOCODE_ID;
$promoCodesApiMock->expects($this->once())->method('getPromoCodesForRule')->with($promoRuleId)->willReturn($promoCodesIds);
$promoCodesApiMock->expects($this->once())->method('getMailChimpHelper')->willReturn($mailChimpHelperMock);

$mailChimpHelperMock->expects($this->once())->method('getAllEcommerceSyncDataItemsPerId')->with(self::PROMOCODE_ID, Ebizmarts_MailChimp_Model_Config::IS_PROMO_CODE)->willReturn($syncDataItemCollectionMock);
$syncDataItems[] = $syncDataItemMock;
$syncDataItemCollectionMock->expects($this->once())->method("getIterator")->willReturn(new ArrayIterator($syncDataItems));

$syncDataItemMock->expects($this->once())->method('delete');

$promoCodesApiMock->deletePromoCodesSyncDataByRule($promoRuleMock);
}

public function testDeletePromoCodeSyncData()
{
$promoCodesApiMock = $this->promoCodesApiMock
Expand Down

0 comments on commit 3fdd46f

Please sign in to comment.