public function Oldbackdatacart($rule) { //grab orders in the past $day_old_back days. //old back data $day_old_back = 0; $emailChain = unserialize($rule['email_chain']); foreach ($emailChain as $emailChainItem) { if ($day_old_back < $emailChainItem['DAYS']) { $day_old_back = $emailChainItem['DAYS']; } } //order in emailqueue //$emailExistEmailQueue = Mage::getModel('followupemail/emailqueue')->getCollection()->getColumnValues("recipient_email"); //->addAttributeToFilter('is_abandoncart', array('eq' => 1)) $emailExistEmailQueue = Mage::getModel('followupemail/emailqueue')->getCollection()->addFieldToFilter('is_abandoncart', array('eq' => 1))->getColumnValues("recipient_email"); $product = Mage::getModel('catalog/product'); $resource = Mage::getSingleton('core/resource'); $read = $resource->getConnection('core_read'); $select = $read->select()->from(array('q' => $resource->getTableName('sales/quote')), array('store_id' => 'q.store_id', 'quote_id' => 'q.entity_id', 'customer_id' => 'q.customer_id', 'subtotal' => 'q.subtotal', 'subtotal_with_discount' => 'q.subtotal_with_discount', 'grand_total' => 'q.grand_total', 'items_qty' => 'q.items_qty', 'updated_at' => 'q.updated_at'))->joinLeft(array('a' => $resource->getTableName('sales/quote_address')), 'q.entity_id=a.quote_id AND a.address_type="billing"', array('customer_email' => new Zend_Db_Expr('IFNULL(q.customer_email, a.email)'), 'customer_firstname' => new Zend_Db_Expr('IFNULL(q.customer_firstname, a.firstname)'), 'customer_middlename' => new Zend_Db_Expr('IFNULL(q.customer_middlename, a.middlename)'), 'customer_lastname' => new Zend_Db_Expr('IFNULL(q.customer_lastname, a.lastname)'), 'city' => 'a.city', 'state' => 'a.region', 'zipcode' => 'a.postcode', 'country_id' => 'a.country_id'))->joinInner(array('i' => $resource->getTableName('sales/quote_item')), 'q.entity_id=i.quote_id', array('product_ids' => new Zend_Db_Expr('GROUP_CONCAT(i.product_id)'), 'item_ids' => new Zend_Db_Expr('GROUP_CONCAT(i.item_id)'), 'sku' => new Zend_Db_Expr('GROUP_CONCAT(i.sku)'), 'product_type' => new Zend_Db_Expr('GROUP_CONCAT(i.product_type)')))->where('q.is_active=1')->where('q.updated_at > ?', date(MW_FollowUpEmail_Model_Mysql4_Emailqueue::MYSQL_DATETIME_FORMAT, time() - $day_old_back * 86400))->where('q.items_count>0')->where('q.customer_email IS NOT NULL OR a.email IS NOT NULL')->where('i.parent_item_id IS NULL')->where('q.customer_email not in (?)', $emailExistEmailQueue)->group('q.entity_id')->order('updated_at'); //->addAttributeToFilter('q.customer_email', array('nin' => $emailExistEmailQueue)); //mage::log(date(MW_FollowUpEmail_Model_Mysql4_Emailqueue::MYSQL_DATETIME_FORMAT,$now - ($intFromTimeHour+$intTimeLastHour))); $carts = $read->fetchAll($select); $storeId = Mage::app()->getStore()->getStoreId(); $groupId = Mage::getSingleton('customer/session')->getCustomerGroupId(); $rulecollection = Mage::getModel('followupemail/rules')->getCollection(); $rules = $rulecollection->loadRulesByEvent(MW_FollowUpEmail_Model_System_Config_Eventfollowupemail::EVENT_TYPE_ABANDON_CART, $storeId, $groupId)->getData(); foreach ($carts as $cart) { /*$timeAbandonCart = strtotime($cart['updated_at'])+$this->_intTimeLast; if($timeAbandonCart > time()) continue; */ $store = Mage::getModel('core/store')->load($storeId); $productIds = explode(',', $cart['product_ids']); $customerInfo = Mage::getModel('followupemail/observer')->_getCustomer($cart['customer_id'], null); foreach ($rules as $rule) { if (!Mage::getModel('followupemail/validate')->validate(unserialize($rule["conditions_serialized"]), null, $cart, $cart['customer_id'])) { continue; } $senderInfo = array(); $senderInfo['sender_email'] = $rule['sender_email']; $senderInfo['sender_name'] = $rule['sender_name']; $emailChain = unserialize($rule['email_chain']); $queue = Mage::getModel('followupemail/emailqueue'); foreach ($emailChain as $emailChainItem) { //get content of current email template $emailTemplate = Mage::getModel('followupemail/rules')->getTemplate($emailChainItem['TEMPLATE_ID'], $rule); //$emailTemplateContent = $emailTemplate['content']; $timeSent = $emailChainItem['DAYS'] * 1440 + $emailChainItem['HOURS'] * 60 + $emailChainItem['MINUTES']; $code = MW_FollowUpEmail_Helper_Data::encryptCode($cart['customer_email'], 'cart', 0); $linkDirect = $store->getUrl('followupemail/index/direct', array('code' => $code)); $params = array(); $params['templateEmailId'] = $emailChainItem['TEMPLATE_ID']; $params['senderInfo'] = $senderInfo; $params['productIds'] = $productIds; $params['orderId'] = ""; $params['cart'] = $cart; $params['data'] = ""; $params['customer'] = ""; $params['customerId'] = $cart['customer_id']; $params['storeId'] = $storeId; $params['code'] = ""; $params['codeCart'] = $code; //$content = $this->_prepareContentEmailAbandonCart($emailTemplate,$cart,$customerInfo,$linkDirect,$productIds); $content = ""; // $scheduledAt, $sentAt, $ruleId, $orderId, $senderName, $recipientName, $recipientEmail, $subject, $content, $params if ($cart['customer_email'] == "") { continue; } //if(Mage::getModel('followupemail/observer')->_checkExistQueueEmail($rule['rule_id'],0,$cart['customer_email'],$emailTemplate['code'],strtotime($cart['updated_at']) + $timeSent * 60,1)){ $coupon = Mage::helper('followupemail/coupon')->generateCode($rule, $cart['customer_email']); $params['coupon'] = $coupon; $queue->add(strtotime($cart['updated_at']) + $timeSent * 60, $rule['rule_id'], 0, $emailTemplate['sender_name'], $emailTemplate['sender_email'], $cart['customer_firstname'] . ' ' . $cart['customer_lastname'], $cart['customer_email'], $emailTemplate['subject'], $content, serialize($params), $emailTemplate['code'], 1, $code, "", $coupon); //} //} } } } }
protected function _checkBirthdays() { $config = Mage::getStoreConfig('followupemail/config/enabled'); if (!$config) { return false; } $rulecollection = Mage::getModel('followupemail/rules')->getCollection(); $eventStatus = MW_FollowUpEmail_Model_System_Config_Eventfollowupemail::CUSTOMER_BIRTHDAY; $storeId = Mage::app()->getStore()->getStoreId(); $rules = $rulecollection->loadRulesByEvent($eventStatus, $storeId, "")->getData(); $customerEntityTypeID = Mage::getModel('eav/entity_type')->loadByCode('customer')->getId(); $customerDateOfBirthAttributeId = Mage::getModel('eav/entity_attribute')->loadByCode($customerEntityTypeID, 'dob')->getId(); $resource = Mage::getSingleton('core/resource'); $read = $resource->getConnection('core_read'); $customer_entity = $resource->getTableName('customer/entity'); $dobTableName = $customer_entity . '_datetime'; $time = time(); foreach ($rules as $rule) { $senderInfo = array(); $senderInfo['sender_email'] = $rule['sender_email']; $senderInfo['sender_name'] = $rule['sender_name']; foreach (unserialize($rule['email_chain']) as $emailChainItem) { $select = $read->select()->from(array('dob' => $dobTableName), array('entity_id', 'value'))->join(array('customer' => $customer_entity), 'customer.entity_id=dob.entity_id AND customer.entity_type_id=dob.entity_type_id', array('store_id' => 'customer.store_id', 'email' => 'customer.email', 'group_id' => 'customer.group_id'))->where('dob.entity_type_id=?', $customerEntityTypeID)->where('dob.attribute_id=?', $customerDateOfBirthAttributeId)->where('DATE_FORMAT(dob.value, "%m-%d")=?', date('m-d', $time - (int) ($emailChainItem['BEFORE'] * $emailChainItem['DAYS'] * 86400))); $birthDays = $read->fetchAll($select); $queue = Mage::getModel('followupemail/emailqueue'); if (count($birthDays)) { $params = array(); foreach ($birthDays as $birthDay) { if (!Mage::getModel('followupemail/validate')->validate(unserialize($rule["conditions_serialized"]), null, null, $birthDay['entity_id'])) { continue; } //get content of current email template $emailTemplate = Mage::getModel('followupemail/rules')->getTemplate($emailChainItem['TEMPLATE_ID'], $rule); $customerInfo = $this->_getCustomer($birthDay['entity_id'], null); //$timeSent = (int)($emailChainItem['BEFORE']*$emailChainItem['DAYS']*1400); $code = MW_FollowUpEmail_Helper_Data::encryptCode($birthDay['email'], '', 0); $params = array(); $params['templateEmailId'] = $emailChainItem['TEMPLATE_ID']; $params['senderInfo'] = $senderInfo; $params['productIds'] = ""; $params['orderId'] = ""; $params['data'] = ""; $params['storeId'] = $birthDay['store_id']; $params['code'] = $code; $params['customer'] = $customerInfo; $params['customerId'] = $birthDay['entity_id']; $params['cart'] = ""; $content = ""; if ($birthDay['email'] == "") { continue; } if ($this->_checkExistQueueEmail($rule['rule_id'], 0, $birthDay['email'], $emailTemplate['code'], time(), 1)) { $coupon = Mage::helper('followupemail/coupon')->generateCode($rule, $birthDay['email']); $params['coupon'] = $coupon; $queue->add(time(), $rule['rule_id'], 0, $emailTemplate['sender_name'], $emailTemplate['sender_email'], $customerInfo['first_name'] . ' ' . $customerInfo['last_name'], $customerInfo['customer_email'], $emailTemplate['subject'], $content, serialize($params), $emailTemplate['code'], 1, $code, "", $coupon); } } } } } }
public function directAction() { if ($code = $this->getRequest()->getParam('code')) { $code = str_replace(' ', '+', $code); $code = str_replace('special', '/', $code); $value = MW_FollowUpEmail_Helper_Data::decryptCode($code); $pos = strrpos($value, ","); if ($pos === false) { // note: three equal signs if (!($queue = Mage::getModel('followupemail/emailqueue')->loadByCode($code))) { Mage::getSingleton('core/session')->addError($this->__('Wrong direct code specified')); $this->_redirect('/'); return; } $customer = Mage::getModel('customer/customer')->setWebsiteId(Mage::app()->getStore()->getWebsiteId())->loadByEmail($queue->getRecipientEmail()); if ($customerId = $customer->getId()) { $session = Mage::getSingleton('customer/session'); if ($session->isLoggedIn() && $customerId != $session->getCustomerId()) { $session->logout(); } try { $session->setCustomerAsLoggedIn($customer); } catch (Exception $ex) { Mage::getSingleton('core/session')->addError($this->__('Your account isn\'t confirmed')); $this->_redirect('/'); } } else { $paramsV = unserialize($queue->getParams()); Mage::getSingleton('checkout/session')->setQuoteId($paramsV['cart']['quote_id']); Mage::getSingleton('core/session')->setEmailGuest($paramsV['cart']['customer_email']); } if ($queue->getOrderId() > 0) { $this->getResponse()->setRedirect(Mage::getUrl("sales/order/view", array('order_id' => $queue->getOrderId()))); } else { $this->getResponse()->setRedirect(Mage::getUrl('checkout/cart')); Mage::getModel('followupemail/observer')->eventDeleteQueueSpecial(MW_FollowUpEmail_Model_System_Config_Eventfollowupemail::EVENT_TYPE_CART_UPDATED, $paramsV['cart']['customer_email'], "", $groupId = 0, $storeId = 0, 1); } } else { $arrValue = explode(',', $value); $customer = Mage::getModel('customer/customer')->setWebsiteId(Mage::app()->getStore()->getWebsiteId())->loadByEmail($arrValue[0]); if ($customerId = $customer->getId()) { $session = Mage::getSingleton('customer/session'); if ($session->isLoggedIn() && $customerId != $session->getCustomerId()) { $session->logout(); } try { $session->setCustomerAsLoggedIn($customer); } catch (Exception $ex) { Mage::getSingleton('core/session')->addError($this->__('Your account isn\'t confirmed')); $this->_redirect('/'); } } else { if (!($queue = Mage::getModel('followupemail/emailqueue')->loadByCode($code))) { Mage::getSingleton('core/session')->addError($this->__('Wrong direct code specified')); $this->_redirect('/'); return; } $paramsV = unserialize($queue->getParams()); //print_r($paramsV['cart']);die; //print_r($paramsV['cart']['quote_id']);die; Mage::getSingleton('checkout/session')->setQuoteId($paramsV['cart']['quote_id']); Mage::getSingleton('core/session')->setEmailGuest($paramsV['cart']['customer_email']); Mage::getModel('followupemail/observer')->eventDeleteQueueSpecial(MW_FollowUpEmail_Model_System_Config_Eventfollowupemail::EVENT_TYPE_CART_UPDATED, $paramsV['cart']['customer_email'], "", $groupId = 0, $storeId = 0, 1); } if ($arrValue[1] == 'order') { $this->getResponse()->setRedirect(Mage::getUrl("sales/order/view", array('order_id' => $arrValue[2]))); } else { $this->getResponse()->setRedirect(Mage::getUrl('checkout/cart')); } } } else { Mage::getSingleton('core/session')->addError($this->__('No resume code cpecified')); $this->_redirect('/'); } }
public function sendTestEmail($data) { $emailChain = $data['email_chain']; $cart = null; $orderId = ""; $customerId = ""; $storeId = Mage::app()->getStore()->getStoreId(); $code = ""; $codeCart = ""; $productIds = array(); $senderInfo = array(); $senderInfo['sender_email'] = $data['sender_email']; $senderInfo['sender_name'] = $data['sender_name']; $webId = Mage::app()->getWebsite()->getId(); if ($webId == 0) { $webId = 1; } if ($data['testemail']['test_customer_name'] != "") { $customer = Mage::getModel("customer/customer"); $customer->setWebsiteId($webId); $customer->loadByEmail($data['testemail']['test_customer_name']); //load customer by email id if ($customer != null) { $customerId = $customer->getId(); $resource = Mage::getSingleton('core/resource'); $read = $resource->getConnection('core_read'); $select = $read->select()->from(array('q' => $resource->getTableName('sales/quote')), array('store_id' => 'q.store_id', 'quote_id' => 'q.entity_id', 'customer_id' => 'q.customer_id', 'subtotal' => 'q.subtotal', 'subtotal_with_discount' => 'q.subtotal_with_discount', 'grand_total' => 'q.grand_total', 'items_qty' => 'q.items_qty', 'updated_at' => 'q.updated_at'))->joinLeft(array('a' => $resource->getTableName('sales/quote_address')), 'q.entity_id=a.quote_id AND a.address_type="billing"', array('customer_email' => new Zend_Db_Expr('IFNULL(q.customer_email, a.email)'), 'customer_firstname' => new Zend_Db_Expr('IFNULL(q.customer_firstname, a.firstname)'), 'customer_middlename' => new Zend_Db_Expr('IFNULL(q.customer_middlename, a.middlename)'), 'customer_lastname' => new Zend_Db_Expr('IFNULL(q.customer_lastname, a.lastname)')))->joinInner(array('i' => $resource->getTableName('sales/quote_item')), 'q.entity_id=i.quote_id', array('product_ids' => new Zend_Db_Expr('GROUP_CONCAT(i.product_id)'), 'item_ids' => new Zend_Db_Expr('GROUP_CONCAT(i.item_id)')))->where('q.is_active=1')->where('q.customer_email = ?', $data['testemail']['test_customer_name'])->where('q.items_count>0')->where('i.parent_item_id IS NULL')->group('q.entity_id')->order('updated_at'); //mage::log(date(MW_FollowUpEmail_Model_Mysql4_Emailqueue::MYSQL_DATETIME_FORMAT,$now - ($intFromTimeHour+$intTimeLastHour))); $carts = $read->fetchAll($select); foreach ($carts as $_cart) { $cart = $_cart; } } } if ($cart != null) { $codeCart = MW_FollowUpEmail_Helper_Data::encryptCode($data['testemail']['test_customer_name'], 'cart', 0); } if ($data['testemail']['test_order_id'] != "") { $_order = Mage::getModel('sales/order')->loadByIncrementId($data['testemail']['test_order_id']); $orderId = $_order->getId(); $code = MW_FollowUpEmail_Helper_Data::encryptCode($data['testemail']['test_customer_name'], 'order', $orderId); $items = $_order->getAllItems(); foreach ($items as $item) { if ($item->getParentItem()) { continue; } $productIds[] = $item->getProductId(); } } $error = true; $templateId = ""; foreach ($emailChain as $emailChainItem) { $params = array(); if ($templateId == $emailChainItem['TEMPLATE_ID']) { continue; } $templateId = $emailChainItem['TEMPLATE_ID']; //get content of current email template $emailTemplate = $this->getTemplate($emailChainItem['TEMPLATE_ID'], $senderInfo); $timeSent = $emailChainItem['DAYS'] * 1440 + $emailChainItem['HOURS'] * 60 + $emailChainItem['MINUTES']; //$code = MW_FollowUpEmail_Helper_Data::getCodeSecurity(); $params['templateEmailId'] = $emailChainItem['TEMPLATE_ID']; $params['senderInfo'] = $senderInfo; $params['productIds'] = $productIds; $params['orderId'] = $orderId; $params['data'] = ""; $params['customer'] = ""; $params['customerId'] = $customerId; $params['cart'] = $cart; $params['storeId'] = $storeId; $params['code'] = $code; $params['codeCart'] = $codeCart; if (!$this->send($params, $emailTemplate, $data['testemail']['test_recipient'])) { $error = false; } } return $error; }