示例#1
0
 public function syncToMailchimp()
 {
     self::$_massSyncFlag = true;
     $syncToParams = Mage::getModel('advancednewsletter/cache')->loadCache('aw_advancednewsletter_mailchimp_to_params');
     if (!$syncToParams) {
         return;
     }
     $syncToParams = unserialize($syncToParams);
     if (!isset($syncToParams['sync_for']) || !isset($syncToParams['include_names']) || !isset($syncToParams['sync_stores'])) {
         return;
     }
     $syncFor = $syncToParams['sync_for'];
     $includeNames = $syncToParams['include_names'];
     $currentPage = Mage::getModel('advancednewsletter/cache')->loadCache('aw_advancednewsletter_mailchimp_to_page');
     if (!$currentPage) {
         $currentPage = 1;
     }
     $subscribersCollection = Mage::getModel('advancednewsletter/subscriber')->getCollection()->addStoreFilter($syncToParams['sync_stores']);
     if ($subscribersCollection->getSize() < ($currentPage - 1) * self::SYNC_TO_MAILCHIMP_PAGE_SIZE) {
         Mage::getModel('advancednewsletter/cache')->removeCache('aw_advancednewsletter_mailchimp_to_params');
         Mage::getModel('advancednewsletter/cache')->removeCache('aw_advancednewsletter_mailchimp_to_page');
         return;
     }
     $subscribers = $subscribersCollection->setPageSize(self::SYNC_TO_MAILCHIMP_PAGE_SIZE)->setCurPage($currentPage);
     switch ($syncFor) {
         case AW_Advancednewsletter_Block_Adminhtml_Synchronization::SYNC_SUBSCRIBED:
             $subscribers->addFilterSubscribed();
             break;
         case AW_Advancednewsletter_Block_Adminhtml_Synchronization::SYNC_UNSUBSCRIBED:
             $subscribers->addFilterUnsubscribed();
             break;
         default:
             $subscribers->addFilterSubscribedUnsubscribed();
             break;
     }
     $syncWithErrorsFlag = false;
     foreach ($subscribers as $subscriber) {
         try {
             if ($subscriber->getStatus() == AW_Advancednewsletter_Model_Subscriber::STATUS_SUBSCRIBED) {
                 AW_Advancednewsletter_Model_Sync_Mailchimpclient::getInstance($subscriber->getStoreId())->setSkipChangesCheck(true)->setIncludeNames($includeNames)->subscribe($subscriber);
             }
             if ($subscriber->getStatus() == AW_Advancednewsletter_Model_Subscriber::STATUS_UNSUBSCRIBED) {
                 AW_Advancednewsletter_Model_Sync_Mailchimpclient::getInstance($subscriber->getStoreId())->setSkipChangesCheck(true)->setIncludeNames($includeNames)->subscribe($subscriber)->unsubscribeFromList($subscriber);
             }
         } catch (Exception $ex) {
             $syncWithErrorsFlag = true;
         }
     }
     Mage::getModel('advancednewsletter/cache')->saveCache($currentPage + 1, 'aw_advancednewsletter_mailchimp_to_page');
     self::$_massSyncFlag = false;
 }
示例#2
0
 public function syncToMailchimp()
 {
     self::$_massSyncFlag = true;
     $cache = Mage::getModel('advancednewsletter/cache');
     $syncToParams = $cache->loadCache(self::SYNC_TO_PARAMS_NAME);
     if (!$syncToParams) {
         return;
     }
     $syncToParams = unserialize($syncToParams);
     foreach ($syncToParams as $storeId => $pagesToSync) {
         if (Mage::getStoreConfig(self::MAILCHIMP_ENABLED, $storeId) && Mage::getStoreConfig(self::MAILCHIMP_APIKEY, $storeId) && Mage::getStoreConfig(self::MAILCHIMP_LISTID, $storeId)) {
             // sync  subspage
             $page = $syncToParams[$storeId]['subscr_page'];
             $syncToParams[$storeId]['subscr_page'] = $this->_exportSubsPage($storeId, 'subscribed', $page, self::SYNC_TO_MAILCHIMP_PAGE_SIZE, $pagesToSync['include_names']);
             // sync  unsubspage
             $page = $syncToParams[$storeId]['unsubscr_page'];
             $syncToParams[$storeId]['unsubscr_page'] = $this->_exportSubsPage($storeId, 'unsubscribed', $page, self::SYNC_TO_MAILCHIMP_PAGE_SIZE, $pagesToSync['include_names']);
             /* remove finished stores */
             if ($pagesToSync['subscr_page'] === 'none' && $pagesToSync['unsubscr_page'] === 'none') {
                 unset($syncToParams[$storeId]);
             }
         } else {
             /* remove disabled store from sync params */
             unset($syncToParams[$storeId]);
         }
     }
     /*  clear & save params  */
     if (count($syncToParams)) {
         $cache->saveCache(serialize($syncToParams), self::SYNC_TO_PARAMS_NAME);
     } else {
         $cache->removeCache(self::SYNC_TO_PARAMS_NAME);
     }
     self::$_massSyncFlag = false;
 }