コード例 #1
0
 /**
  * Proccess abandoned carts.
  *
  * @param string $mode
  */
 public function proccessAbandonedCarts($mode = 'all')
 {
     /*
      * Save lost baskets to be send in Send table.
      */
     $stores = $this->_helper->getStores();
     foreach ($stores as $store) {
         $storeId = $store->getId();
         if ($mode == 'all' || $mode == 'customers') {
             /*
              * Customers campaigns
              */
             foreach ($this->lostBasketCustomers as $num) {
                 //customer enabled
                 if ($this->_isLostBasketCustomerEnabled($num, $storeId)) {
                     //number of the campaign use minutes
                     if ($num == 1) {
                         $minutes = $this->_getLostBasketCustomerInterval($num, $storeId);
                         $interval = new \DateInterval('PT' . $minutes . 'M');
                     } else {
                         $hours = (int) $this->_getLostBasketCustomerInterval($num, $storeId);
                         $interval = new \DateInterval('PT' . $hours . 'H');
                     }
                     $fromTime = new \DateTime('now', new \DateTimeZone('UTC'));
                     $fromTime->sub($interval);
                     $toTime = clone $fromTime;
                     $fromTime->sub(new \DateInterval('PT5M'));
                     //format time
                     $fromDate = $fromTime->format('Y-m-d H:i:s');
                     $toDate = $toTime->format('Y-m-d H:i:s');
                     //active quotes
                     $quoteCollection = $this->_getStoreQuotes($fromDate, $toDate, $guest = false, $storeId);
                     //found abandoned carts
                     if ($quoteCollection->getSize()) {
                         $this->_helper->log('Customer cart : ' . $num . ', from : ' . $fromDate . ' ,to ' . $toDate);
                     }
                     //campaign id for customers
                     $campaignId = $this->_getLostBasketCustomerCampaignId($num, $storeId);
                     foreach ($quoteCollection as $quote) {
                         $email = $quote->getCustomerEmail();
                         $websiteId = $store->getWebsiteId();
                         $quoteId = $quote->getId();
                         //api - set the last quote id for customer
                         $this->_helper->updateLastQuoteId($quoteId, $email, $websiteId);
                         $items = $quote->getAllItems();
                         $mostExpensiveItem = false;
                         foreach ($items as $item) {
                             if ($mostExpensiveItem == false) {
                                 $mostExpensiveItem = $item;
                             } elseif ($item->getPrice() > $mostExpensiveItem->getPrice()) {
                                 $mostExpensiveItem = $item;
                             }
                         }
                         //api-send the most expensive product for abandoned cart
                         if ($mostExpensiveItem) {
                             $this->_helper->updateAbandonedProductName($mostExpensiveItem->getName(), $email, $websiteId);
                         }
                         //send email only if the interval limit passed, no emails during this interval
                         $intervalLimit = $this->_checkCustomerCartLimit($email, $storeId);
                         //no campign found for interval pass
                         if (!$intervalLimit) {
                             //save lost basket for sending
                             //@codingStandardsIgnoreStart
                             $this->_campaignFactory->create()->setEmail($email)->setCustomerId($quote->getCustomerId())->setEventName('Lost Basket')->setQuoteId($quoteId)->setMessage('Abandoned Cart ' . $num)->setCampaignId($campaignId)->setStoreId($storeId)->setWebsiteId($websiteId)->setIsSent(null)->save();
                             //@codingStandardsIgnoreEnd
                         }
                     }
                 }
             }
         }
         if ($mode == 'all' || $mode == 'guests') {
             /*
              * Guests campaigns
              */
             foreach ($this->lostBasketGuests as $num) {
                 if ($this->_isLostBasketGuestEnabled($num, $storeId)) {
                     //for the  first cart which use the minutes
                     if ($num == 1) {
                         $minutes = $this->_getLostBasketGuestIterval($num, $storeId);
                         $interval = new \DateInterval('PT' . $minutes . 'M');
                     } else {
                         $hours = $this->_getLostBasketGuestIterval($num, $storeId);
                         $interval = new \DateInterval('PT' . $hours . 'H');
                     }
                     $fromTime = new \DateTime('now', new \DateTimeZone('UTC'));
                     $fromTime->sub($interval);
                     $toTime = clone $fromTime;
                     $fromTime->sub(new \DateInterval('PT5M'));
                     //format time
                     $fromDate = $fromTime->format('Y-m-d H:i:s');
                     $toDate = $toTime->format('Y-m-d H:i:s');
                     //active guest quotes
                     $quoteCollection = $this->_getStoreQuotes($fromDate, $toDate, $guest = true, $storeId);
                     //log the time for carts found
                     if ($quoteCollection->getSize()) {
                         $this->_helper->log('Guest cart : ' . $num . ', from : ' . $fromDate . ' ,to : ' . $toDate);
                     }
                     $guestCampaignId = $this->_getLostBasketGuestCampaignId($num, $storeId);
                     foreach ($quoteCollection as $quote) {
                         $email = $quote->getCustomerEmail();
                         $websiteId = $store->getWebsiteId();
                         $quoteId = $quote->getId();
                         // upate last quote id for the contact
                         $this->_helper->updateLastQuoteId($quoteId, $email, $websiteId);
                         // update abandoned product name for contact
                         $items = $quote->getAllItems();
                         $mostExpensiveItem = false;
                         foreach ($items as $item) {
                             if ($mostExpensiveItem == false) {
                                 $mostExpensiveItem = $item;
                             } elseif ($item->getPrice() > $mostExpensiveItem->getPrice()) {
                                 $mostExpensiveItem = $item;
                             }
                         }
                         //api- set the most expensive product to datafield
                         if ($mostExpensiveItem) {
                             $this->_helper->updateAbandonedProductName($mostExpensiveItem->getName(), $email, $websiteId);
                         }
                         //send email only if the interval limit passed, no emails during this interval
                         $campignFound = $this->_checkCustomerCartLimit($email, $storeId);
                         //no campign found for interval pass
                         if (!$campignFound) {
                             //save lost basket for sending
                             //@codingStandardsIgnoreStart
                             $this->_campaignFactory->create()->setEmail($email)->setEventName('Lost Basket')->setQuoteId($quoteId)->setCheckoutMethod('Guest')->setMessage('Guest Abandoned Cart ' . $num)->setCampaignId($guestCampaignId)->setStoreId($storeId)->setWebsiteId($websiteId)->setIsSent(null)->save();
                             //@codingStandardsIgnoreEnd
                         }
                     }
                 }
             }
         }
     }
 }