/** * @param Bronto_Api_Order $orderObject * @param array $orderCache * @param array $result * * @return array * @access protected */ protected function _flushOrders($orderObject, $orderCache, $result) { // Get delivery results from order object $flushResult = $orderObject->flush(); $flushCount = count($flushResult); // Log Order import flush process starting $this->_helper->writeDebug(" Flush resulted in {$flushCount} orders processed"); $this->_helper->writeVerboseDebug('===== FLUSH =====', 'bronto_order_api.log'); $this->_helper->writeVerboseDebug(var_export($orderObject->getApi()->getLastRequest(), true), 'bronto_order_api.log'); $this->_helper->writeVerboseDebug(var_export($orderObject->getApi()->getLastResponse(), true), 'bronto_order_api.log'); // Cycle through flush results and handle any errors that were returned foreach ($flushResult as $i => $flushResultRow) { if ($flushResultRow->hasError()) { $hasError = true; $errorCode = $flushResultRow->getErrorCode(); $errorMessage = $flushResultRow->getErrorMessage(); } else { $hasError = false; $errorCode = false; $errorMessage = false; } if (isset($orderCache[$i])) { /** @var Mage_Sales_Model_Order $order */ $order = Mage::getModel('sales/order')->load($orderCache[$i]['orderId']); /** @var Mage_Core_Model_Store $store */ $store = Mage::getModel('core/store')->load($orderCache[$i]['storeId']); /** @var Mage_Core_Model_Website $website */ $website = Mage::getModel('core/website')->load($store->getWebsiteId()); $storeMessage = "For `{$website->getName()}`:`{$store->getName()}`: "; /** @var Bronto_Order_Model_Queue $orderRow */ $orderRow = Mage::getModel('bronto_order/queue')->getOrderRow($order->getId(), $order->getQuoteId(), $order->getStoreId()); } else { if ($hasError) { Mage::helper('bronto_order')->writeError("[{$errorCode}] {$errorMessage}"); $result['error']++; } continue; } if ($hasError) { // If error code is 915, try to pull customer email address if (915 == $errorCode) { if ($customerEmail = $order->getCustomerEmail()) { $errorMessage = "Invalid Email Address: `{$customerEmail}`"; } else { $errorMessage = "Email Address is empty for this order"; } } // Append order id to message to assist troubleshooting $errorMessage .= " (Order #: {$order->getIncrementId()})"; // Log and Display error message $this->_helper->writeError("[{$errorCode}] {$storeMessage}{$errorMessage}"); // Reset Bronto Import status $orderRow->setBrontoImported(null)->setBrontoSuppressed($errorMessage)->save(); // Increment number of errors $result['error']++; } else { $orderRow->setBrontoImported(Mage::getSingleton('core/date')->gmtDate()); $orderRow->save(); // Increment number of successes $result['success']++; } } return $result; }
/** * Function to persist orders to be flushed to Bronto * * @param Mage_Sales_Model_Order $order * @param Bronto_Api_Order $brontoOrder * @param array $context */ protected function _persistOrder($order, $brontoOrder, $context) { extract($context); // Get visible items from order $items = $order->getAllVisibleItems(); // Keep product order by using a new array $fullItems = array(); $brontoOrderItems = array(); // loop through the items. if it's a bundled item, // replace the parent item with the child items. foreach ($items as $item) { $itemProduct = Mage::getModel('catalog/product')->load($item->getProductId()); // Handle product based on product type switch ($itemProduct->getTypeId()) { // Bundled products need child items case Mage_Catalog_Model_Product_Type::TYPE_BUNDLE: if (count($item->getChildrenItems()) > 0) { foreach ($item->getChildrenItems() as $childItem) { if ($childItem->getPrice() != 0) { $item->setPrice(0); } $fullItems[] = $childItem; } } $fullItems[] = $item; break; // Configurable products just need simple config item // Configurable products just need simple config item case Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE: $childItems = $item->getChildrenItems(); if (1 === count($childItems)) { $childItem = $childItems[0]; // Collect options applicable to the configurable product $productAttributeOptions = $itemProduct->getTypeInstance(true)->getConfigurableAttributesAsArray($itemProduct); // Build Selected Options Name $nameWithOptions = array(); foreach ($productAttributeOptions as $productAttribute) { $itemValue = $productHelper->getProductAttribute($childItem->getProductId(), $productAttribute['attribute_code'], $storeId); $nameWithOptions[] = $productAttribute['label'] . ': ' . $itemValue; } // Set parent product name to include selected options $parentName = $item->getName() . ' [' . implode(', ', $nameWithOptions) . ']'; $item->setName($parentName); } $fullItems[] = $item; break; // Grouped products need parent and child items // Grouped products need parent and child items case Mage_Catalog_Model_Product_Type::TYPE_GROUPED: // This condition probably never gets hit, parent grouped items don't show in order $fullItems[] = $item; foreach ($item->getChildrenItems() as $child_item) { $fullItems[] = $child_item; } break; // Anything else (namely simples) just get added to array // Anything else (namely simples) just get added to array default: $fullItems[] = $item; break; } } // Cycle through newly created array of products foreach ($fullItems as $item) { // If product has a parent, get that parent product $parent = false; if ($item->getParentItem()) { $parent = Mage::getModel('catalog/product')->setStoreId($storeId)->load($item->getParentItem()->getProductId()); } /* @var $product Mage_Catalog_Model_Product */ $product = Mage::getModel('catalog/product')->setStoreId($storeId)->load($item->getProductId()); // If there is a parent product, use that to get category ids if ($parent) { $categoryIds = $parent->getCategoryIds(); } else { $categoryIds = $product->getCategoryIds(); } // If the product type is simple and the description // is empty, then attempt to find a parent product // to backfill the description. $parentProduct = $productHelper->getConfigurableProduct($product); if (!$product->getData($descriptionAttr)) { $product->setData($descriptionAttr, $parentProduct->getData($descriptionAttr)); } if (empty($categoryIds)) { $categoryIds = $parentProduct->getCategoryIds(); } // Cycle through category ids to pull category details $categories = array(); foreach ($categoryIds as $categoryId) { /* @var $category Mage_Catalog_Model_Category */ $category = Mage::getModel('catalog/category')->load($categoryId); $parent = $category->getParentCategory(); $categories[] = $parent->getUrlKey() ? $parent->getUrlKey() : $parent->formatUrlKey($parent->getName()); $categories[] = $category->getUrlKey() ? $category->getUrlKey() : $category->formatUrlKey($category->getName()); } // Check to ensure there are no duplicate categories $categories = array_unique($categories); // Write orderItem $brontoOrderItems[] = array('id' => $item->getId(), 'sku' => $item->getSku(), 'name' => $item->getName(), 'description' => $product->getData($descriptionAttr), 'category' => implode(' ', $categories), 'image' => $this->_helper->getItemImg($item, $product, $storeId), 'url' => $this->_helper->getItemUrl($item, $product, $storeId), 'quantity' => (int) $item->getQtyOrdered(), 'price' => $this->_helper->getItemPrice($item, $basePrefix, $inclTaxes, $inclDiscounts)); } if ($inclShipping && $order->getState() == Mage_Sales_Model_Order::STATE_COMPLETE && $order->hasShipments()) { $shippingObject = new Varien_Object(array('qty_ordered' => 1, 'base_row_total' => $order->getBaseShippingAmount(), 'row_total' => $order->getShippingAmount(), 'base_tax_amount' => $order->getBaseShippingTaxAmount(), 'tax_amount' => $order->getShippingTaxAmount(), 'base_discount_amount' => $order->getBaseShippingDiscountAmount(), 'discount_amount' => $order->getShippingDiscountAmount())); $descriptions = array(); foreach ($order->getTracksCollection() as $track) { if ($track->hasTrackNumber() && $track->hasTitle()) { $descriptions[] = "{$track->getTitle()} - {$track->getTrackNumber()}"; } } $shipmentItem = array('sku' => 'SHIPPING', 'name' => $order->getShippingDescription(), 'description' => implode("<br/>", $descriptions), 'quantity' => 1, 'price' => $this->_helper->getItemPrice($shippingObject, $basePrefix, $inclTaxes, $inclDiscounts)); $brontoOrderItems[] = $shipmentItem; } $brontoOrder->withProducts($brontoOrderItems); }