/** * Test if the item needs to have its quantity checked for available * inventory. * @param Mage_Sales_Model_Quote_Item $item The item to check * @return bool True if inventory is managed, false if not */ public function isItemInventoried(Mage_Sales_Model_Quote_Item $item) { // never consider a child product as inventoried, allow the parent deal // with inventory and let the child not need to worry about it as the parent // will be the item to keep track of qty being ordered. // Both checks needed as child items will not have a parent item id prior // to being saved and a parent item prior to being added to the parent (e.g. // immediately after being loaded from the DB). if ($item->getParentItemId() || $item->getParentItem()) { return false; } // when dealing with parent items, if any child of the product is managed // stock, consider the entire item as managed stock - allows for the parent // config product in the quote to deal with inventory while allowing child // products to not care individually if ($item->getHasChildren()) { foreach ($item->getChildren() as $childItem) { $childStock = $childItem->getProduct()->getStockItem(); if ($this->isManagedStock($childStock)) { // This Parent is inventoried. Child's ROM setting is 'No backorders', and Manage Stock check hasn't been manually overridden return true; } } // if none of the children were managed stock, the parent is not inventoried return false; } return $this->isManagedStock($item->getProduct()->getStockItem()); }
/** * Gets the item information for standard items or child items of configurable products * * @param Mage_Sales_Model_Quote_Item $item * @param bool $cartFreeShipping * @return Webshopapps_Wsacommon_Model_Totals */ protected function _getStdWeightQtyTotals($item, $cartFreeShipping) { $finalTotals = new Webshopapps_Wsacommon_Model_Totals(); $addressWeight = 0; $addressQty = 0; $freeMethodWeight = 0; if ($item->getHasChildren() && ($item->isShipSeparately() || $item->getProduct()->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE)) { foreach ($item->getChildren() as $child) { if ($child->getProduct()->isVirtual()) { continue; } $addressQty += $item->getQty() * $child->getQty(); $itemWeight = $child->getWeight(); $itemQty = $child->getTotalQty(); $rowWeight = $itemWeight * $itemQty; if ($cartFreeShipping || $child->getFreeShipping() === true) { $rowWeight = 0; } elseif (is_numeric($child->getFreeShipping())) { $freeQty = $child->getFreeShipping(); if ($itemQty > $freeQty) { $rowWeight = $itemWeight * ($itemQty - $freeQty); } else { $rowWeight = 0; } } $freeMethodWeight += $rowWeight; $addressWeight += $rowWeight; } } else { $addressQty += $item->getQty(); $itemWeight = $item->getWeight(); $rowWeight = $itemWeight * $item->getQty(); $addressWeight += $rowWeight; if ($cartFreeShipping || $item->getFreeShipping() === true) { $rowWeight = 0; } elseif (is_numeric($item->getFreeShipping())) { $freeQty = $item->getFreeShipping(); if ($item->getQty() > $freeQty) { $rowWeight = $itemWeight * ($item->getQty() - $freeQty); } else { $rowWeight = 0; } } $freeMethodWeight += $rowWeight; } $finalTotals->setWeight($addressWeight); $finalTotals->setFreeMethodWeight($freeMethodWeight); // TODO This wasnt in original, needs checking $finalTotals->setQty($addressQty); return $finalTotals; }
/** * Validate stock of a quoteItem * * @param Mage_Sales_Model_Quote_Item $item * @param float $priceInclTax * @param float $priceExclTax * * @return ShopgateCartItem $result */ public function validateStock(Mage_Sales_Model_Quote_Item $item, $priceInclTax, $priceExclTax) { $product = $item->getProduct(); /** @var Mage_CatalogInventory_Model_Stock_Item $stockItem */ $stockItem = $product->getStockItem(); $errors = array(); $isBuyable = true; $qtyBuyable = null; foreach ($item->getChildren() as $childItem) { /** @var Mage_Catalog_Model_Product $childProduct */ $childProduct = $childItem->getProduct(); /** @var Mage_CatalogInventory_Model_Stock_Item $childStock */ $childStock = $childProduct->getStockItem(); if ($childStock->getManageStock() && !$childProduct->isSaleable() && !$childStock->getBackorders()) { $isBuyable = false; $error = array(); $error['type'] = ShopgateLibraryException::CART_ITEM_OUT_OF_STOCK; $error['message'] = ShopgateLibraryException::getMessageFor(ShopgateLibraryException::CART_ITEM_OUT_OF_STOCK); $errors[] = $error; } else { if ($childStock->getManageStock() && !$childStock->checkQty($childItem->getQty()) && !$childStock->getBackorders()) { $isBuyable = false; $error = array(); $error['type'] = ShopgateLibraryException::CART_ITEM_REQUESTED_QUANTITY_NOT_AVAILABLE; $error['message'] = ShopgateLibraryException::getMessageFor(ShopgateLibraryException::CART_ITEM_REQUESTED_QUANTITY_NOT_AVAILABLE); $errors[] = $error; if ($qtyBuyable == null || $qtyBuyable > $childStock->getQty()) { $qtyBuyable = $childStock->getQty(); } } else { if (Mage::helper('shopgate/config')->getIsMagentoVersionLower1410()) { $checkIncrements = Mage::helper('shopgate')->checkQtyIncrements($childStock, $childItem->getQty()); } else { $checkIncrements = $childStock->checkQtyIncrements($childItem->getQty()); } if ($childStock->getManageStock() && $checkIncrements->getHasError()) { $isBuyable = false; $error = array(); $error['type'] = ShopgateLibraryException::CART_ITEM_REQUESTED_QUANTITY_NOT_AVAILABLE; $error['message'] = ShopgateLibraryException::getMessageFor(ShopgateLibraryException::CART_ITEM_REQUESTED_QUANTITY_NOT_AVAILABLE); $errors[] = $error; $stockItem->setQty((int) ($item->getQtyToAdd() / $stockItem->getQtyIncrements()) * $stockItem->getQtyIncrements()); } } } } $qtyBuyable = $qtyBuyable == null ? (int) $item->getQty() : (int) $qtyBuyable; return Mage::helper('shopgate')->generateShopgateCartItem($product, $isBuyable, $qtyBuyable, $priceInclTax, $priceExclTax, $errors, (int) $stockItem->getQty()); }
/** * Delete quote item. If it does not have identifier then it will be only removed from collection * * @param Mage_Sales_Model_Quote_Item $item * @return Mage_Sales_Model_Quote */ public function deleteItem(Mage_Sales_Model_Quote_Item $item) { if ($item->getId()) { $this->removeItem($item->getId()); } else { $quoteItems = $this->getItemsCollection(); $items = array($item); if ($item->getHasChildren()) { foreach ($item->getChildren() as $child) { $items[] = $child; } } foreach ($quoteItems as $key => $quoteItem) { foreach ($items as $item) { if ($quoteItem->compare($item)) { $quoteItems->removeItemByKey($key); } } } } return $this; }
public function getProductInformationFromQuoteItem(Mage_Sales_Model_Quote_Item $item, $additionalInformation = array()) { $children = $item->getChildren(); return $this->_getProductInformationWork($item, $additionalInformation, count($children) > 0, $children); }
/** * determines if we should skip the items with special price or other (in futeure) conditions * * @param Mage_Sales_Model_Quote_Item $item * @param Amasty_Promo_Model_Sales_Quote_Address $address * * @return bool */ protected function _skip($item, $address) { if (!Mage::getStoreConfig('ampromo/general/skip_special_price')) { return false; } if ($item->getProductType() == 'bundle') { return false; } if (is_null($this->_itemsWithDiscount) || count($this->_itemsWithDiscount) == 0) { $productIds = array(); $this->_itemsWithDiscount = array(); foreach ($this->_getAllItems($address) as $addressItem) { $productIds[] = $addressItem->getProductId(); } if (!$productIds) { return false; } $productsCollection = Mage::getModel('catalog/product')->getCollection()->addPriceData()->addAttributeToFilter('entity_id', array('in' => $productIds))->addAttributeToFilter('price', array('gt' => new Zend_Db_Expr('final_price'))); foreach ($productsCollection as $product) { $this->_itemsWithDiscount[] = $product->getId(); } } if (Mage::getStoreConfig('ampromo/general/skip_special_price_configurable')) { if ($item->getProductType() == "configurable") { foreach ($item->getChildren() as $child) { if (in_array($child->getProductId(), $this->_itemsWithDiscount)) { return true; } } } } if (!in_array($item->getProductId(), $this->_itemsWithDiscount)) { return false; } return true; }
/** * override for changing the current stock qty based on the quote_item * (this is a bad design from Magento, checkQuoteItemQty should receive the quoteItem in the original code too) * * @param mixed $qty * @param mixed $summaryQty * @param int $origQty * @param Mage_Sales_Model_Quote_Item $quoteItem * * @return Varien_Object */ public function checkQuoteItemQty($qty, $summaryQty, $origQty = 0, $quoteItem = null, $product = null) { if ($quoteItem && ITwebexperts_Payperrentals_Helper_Data::isReservationOnly($product) && (!$quoteItem->getChildren() || Mage::app()->getStore()->isAdmin())) { $qtyOption = 1; if ($quoteItem->getParentProductQty()) { $qty = $quoteItem->getParentProductQty(); $qtyOption = $quoteItem->getParentProductQtyOption(); } $quoteItemId = $quoteItem->getId(); if ($quoteItem->getParentItem() && $quoteItem->getParentItem()->getProduct()) { if ($quoteItem->getParentItem()->getProduct()->getTypeId() == ITwebexperts_Payperrentals_Helper_Data::PRODUCT_TYPE_BUNDLE) { $quoteItemId = $quoteItem->getParentItem()->getItemId(); } } $turnoverArr = ITwebexperts_Payperrentals_Helper_Data::getTurnoverFromQuoteItemOrBuyRequest($product, $quoteItem); list($startDate, $endDate) = array($turnoverArr['before'], $turnoverArr['after']); $result = new Varien_Object(); $result->setHasError(false); if (!is_numeric($qty)) { $qty = Mage::app()->getLocale()->getNumber($qty); } /** * Check quantity type */ $result->setItemIsQtyDecimal($this->getIsQtyDecimal()); if (!$this->getIsQtyDecimal()) { $result->setHasQtyOptionUpdate(true); $qty = intval($qty); /** * Adding stock data to quote item */ $result->setItemQty($qty); if (!is_numeric($qty)) { $qty = Mage::app()->getLocale()->getNumber($qty); } $origQty = intval($origQty); $result->setOrigQty($origQty); } if ($qty < ITwebexperts_Payperrentals_Helper_Inventory::getMinSaleQuantity($product)) { $result->setHasError(true)->setMessage(Mage::helper('cataloginventory')->__('The minimum quantity allowed for purchase is %s.', ITwebexperts_Payperrentals_Helper_Inventory::getMinSaleQuantity($product)))->setErrorCode('qty_min')->setQuoteMessage(Mage::helper('cataloginventory')->__('Some of the products cannot be ordered in requested quantity.'))->setQuoteMessageIndex('qty'); return $result; } if ($qty > ITwebexperts_Payperrentals_Helper_Inventory::getMaxSaleQuantity($product)) { $result->setHasError(true)->setMessage(Mage::helper('cataloginventory')->__('The maximum quantity allowed for purchase is %s.', ITwebexperts_Payperrentals_Helper_Inventory::getMaxSaleQuantity($product)))->setErrorCode('qty_max')->setQuoteMessage(Mage::helper('cataloginventory')->__('Some of the products cannot be ordered in requested quantity.'))->setQuoteMessageIndex('qty'); return $result; } $result->addData($this->checkQtyIncrements($qty)->getData()); if ($result->getHasError()) { return $result; } $option = $quoteItem->getOptionByCode('info_buyRequest'); $buyRequest = $option ? unserialize($option->getValue()) : null; if (Mage::app()->getStore()->isAdmin() && Mage::getSingleton('adminhtml/session_quote')->getOrderId()) { $editedOrderId = Mage::getSingleton('adminhtml/session_quote')->getOrderId(); $order = Mage::getModel('sales/order')->load($editedOrderId); $buyRequestArray = (array) $buyRequest; foreach ($order->getAllItems() as $oItem) { if ($oItem->getProduct()->getId() != $product->getId()) { continue; } if (is_object($oItem->getOrderItem())) { $item = $oItem->getOrderItem(); } else { $item = $oItem; } if ($item->getParentItem()) { continue; } //check for bundles if (count($item->getChildrenItems()) > 0) { foreach ($item->getChildrenItems() as $child) { $turnoverArr = ITwebexperts_Payperrentals_Helper_Data::getTurnoverFromQuoteItemOrBuyRequest($child->getProductId(), $child); $buyRequestArray['excluded_qty'][] = array('product_id' => $child->getProductId(), 'start_date' => $turnoverArr['before'], 'end_date' => $turnoverArr['after'], 'qty' => $item->getQtyOrdered()); } } else { $turnoverArr = ITwebexperts_Payperrentals_Helper_Data::getTurnoverFromQuoteItemOrBuyRequest($item->getProductId(), $item); $buyRequestArray['excluded_qty'][] = array('product_id' => $item->getProductId(), 'start_date' => $turnoverArr['before'], 'end_date' => $turnoverArr['after'], 'qty' => $item->getQtyOrdered()); } } $buyRequest = new Varien_Object($buyRequestArray); } if (isset($buyRequest['start_date']) && isset($buyRequest['end_date'])) { if (strtotime($buyRequest['start_date']) > strtotime($buyRequest['end_date'])) { $message = Mage::helper('payperrentals')->__('Start Date is bigger then End Date. Please change!'); $result->setHasError(true)->setMessage($message)->setQuoteMessage($message); return $result; } } $isAvailable = false; if (ITwebexperts_Payperrentals_Helper_Inventory::isAllowedOverbook($product->getId())) { $isAvailable = true; } if (ITwebexperts_Payperrentals_Helper_Data::isBuyout($buyRequest)) { if (!$isAvailable) { Mage::register('quote_item_id', $quoteItemId); $maxQty = ITwebexperts_Payperrentals_Helper_Inventory::getQuantity($product, null, null, $buyRequest); Mage::unregister('quote_item_id'); if ($maxQty >= $qty) { $isAvailable = true; } } if ($isAvailable) { return $result; } else { $message = Mage::helper('payperrentals')->__('There is not enough quantity for buy this product'); $result->setHasError(true)->setMessage($message)->setQuoteMessage($message); return $result; } } if (!Mage::app()->getStore()->isAdmin() && !Mage::getSingleton('checkout/session')->getIsExtendedQuote() && isset($buyRequest['start_date']) && isset($buyRequest['end_date']) && ITwebexperts_Payperrentals_Helper_Inventory::isExcludedDay($product->getId(), $buyRequest['start_date'], $buyRequest['end_date'])) { $message = Mage::helper('payperrentals')->__('There are blocked dates or excluded days on your selected dates!'); $result->setHasError(true)->setMessage($message)->setQuoteMessage($message); return $result; } else { if (!$isAvailable && isset($buyRequest['start_date']) && isset($buyRequest['end_date'])) { Mage::register('quote_item_id', $quoteItemId); $maxQty = ITwebexperts_Payperrentals_Helper_Inventory::getQuantity($product, $startDate, $endDate, $buyRequest); Mage::unregister('quote_item_id'); if ($maxQty >= $qty) { $isAvailable = true; } } } if (ITwebexperts_Payperrentals_Helper_Data::isUsingGlobalDatesShoppingCart($product) && !$buyRequest['start_date']) { $message = Mage::helper('payperrentals')->__('Please select Global Dates!'); $result->setHasError(true)->setMessage($message)->setQuoteMessage($message); return $result; } if (!Mage::app()->getStore()->isAdmin() && !ITwebexperts_Payperrentals_Helper_Data::isBuyout($buyRequest) && (isset($buyRequest['start_date']) && strtotime($buyRequest['start_date']) < strtotime(date('Y-m-d')) || isset($buyRequest['end_date']) && strtotime($buyRequest['end_date']) < strtotime(date('Y-m-d')))) { $message = Mage::helper('payperrentals')->__('The selected Dates are in the past. Please select new dates!'); $result->setHasError(true)->setMessage($message)->setQuoteMessage($message); return $result; } if (Mage::app()->getStore()->isAdmin() && ITwebexperts_Payperrentals_Helper_Inventory::isAllowedOverbook($product->getId()) && ITwebexperts_Payperrentals_Helper_Inventory::showAdminOverbookWarning()) { Mage::register('quote_item_id', $quoteItemId); $maxQty = ITwebexperts_Payperrentals_Helper_Inventory::getQuantity($product, $startDate, $endDate, $buyRequest, true); Mage::unregister('quote_item_id'); if ($maxQty < $qty) { $message = Mage::helper('cataloginventory')->__('Product is not available for the selected dates'); $result->setHasError(false)->setMessage($message)->setQuoteMessage($message)->setQuoteMessageIndex('qtyppr'); return $result; } } Mage::dispatchEvent('before_stock_check', array('buyrequest' => $buyRequest, 'isavailable' => &$isAvailable)); if (!$isAvailable) { if (isset($buyRequest['start_date']) && isset($buyRequest['end_date'])) { //Mage::register('no_quote', 1); Mage::register('quote_item_id', $quoteItemId); $maxQty = ITwebexperts_Payperrentals_Helper_Inventory::getQuantity($product, $startDate, $endDate, $buyRequest); Mage::unregister('quote_item_id'); $maxQty = intval($maxQty / $qtyOption); //Mage::unregister('no_quote', 1); if ($maxQty > 0) { $message = Mage::helper('payperrentals')->__('Max quantity available for these dates is: ' . $maxQty . ', your quantity has been adjusted.'); // Mage::getSingleton('checkout/session')->addError($message); $result->setHasQtyOptionUpdate(true); //$result->setOrigQty($maxQty); $result->setQty($maxQty); $result->setItemQty($maxQty); $result->setMessage($message)->setQuoteMessage($message); return $result; } $message = Mage::helper('cataloginventory')->__('The requested quantity for "%s" is not available.', $this->getProductName()); } else { $message = Mage::helper('cataloginventory')->__('Please select Start and End Dates'); } $result->setHasError(true)->setMessage($message)->setQuoteMessage($message)->setQuoteMessageIndex('qtyppr'); return $result; } return $result; } $return = parent::checkQuoteItemQty($qty, $summaryQty, $origQty); return $return; }
/** * Adds all the items to an array including any child items. * No need to check if item is valid. This is called after totals which already does the validation * * @param Array $itemGroup - The array which is passed back by reference * @param Mage_Sales_Model_Quote_Item $item - The item to process */ private static function processItemGroup(&$itemGroup, $item) { if ($item->getHasChildren() && $item->isShipSeparately()) { foreach ($item->getChildren() as $child) { $itemGroup[] = $child; } } else { $itemGroup[] = $item; } }
/** * Add error info to the item and quote the item belongs to with the * provided error code and messages. * * @param Mage_Sales_Model_Quote_Item * @param int * @param string * @param string * @return self */ protected function _addErrorInfoForItem(Mage_Sales_Model_Quote_Item $item, $errorCode, $itemMessage, $quoteMessage) { $item->addErrorInfo(EbayEnterprise_Inventory_Model_Quantity_Service::ERROR_INFO_SOURCE, $errorCode, $itemMessage); foreach ($item->getChildren() as $child) { $child->addErrorInfo(EbayEnterprise_Inventory_Model_Quantity_Service::ERROR_INFO_SOURCE, $errorCode, $itemMessage); } $item->getQuote()->addErrorInfo(EbayEnterprise_Inventory_Model_Quantity_Service::ERROR_INFO_TYPE, EbayEnterprise_Inventory_Model_Quantity_Service::ERROR_INFO_SOURCE, $errorCode, $quoteMessage); return $this; }