/** * 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) { /** @var Mage_Catalog_Model_Product $product */ $product = $item->getProduct(); /** @var Mage_CatalogInventory_Model_Stock_Item $stockItem */ $stockItem = $product->getStockItem(); $isBuyable = true; if ($product->isConfigurable()) { $parent = $product; $product = $product->getCustomOption('simple_product')->getProduct(); $product->setShopgateItemNumber($parent->getShopgateItemNumber()); $product->setShopgateOptions($parent->getShopgateOptions()); $product->setShopgateInputs($parent->getShopgateInputs()); $product->setShhopgateAttributes($parent->getShhopgateAttributes()); $stockItem = $item->getProduct()->getCustomOption('simple_product')->getProduct()->getStockItem(); } $errors = array(); if (Mage::helper('shopgate/config')->getIsMagentoVersionLower1410()) { $checkIncrements = Mage::helper('shopgate')->checkQtyIncrements($stockItem, $item->getQty()); } else { $checkIncrements = $stockItem->checkQtyIncrements($item->getQty()); } if ($stockItem->getManageStock() && !$product->isSaleable() && (!$stockItem->getBackorders() || !$stockItem->getIsInStock())) { $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 ($stockItem->getManageStock() && !$stockItem->checkQty($item->getQty()) && !$stockItem->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; } else { if ($stockItem->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 = $isBuyable ? (int) $item->getQty() : (int) $stockItem->getQty(); return Mage::helper('shopgate')->generateShopgateCartItem($product, $isBuyable, $qtyBuyable, $priceInclTax, $priceExclTax, $errors, (int) $stockItem->getQty()); }
/** * 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()); }
/** * Check Quote item qty. If qty is not enougth for order, error flag and message added to $quote item * * @param Mage_Sales_Model_Quote_Item $quoteItem */ protected function checkQuoteItemQty($quoteItem) { $qty = $quoteItem->getQty(); if (($options = $quoteItem->getQtyOptions()) && $qty > 0) { $qty = $quoteItem->getProduct()->getTypeInstance(true)->prepareQuoteItemQty($qty, $quoteItem->getProduct()); $quoteItem->setData('qty', $qty); foreach ($options as $option) { $optionQty = $qty * $option->getValue(); $increaseOptionQty = ($quoteItem->getQtyToAdd() ? $quoteItem->getQtyToAdd() : $qty) * $option->getValue(); $stockItem = $option->getProduct()->getStockItem(); /* @var $stockItem Mage_CatalogInventory_Model_Stock_Item */ if (!$stockItem instanceof Mage_CatalogInventory_Model_Stock_Item) { $quoteItem->setHasError(true)->setMessage('Stock item for Product in option is not valid'); return; } $result = $stockItem->checkQuoteItemQty($optionQty, $optionQty, $option->getValue()); if ($result->getHasError()) { $quoteItem->setHasError(true)->setMessage($result->getQuoteMessage()); } } } else { $stockItem = $quoteItem->getProduct()->getStockItem(); /* @var $stockItem Mage_CatalogInventory_Model_Stock_Item */ if (!$stockItem instanceof Mage_CatalogInventory_Model_Stock_Item) { Mage::throwException(Mage::helper('cataloginventory')->__('Stock item for Product is not valid')); } /** * When we work with subitem (as subproduct of bundle or configurable product) */ if ($quoteItem->getParentItem()) { $rowQty = $quoteItem->getParentItem()->getQty() * $qty; /** * we are using 0 because original qty was processed */ $qtyForCheck = 0; } else { $increaseQty = $quoteItem->getQtyToAdd() ? $quoteItem->getQtyToAdd() : $qty; $rowQty = $qty; $qtyForCheck = $qty; } $result = $stockItem->checkQuoteItemQty($rowQty, $qtyForCheck, $qty); if ($result->getHasError()) { $quoteItem->setHasError(true)->setMessage($result->getQuoteMessage()); } } }