/** * Set the available quantity for a given item. * * @param Mage_CatalogInventory_Model_Stock_Item * @param int $quantity the amount to set * @return bool true if the quantity has changed for this product */ protected function updateProductQuantity(Mage_CatalogInventory_Model_Stock_Item $stockItem, $quantity) { $oldQty = $stockItem->getQty(); $change = $oldQty !== $quantity; if ($change) { $stockItem->setQty($quantity); } return $change; }
/** * @param Mage_CatalogInventory_Model_Stock_Item $stockItem the stock item that needs to be constructed * @param $productSku a string representing the SKU for product * @return array */ protected function _createStockItem($stockItem, $productSku) { $shippingOrigin = Mage::getStoreConfig('shipping/origin'); $locationName = ''; if (isset($shippingOrigin['country_id'])) { $locationName .= $shippingOrigin['country_id']; } $locationName .= ":"; if (isset($shippingOrigin['postcode'])) { $locationName .= $shippingOrigin['postcode']; } $locationName .= ":"; if (isset($shippingOrigin['region_id'])) { $locationName .= $shippingOrigin['region_id']; } $data = array('sku' => $productSku, 'quantity' => (int) $stockItem->getQty(), 'locationName' => $locationName); return $data; }
/** * Prepare stock item data for save * * @param Mage_CatalogInventory_Model_Stock_Item $item * @param Mage_Catalog_Model_Product $product * @return Mage_CatalogInventory_Model_Observer */ protected function _prepareItemForSave($item, $product) { $item->addData($product->getStockData())->setProduct($product)->setProductId($product->getId())->setStockId($item->getStockId()); if (!is_null($product->getData('stock_data/min_qty')) && is_null($product->getData('stock_data/use_config_min_qty'))) { $item->setData('use_config_min_qty', false); } if (!is_null($product->getData('stock_data/min_sale_qty')) && is_null($product->getData('stock_data/use_config_min_sale_qty'))) { $item->setData('use_config_min_sale_qty', false); } if (!is_null($product->getData('stock_data/max_sale_qty')) && is_null($product->getData('stock_data/use_config_max_sale_qty'))) { $item->setData('use_config_max_sale_qty', false); } if (!is_null($product->getData('stock_data/backorders')) && is_null($product->getData('stock_data/use_config_backorders'))) { $item->setData('use_config_backorders', false); } if (!is_null($product->getData('stock_data/notify_stock_qty')) && is_null($product->getData('stock_data/use_config_notify_stock_qty'))) { $item->setData('use_config_notify_stock_qty', false); } $originalQty = $product->getData('stock_data/original_inventory_qty'); if (strlen($originalQty) > 0) { $item->setQtyCorrection($item->getQty() - $originalQty); } if (!is_null($product->getData('stock_data/enable_qty_increments')) && is_null($product->getData('stock_data/use_config_enable_qty_inc'))) { $item->setData('use_config_enable_qty_inc', false); } if (!is_null($product->getData('stock_data/qty_increments')) && is_null($product->getData('stock_data/use_config_qty_increments'))) { $item->setData('use_config_qty_increments', false); } return $this; }
/** * Change Stock Item status process * * @param Mage_CatalogInventory_Model_Stock_Item $item * @return Mage_CatalogInventory_Model_Stock_Status */ public function changeItemStatus(Mage_CatalogInventory_Model_Stock_Item $item) { $productId = $item->getProductId(); if (!($productType = $item->getProductTypeId())) { $productType = $this->getProductType($productId); } $status = (int) $item->getIsInStock(); $qty = (int) $item->getQty(); $this->_processChildren($productId, $productType, $qty, $status, $item->getStockId()); $this->_processParents($productId, $item->getStockId()); return $this; }
/** * Check if the given quote item is allocatable. * * @param Mage_CatalogInventory_Model_Stock_Item * @return bool */ protected function isAllocatable(Mage_CatalogInventory_Model_Stock_Item $stockItem) { return (int) $stockItem->getBackorders() === Mage_CatalogInventory_Model_Stock::BACKORDERS_NO || (int) $stockItem->getQty(); }
/** * Test if stock item quantity properly saved after import * * @magentoDataFixture Mage/Catalog/_files/multiple_products.php */ public function testSaveStockItemQty() { $existingProductIds = array(10, 11, 12); $stockItems = array(); foreach ($existingProductIds as $productId) { $stockItem = new Mage_CatalogInventory_Model_Stock_Item(); $stockItem->loadByProduct($productId); $stockItems[$productId] = $stockItem; } $source = new Mage_ImportExport_Model_Import_Adapter_Csv(__DIR__ . '/_files/products_to_import.csv'); $this->_model->setParameters(array('behavior' => Mage_ImportExport_Model_Import::BEHAVIOR_REPLACE, 'entity' => 'catalog_product'))->setSource($source)->isDataValid(); $this->_model->importData(); /** @var $stockItmBeforeImport Mage_CatalogInventory_Model_Stock_Item */ foreach ($stockItems as $productId => $stockItmBeforeImport) { /** @var $stockItemAfterImport Mage_CatalogInventory_Model_Stock_Item */ $stockItemAfterImport = new Mage_CatalogInventory_Model_Stock_Item(); $stockItemAfterImport->loadByProduct($productId); $this->assertEquals($stockItmBeforeImport->getQty(), $stockItemAfterImport->getQty()); unset($stockItemAfterImport); } unset($stockItems, $stockItem); }
/** * Set inventory data to custom attribute * * @param Varien_Object $object * @return Mage_Eav_Model_Entity_Attribute_Backend_Abstract */ public function afterLoad($object) { $this->_inventory->loadByProduct($object); $object->setData($this->getAttribute()->getAttributeCode(), array('is_in_stock' => $this->_inventory->getIsInStock(), 'qty' => $this->_inventory->getQty())); return parent::afterLoad($object); }
public function insertStockHistory(Mage_CatalogInventory_Model_Stock_Item $stockItem, $message = '') { Mage::getModel('jr_stockhistory/stock_history')->setItemId($stockItem->getId())->setUser($this->_getUsername())->setUserId($this->_getUserId())->setQty($stockItem->getQty())->setIsInStock((int) $stockItem->getIsInStock())->setMessage($message)->save(); Mage::getModel('catalog/product')->load($stockItem->getProductId())->cleanCache(); }
public function insertStockMovement(Mage_CatalogInventory_Model_Stock_Item $stockItem, $message = '') { if ($stockItem->getId()) { Mage::getModel('bubble_stockmovements/stock_movement')->setItemId($stockItem->getId())->setUser($this->_getUsername())->setUserId($this->_getUserId())->setIsAdmin((int) Mage::getSingleton('admin/session')->isLoggedIn())->setQty($stockItem->getQty())->setIsInStock((int) $stockItem->getIsInStock())->setMessage($message)->save(); Mage::getModel('catalog/product')->load($stockItem->getProductId())->cleanCache(); } }
/** * Creates a new StockMovement object and commits to database. * * @param Mage_CatalogInventory_Model_Stock_Item $stockItem * @param string $message * @param null $origQty */ public function insertStockMovement($stockItem, $message = '', $origQty = null) { if ($stockItem->getId()) { $origQty = $origQty !== null ? $origQty : $stockItem->getOriginalInventoryQty(); // Do not create entry if the quantity hasn't changed if ($origQty == $stockItem->getQty()) { return; } Mage::getModel('bubble_stockmovements/stock_movement')->setItemId($stockItem->getId())->setUser($this->_getUsername())->setUserId($this->_getUserId())->setIsAdmin((int) Mage::getSingleton('admin/session')->isLoggedIn())->setQty($stockItem->getQty())->setOriginalQty($origQty !== null ? $origQty : $stockItem->getOriginalInventoryQty())->setIsInStock((int) $stockItem->getIsInStock())->setMessage($message)->save(); } }
/** * Return true when the quantity of inventory stock item is greater than zero * otherwise return false. * * @param Mage_CatalogInventory_Model_Stock_Item * @return bool */ protected function isAllowedInventoryDetail(Mage_CatalogInventory_Model_Stock_Item $stock) { return $stock->getQty() > 0; }