public function getStockAvailableByProduct($product, $id_product_attribute = null, $id_shop = null) { return new StockAvailable(StockAvailable::getStockAvailableIdByProductId($product->id, $id_product_attribute, $id_shop)); }
/** * For a given id_product and id_product_attribute sets the quantity available * * @param int $id_product * @param int $id_product_attribute Optional * @param int $delta_quantity The delta quantity to update * @param int $id_shop Optional */ public static function setQuantity($id_product, $id_product_attribute, $quantity, $id_shop = null) { if (!Validate::isUnsignedId($id_product)) { return false; } $context = Context::getContext(); // if there is no $id_shop, gets the context one if ($id_shop === null && Shop::getContext() != Shop::CONTEXT_GROUP) { $id_shop = (int) $context->shop->id; } $depends_on_stock = StockAvailable::dependsOnStock($id_product); //Try to set available quantity if product does not depend on physical stock if (!$depends_on_stock) { $id_stock_available = (int) StockAvailable::getStockAvailableIdByProductId($id_product, $id_product_attribute, $id_shop); if ($id_stock_available) { $stock_available = new StockAvailable($id_stock_available); $stock_available->quantity = (int) $quantity; $stock_available->update(); } else { $out_of_stock = StockAvailable::outOfStock($id_product, $id_shop); $stock_available = new StockAvailable(); $stock_available->out_of_stock = (int) $out_of_stock; $stock_available->id_product = (int) $id_product; $stock_available->id_product_attribute = (int) $id_product_attribute; $stock_available->quantity = (int) $quantity; $shop_group = new ShopGroup((int) Shop::getContextShopGroupID()); // if quantities are shared between shops of the group if ($shop_group->share_stock) { $stock_available->id_shop = 0; $stock_available->id_shop_group = (int) $shop_group->id; } else { $stock_available->id_shop = $id_shop; $stock_available->id_shop_group = Shop::getGroupFromShop($id_shop); } $stock_available->add(); } Hook::exec('actionUpdateQuantity', array('id_product' => $id_product, 'id_product_attribute' => $id_product_attribute, 'quantity' => $stock_available->quantity)); } }
public static function setQuantity($id_product, $id_product_attribute, $quantity, $id_shop = null) { if (!Validate::isUnsignedId($id_product)) { return false; } $context = Context::getContext(); if ($id_shop === null && Shop::getContext() != Shop::CONTEXT_GROUP) { $id_shop = (int) $context->shop->id; } $depends_on_stock = StockAvailable::dependsOnStock($id_product); if (!$depends_on_stock) { $id_stock_available = (int) StockAvailable::getStockAvailableIdByProductId($id_product, $id_product_attribute, $id_shop); if ($id_stock_available) { $stock_available = new StockAvailable($id_stock_available); PP::setQty($stock_available, $quantity); $stock_available->update(); } else { $out_of_stock = StockAvailable::outOfStock($id_product, $id_shop); $stock_available = new StockAvailable(); $stock_available->out_of_stock = (int) $out_of_stock; $stock_available->id_product = (int) $id_product; $stock_available->id_product_attribute = (int) $id_product_attribute; PP::setQty($stock_available, $quantity); if ($id_shop === null) { $shop_group = Shop::getContextShopGroup(); } else { $shop_group = new ShopGroup((int) Shop::getGroupFromShop((int) $id_shop)); } if ($shop_group->share_stock) { $stock_available->id_shop = 0; $stock_available->id_shop_group = (int) $shop_group->id; } else { $stock_available->id_shop = (int) $id_shop; $stock_available->id_shop_group = 0; } $stock_available->add(); } Hook::exec('actionUpdateQuantity', array('id_product' => $id_product, 'id_product_attribute' => $id_product_attribute, 'quantity' => $stock_available->quantity + $stock_available->quantity_remainder)); } Cache::clean('StockAvailable::getQuantityAvailableByProduct_' . (int) $id_product . '*'); }