/** * Loads customer, quote and quote item by request params * * @return Mage_Adminhtml_Customer_Cart_Product_Composite_CartController */ protected function _initData() { $customerId = (int) $this->getRequest()->getParam('customer_id'); if (!$customerId) { Mage::throwException($this->__('No customer id defined.')); } $this->_customer = Mage::getModel('Mage_Customer_Model_Customer')->load($customerId); $quoteItemId = (int) $this->getRequest()->getParam('id'); $websiteId = (int) $this->getRequest()->getParam('website_id'); $this->_quote = Mage::getModel('Mage_Sales_Model_Quote')->setWebsite(Mage::app()->getWebsite($websiteId))->loadByCustomer($this->_customer); $this->_quoteItem = $this->_quote->getItemById($quoteItemId); if (!$this->_quoteItem) { Mage::throwException($this->__('Wrong quote item.')); } return $this; }
/** * This function will attempt to update the quote catalog redemptions * data. This should be triggered when the cart changes. * @param Mage_Sales_Model_Quote $quote * @param array $data[=null] * @param unknown_type $date[=null] * @param int $wId[=null] */ public function updateQuoteCatalogRedemptions($quote, $data = null, $date = null, $wId = null) { try { if (!$quote) { return $this; } if ($data) { //@nelkaake -a 17/02/11: A save-on-quote event $is_on_quote_save = false; $items = $data; } else { $is_on_quote_save = true; $items = $quote->getAllItems(); } if (!is_array($items)) { $items = array($items); } $refactorItems = array(); foreach ($items as $key => $itemInfo) { if ($is_on_quote_save) { $itemId = $itemInfo->getId(); $item = $itemInfo; } else { $itemId = $key; $item = $quote->getItemById($itemId); } if (!$itemId || !$item) { continue; } if (!$is_on_quote_save) { if (!empty($itemInfo['remove']) || isset($itemInfo['qty']) && $itemInfo['qty'] == '0') { continue; } } $product = $item->getProduct(); $pId = $product->getId(); $storeId = $product->getStoreId(); if (!$date) { $date = Mage::helper('rewards')->now(); } if ($wId) { $wId = Mage::app()->getStore($storeId)->getWebsiteId(); } $gId = Mage::getSingleton('customer/session')->getCustomerGroupId(); if ($gId !== 0 && empty($gId)) { $gId = Mage_Customer_Model_Group::NOT_LOGGED_IN_ID; } if ($is_on_quote_save) { $qty = $item->getQty(); } else { $qty = isset($itemInfo['qty']) ? (double) $itemInfo['qty'] : false; } // Since the cart has changed, reset our before-redemptions value and let Sweet Tooth recalculate the discount amount. Mage::getSingleton('rewards/redeem')->resetBeforeDiscount($item); $refactorItems[] = $item; } Mage::getSingleton('rewards/redeem')->refactorRedemptions($refactorItems); } catch (Exception $e) { Mage::logException($e); Mage::helper('rewards')->log($e->getMessage()); Mage::helper('rewards')->log($e); die($e->getMessage()); } }
/** * Save changed order products * * @param Mage_Sales_Model_Quote $quote * @param Mage_Sales_Model_Order $order * @param $changes * @return $this */ public function saveOldOrderItems(Mage_Sales_Model_Quote $quote, Mage_Sales_Model_Order $order, $changes) { foreach ($changes as $itemId => $params) { $quoteItem = $quote->getItemById($itemId); $orderItem = $order->getItemByQuoteItemId($itemId); if (!$orderItem) { continue; } $qtyToRefund = $orderItem->getQtyToRefund(); if ($orderItem->getProductType() == 'bundle') { $qtyToRefund = $orderItem->getQtyInvoiced() - $orderItem->getQtyRefunded(); } $orderItemQty = $qtyToRefund + $orderItem->getQtyToCancel(); if (isset($params['action']) && $params['action'] == 'remove' || isset($params['qty']) && $params['qty'] < 1) { $this->returnOrderItem($order, $orderItem); $orderItem->setSubtotal(0)->setBaseSubtotal(0)->setTaxAmount(0)->setBaseTaxAmount(0)->setTaxPercent(0)->setDiscountAmount(0)->setBaseDiscountAmount(0)->setRowTotal(0)->setBaseRowTotal(0); continue; } $origQtyOrdered = $orderItem->getQtyOrdered(); $orderItem = $this->getConvertor()->itemToOrderItem($quoteItem, $orderItem); if (isset($params['qty']) && $params['qty'] != $orderItemQty) { $qtyDiff = $params['qty'] - $orderItemQty; if ($params['qty'] < $orderItemQty) { $qtyToRemove = $orderItemQty - $params['qty']; $this->returnOrderItem($order, $orderItem, $qtyToRemove); $orderItem->setQtyOrdered($origQtyOrdered); } else { $orderItem->setQtyOrdered($origQtyOrdered + $qtyDiff); } if ($orderItem->getProductType() == 'bundle') { foreach ($quote->getAllItems() as $childQuoteItem) { if ($childQuoteItem->getParentItemId() != $quoteItem->getId()) { continue; } $childQuoteItem->save(); $childOrderItem = $order->getItemByQuoteItemId($childQuoteItem->getId()); $childOrderItem->setParentItem($orderItem); $origChildQtyOrdered = $childOrderItem->getQtyOrdered(); $childOrderItem = $this->getConvertor()->itemToOrderItem($childQuoteItem, $childOrderItem); // Mage::helper('core')->copyFieldset('sales_convert_quote_item', 'to_order_item', $childQuoteItem, $childOrderItem); if ($params['qty'] < $orderItemQty) { $qtyToRemove = $origChildQtyOrdered - $childOrderItem->getQtyOrdered() - $childOrderItem->getQtyCanceled() - $childOrderItem->getQtyRefunded(); $this->returnOrderItem($order, $childOrderItem, $qtyToRemove); $childOrderItem->setQtyOrdered($origChildQtyOrdered); } else { $childQtyDiff = $qtyDiff * $childQuoteItem->getQty(); $childOrderItem->setQtyOrdered($origChildQtyOrdered + $childQtyDiff); } $childOrderItem->save(); $this->_savedOrderItems[] = $childOrderItem->getItemId(); } } } // Check Qty & Price changes $itemChange = array('name' => $orderItem->getName(), 'qty_before' => $orderItemQty, 'qty_after' => $orderItem->getQtyToRefund() + $orderItem->getQtyToCancel(), 'price_before' => $orderItem->getOrigData('price'), 'price_after' => $orderItem->getPrice()); // Check Discount changes if (isset($params['use_discount']) && $params['use_discount'] == 1 && $quoteItem->getOrigData('discount_amount') == 0 && $quoteItem->getData('discount_amount') > 0) { $itemChange['discount'] = 1; } elseif ($quoteItem->getData('discount_amount') < 0.001 && $quoteItem->getOrigData('discount_amount') > 0) { $itemChange['discount'] = -1; } // Add item changes to log if ($itemChange['qty_before'] != $itemChange['qty_after'] || $itemChange['price_before'] != $itemChange['price_after'] || isset($itemChange['discount'])) { $this->getLogModel()->addItemChange($orderItem->getId(), $itemChange); } $quoteItem->save(); $orderItem->save(); $this->_savedOrderItems[] = $orderItem->getItemId(); } return $this; }
/** * @param Mage_Sales_Model_Quote $quote * @param int $id * * @return Mage_Sales_Model_Quote_Item * * @throws Exception */ protected function loadItem(Mage_Sales_Model_Quote $quote, $id) { $id = intval($id); if (!$id) { $this->_critical('Not Found', Mage_Api2_Model_Server::HTTP_NOT_FOUND); } /** @var Mage_Sales_Model_Quote_Item $item */ $item = $quote->getItemById($id); if (!$item || $item->isDeleted() || $item->getParentItemId()) { $this->_critical('Not Found', Mage_Api2_Model_Server::HTTP_NOT_FOUND); } return $item; }
/** * Apply updated order items to quote * * @param Mage_Sales_Model_Quote $quote * @param $data * @return $this */ public function updateItems(Mage_Sales_Model_Quote $quote, $data) { foreach ($data as $itemId => $params) { $quoteItem = $quote->getItemById($itemId); if (!$quoteItem) { $this->addNewItems($quote, array($itemId => $params)); $quoteItem = $quote->getItemById($itemId); if (!$quoteItem) { continue; } } if (isset($params['action']) && $params['action'] == 'remove' || isset($params['qty']) && $params['qty'] < 1) { $quote->removeItem($itemId); continue; } if (isset($params['qty'])) { $quoteItem->setQty($params['qty']); } if (isset($params['custom_price']) && $params['custom_price'] > 0) { $quoteItem->setCustomPrice((double) $params['custom_price']); $quoteItem->setOriginalCustomPrice((double) $params['custom_price']); } $noDiscount = !isset($params['use_discount']); $quoteItem->setNoDiscount($noDiscount); $quoteItem->save(); } return $this; }