/** * Send order to MailChimp * * @param Mage_Sales_Model_Order $order * @return bool|array */ public function logSale($order) { $this->_order = $order; $api = Mage::getSingleton('monkey/api', array('store' => $this->_order->getStoreId())); if (!$api) { return false; } $subtotal = $this->_order->getSubtotal(); $discount = (double) $this->_order->getDiscountAmount(); if ($discount != 0) { $subtotal = $subtotal + $discount; } $this->_info = array('id' => $this->_order->getIncrementId(), 'total' => $subtotal, 'shipping' => $this->_order->getShippingAmount(), 'tax' => $this->_order->getTaxAmount(), 'store_id' => $this->_order->getStoreId(), 'store_name' => $this->_order->getStoreName(), 'plugin_id' => 1215, 'items' => array()); $emailCookie = $this->_getEmailCookie(); $campaignCookie = $this->_getCampaignCookie(); $this->setItemstoSend(); if ($emailCookie && $campaignCookie) { $this->_info['email_id'] = $emailCookie; $this->_info['campaign_id'] = $campaignCookie; //Send order to MailChimp $rs = $api->campaignEcommOrderAdd($this->_info); } else { $this->_info['email'] = $this->_order->getCustomerEmail(); $rs = $api->ecommOrderAdd($this->_info); } if ($rs === TRUE) { $this->_logCall(); return true; } else { return $rs; } }
/** Send order to MailChimp Automatically by Order Status * * */ public function autoExportJobs() { $allow_sent = false; $orderIds[] = '0'; $ecommerceOrders = Mage::getModel('monkey/ecommerce')->getCollection()->getData(); if ($ecommerceOrders) { foreach ($ecommerceOrders as $ecommerceOrder) { $orderIds[] = $ecommerceOrder['order_id']; } } $orders = Mage::getResourceModel('sales/order_collection'); //Get ALL orders which has not been sent to MailChimp $orders->getSelect()->where('main_table.entity_id NOT IN(?)', $orderIds); //Get status options selected in the Configuration $states = explode(',', Mage::helper('monkey')->config('order_status')); foreach ($orders as $order) { foreach ($states as $state) { if ($order->getStatus() == $state || $state == 'all_status') { $allow_sent = true; } } if ($allow_sent == true) { $this->_order = $order; $api = Mage::getSingleton('monkey/api', array('store' => $this->_order->getStoreId())); if (!$api) { return false; } $subtotal = $this->_order->getSubtotal(); $discount = (double) $this->_order->getDiscountAmount(); if ($discount != 0) { $subtotal = $subtotal + $discount; } $this->_info = array('id' => $this->_order->getIncrementId(), 'total' => $subtotal, 'shipping' => $this->_order->getShippingAmount(), 'tax' => $this->_order->getTaxAmount(), 'store_id' => $this->_order->getStoreId(), 'store_name' => $this->_order->getStoreName(), 'plugin_id' => 1215, 'items' => array()); $email = $this->_order->getCustomerEmail(); $campaign = $this->_order->getEbizmartsMagemonkeyCampaignId(); $this->setItemstoSend(); if ($email && $campaign) { $this->_info['email_id'] = $email; $this->_info['campaign_id'] = $campaign; //Send order to MailChimp $rs = $api->campaignEcommOrderAdd($this->_info); } else { $this->_info['email'] = $email; $rs = $api->ecommOrderAdd($this->_info); } $allow_sent = false; if ($rs === TRUE) { $this->_logCall(); } } } }
/** Send order to MailChimp Automatically by Order Status * * */ public function autoExportJobs($storeId) { $allow_sent = false; //Get status options selected in the Configuration $states = explode(',', Mage::getStoreConfig(Ebizmarts_MageMonkey_Model_Config::ECOMMERCE360_ORDER_STATUS, $storeId)); $max = Mage::getStoreConfig(Ebizmarts_MageMonkey_Model_Config::ECOMMERCE360_ORDER_MAX, $storeId); $count = 0; foreach ($states as $state) { if ($max == $count) { break; } $ecommerceTable = Mage::getSingleton('core/resource')->getTableName('monkey/ecommerce'); if ($state != 'all_status') { $orders = Mage::getResourceModel('sales/order_collection')->addFieldToFilter('main_table.store_id', array('eq' => $storeId)); // $orders->getSelect()->joinLeft(array('ecommerce' => Mage::getSingleton('core/resource')->getTableName('monkey/ecommerce')), 'main_table.entity_id = ecommerce.order_id', 'main_table.*')->where('ecommerce.order_id is null AND main_table.status = \'' . $state . '\'') // ->limit($max - $count); $orders->getSelect()->where('main_table.status = \'' . $state . '\' ' . 'AND main_table.entity_id NOT IN ' . "(SELECT ecommerce.order_id FROM {$ecommerceTable} AS ecommerce WHERE ecommerce.store_id = {$storeId})")->limit($max - $count); } else { $orders = Mage::getResourceModel('sales/order_collection')->addFieldToFilter('main_table.store_id', array('eq' => $storeId)); // $orders->getSelect()->joinLeft(array('ecommerce' => Mage::getSingleton('core/resource')->getTableName('monkey/ecommerce')), 'main_table.entity_id = ecommerce.order_id', 'main_table.*')->where('ecommerce.order_id is null') // ->limit($max - $count); $orders->getSelect()->where('main_table.entity_id NOT IN ' . "(SELECT ecommerce.order_id FROM {$ecommerceTable} AS ecommerce WHERE ecommerce.store_id = {$storeId})")->limit($max - $count); } $count += count($orders); foreach ($orders as $order) { $this->_order = $order; $ordersToSend = Mage::getModel('monkey/asyncorders')->getCollection()->addFieldToFilter('processed', array('eq' => 0)); foreach ($ordersToSend as $orderToSend) { $info = (array) unserialize($orderToSend->getInfo()); if ($this->_order->getIncrementId() == $info['id']) { continue; } } $api = Mage::getSingleton('monkey/api', array('store' => $this->_order->getStoreId())); if (!$api) { return false; } $subtotal = $this->_order->getBaseSubtotal(); $discount = (double) $this->_order->getBaseDiscountAmount(); if ($discount != 0) { $subtotal = $subtotal + $discount; } $createdAtArr = str_split($this->_order->getCreatedAt(), 10); $this->_info = array('id' => $this->_order->getIncrementId(), 'total' => $subtotal, 'shipping' => $this->_order->getBaseShippingAmount(), 'tax' => $this->_order->getBaseTaxAmount(), 'store_id' => $this->_order->getStoreId(), 'store_name' => $this->_order->getStoreName(), 'order_date' => $createdAtArr[0], 'plugin_id' => 1215, 'items' => array()); $email = $this->_order->getCustomerEmail(); $campaign = $this->_order->getEbizmartsMagemonkeyCampaignId(); $this->setItemstoSend($storeId); $rs = false; if ($email && $campaign) { $this->_info['email_id'] = $email; $this->_info['campaign_id'] = $campaign; if (Mage::getStoreConfig('monkey/general/checkout_async', Mage::app()->getStore()->getId())) { $sync = Mage::getModel('monkey/asyncorders'); $this->_info['order_id'] = $this->_order->getId(); $sync->setInfo(serialize($this->_info))->setCreatedAt($createdAtArr[0])->setProcessed(0)->save(); $rs['complete'] = true; } else { //Send order to MailChimp $rs = $api->campaignEcommOrderAdd($this->_info); } } else { $this->_info['email'] = $email; if (Mage::getStoreConfig('monkey/general/checkout_async', Mage::app()->getStore()->getId())) { $sync = Mage::getModel('monkey/asyncorders'); $this->_info['order_id'] = $this->_order->getId(); $sync->setInfo(serialize($this->_info))->setCreatedAt(Mage::getModel('core/date')->gmtDate())->setProcessed(0)->save(); $rs['complete'] = true; } else { $rs = $api->ecommOrderAdd($this->_info); } } if (isset($rs['complete']) && $rs['complete'] == TRUE && !Mage::getStoreConfig('monkey/general/checkout_async', Mage::app()->getStore()->getId())) { $order = Mage::getModel('monkey/ecommerce')->setOrderIncrementId($this->_info['id'])->setOrderId($this->_info['order_id'])->setMcEmailId($this->_info['email'])->setCreatedAt($createdAtArr[0])->setStoreId($this->_info['store_id']); if (isset($this->_info['campaign_id']) && $this->_info['campaign_id']) { $order->setMcCampaignId($this->_info['campaign_id']); } $order->save(); //$this->_logCall(); } } } }
/** * set the order information * * @param Mage_Sales_Model_Order $orderData */ public function setOrderData(Mage_Sales_Model_Order $orderData) { $this->id = (string) $orderData->getIncrementId(); $this->quote_id = (string) $orderData->getQuoteId(); $this->email = (string) $orderData->getCustomerEmail(); $this->store_name = (string) $orderData->getStoreName(); $created_at = new Zend_Date($orderData->getCreatedAt(), Zend_Date::ISO_8601); $this->purchase_date = $created_at->toString(Zend_Date::ISO_8601); $this->delivery_method = $orderData->getShippingDescription(); $this->delivery_total = (double) number_format($orderData->getShippingAmount(), 2, '.', ''); $this->currency = $orderData->getStoreCurrencyCode(); if ($payment = $orderData->getPayment()) { $this->payment = $payment->getMethodInstance()->getTitle(); } $this->couponCode = (string) $orderData->getCouponCode(); //set order custom attributes $this->_setOrderCustomAttributes($orderData); //billing $this->_setBillingData($orderData); //shipping $this->_setShippingData($orderData); //order items $this->_setOrderItems($orderData); //sales data $this->order_subtotal = (double) number_format($orderData->getData('subtotal'), 2, '.', ''); $this->discount_ammount = (double) number_format($orderData->getData('discount_amount'), 2, '.', ''); $orderTotal = abs($orderData->getData('grand_total') - $orderData->getTotalRefunded()); $this->order_total = (double) number_format($orderTotal, 2, '.', ''); $this->order_status = (string) $orderData->getStatus(); }
/** * Build a hash over some unmodifiable(!) order properties. * * @param Mage_Sales_Model_Order $order * @return string */ public function createHashForOrder(Mage_Sales_Model_Order $order) { $orderHash = $order->getId(); $orderHash .= $order->getIncrementId(); $orderHash .= $order->getQuoteId(); $orderHash .= $order->getCustomerEmail(); $orderHash .= $order->getCustomerFirstname(); $orderHash .= $order->getCustomerLastname(); $orderHash .= $order->getShippingMethod(); $orderHash .= $order->getStoreName(); $orderHash .= $order->getGrandTotal(); return hash("sha512", $orderHash); }
/** * set the order information * @param Mage_Sales_Model_Order $orderData */ public function __construct(Mage_Sales_Model_Order $orderData) { $customerModel = Mage::getModel('customer/customer'); $customerModel->load($orderData->getCustomerId()); $this->id = $orderData->getIncrementId(); $this->quote_id = $orderData->getQuoteId(); $this->email = $orderData->getCustomerEmail(); $this->store_name = $orderData->getStoreName(); $created_at = new Zend_Date($orderData->getCreatedAt(), Zend_Date::ISO_8601); $this->purchase_date = $created_at->toString(Zend_Date::ISO_8601); $this->delivery_method = $orderData->getShippingDescription(); $this->delivery_total = $orderData->getShippingAmount(); $this->currency = $orderData->getStoreCurrencyCode(); if ($payment = $orderData->getPayment()) { $this->payment = $payment->getMethodInstance()->getTitle(); } $this->couponCode = $orderData->getCouponCode(); /** * custom order attributes */ $helper = Mage::helper('ddg'); $website = Mage::app()->getStore($orderData->getStore())->getWebsite(); $customAttributes = $helper->getConfigSelectedCustomOrderAttributes($website); if ($customAttributes) { $fields = Mage::getResourceModel('ddg_automation/order')->getOrderTableDescription(); foreach ($customAttributes as $customAttribute) { if (isset($fields[$customAttribute])) { $field = $fields[$customAttribute]; $value = $this->_getCustomAttributeValue($field, $orderData); if ($value) { $this->_assignCustom($field, $value); } } } } /** * Billing address. */ if ($orderData->getBillingAddress()) { $billingData = $orderData->getBillingAddress()->getData(); $this->billing_address = array('billing_address_1' => $this->_getStreet($billingData['street'], 1), 'billing_address_2' => $this->_getStreet($billingData['street'], 2), 'billing_city' => $billingData['city'], 'billing_region' => $billingData['region'], 'billing_country' => $billingData['country_id'], 'billing_postcode' => $billingData['postcode']); } /** * Shipping address. */ if ($orderData->getShippingAddress()) { $shippingData = $orderData->getShippingAddress()->getData(); $this->delivery_address = array('delivery_address_1' => $this->_getStreet($shippingData['street'], 1), 'delivery_address_2' => $this->_getStreet($shippingData['street'], 2), 'delivery_city' => $shippingData['city'], 'delivery_region' => $shippingData['region'], 'delivery_country' => $shippingData['country_id'], 'delivery_postcode' => $shippingData['postcode']); } $syncCustomOption = $helper->getWebsiteConfig(Dotdigitalgroup_Email_Helper_Config::XML_PATH_CONNECTOR_SYNC_ORDER_PRODUCT_CUSTOM_OPTIONS, $website); /** * Order items. * @var Mage_Sales_Model_Order_Item $productItem */ foreach ($orderData->getAllItems() as $productItem) { //product custom options $customOptions = array(); if ($syncCustomOption) { $customOptions = $this->_getOrderItemOptions($productItem); } $product = $productItem->getProduct(); if ($product) { // category names $categoryCollection = $product->getCategoryCollection()->addAttributeToSelect('name'); $productCat = array(); foreach ($categoryCollection as $cat) { $categories = array(); $categories[] = $cat->getName(); $productCat[]['Name'] = substr(implode(', ', $categories), 0, 244); } $attributes = array(); //selected attributes from config $configAttributes = Mage::helper('ddg')->getWebsiteConfig(Dotdigitalgroup_Email_Helper_Config::XML_PATH_CONNECTOR_SYNC_ORDER_PRODUCT_ATTRIBUTES, $orderData->getStore()->getWebsite()); if ($configAttributes) { $configAttributes = explode(',', $configAttributes); //attributes from attribute set $attributesFromAttributeSet = $this->_getAttributesArray($product->getAttributeSetId()); foreach ($configAttributes as $attribute_code) { //if config attribute is in attribute set if (in_array($attribute_code, $attributesFromAttributeSet)) { //attribute input type $inputType = $product->getResource()->getAttribute($attribute_code)->getFrontend()->getInputType(); //fetch attribute value from product depending on input type switch ($inputType) { case 'multiselect': case 'select': case 'dropdown': $value = $product->getAttributeText($attribute_code); break; default: $value = $product->getData($attribute_code); break; } if ($value) { // check limit on text and assign value to array $attributes[][$attribute_code] = $this->_limitLength($value); } } } } $attributeSetName = $this->_getAttributeSetName($product); $this->products[] = array('name' => $productItem->getName(), 'sku' => $productItem->getSku(), 'qty' => (int) number_format($productItem->getData('qty_ordered'), 2), 'price' => (double) number_format($productItem->getPrice(), 2, '.', ''), 'attribute-set' => $attributeSetName, 'categories' => $productCat, 'attributes' => $attributes, 'custom-options' => $customOptions); } else { // when no product information is available limit to this data $this->products[] = array('name' => $productItem->getName(), 'sku' => $productItem->getSku(), 'qty' => (int) number_format($productItem->getData('qty_ordered'), 2), 'price' => (double) number_format($productItem->getPrice(), 2, '.', ''), 'attribute-set' => '', 'categories' => array(), 'attributes' => array(), 'custom-options' => $customOptions); } } $this->order_subtotal = (double) number_format($orderData->getData('subtotal'), 2, '.', ''); $this->discount_ammount = (double) number_format($orderData->getData('discount_amount'), 2, '.', ''); $orderTotal = abs($orderData->getData('grand_total') - $orderData->getTotalRefunded()); $this->order_total = (double) number_format($orderTotal, 2, '.', ''); $this->order_status = $orderData->getStatus(); return true; }