Esempio n. 1
0
    /**
     * Removes a given product from the stock available
     *
     * @param int $id_product
     * @param int $id_product_attribute Optional
     * @param mixed $id_shop shop id or shop object Optional
     */
    public static function removeProductFromStockAvailable($id_product, $id_product_attribute = null, $shop = null)
    {
        if (!Validate::isUnsignedId($id_product)) {
            return false;
        }
        if (Shop::getContext() == SHOP::CONTEXT_SHOP) {
            if (Shop::getContextShopGroup()->share_stock == 1) {
                $pa_sql = '';
                if ($id_product_attribute !== null) {
                    $pa_sql = '_attribute';
                    $id_product_attribute_sql = $id_product_attribute;
                } else {
                    $id_product_attribute_sql = $id_product;
                }
                if ((int) Db::getInstance()->getValue('SELECT COUNT(*)
						FROM ' . _DB_PREFIX_ . 'product' . $pa_sql . '_shop
						WHERE id_product' . $pa_sql . '=' . (int) $id_product_attribute_sql . ' 
							AND id_shop IN (' . implode(',', array_map('intval', Shop::getContextListShopID(SHOP::SHARE_STOCK))) . ')')) {
                    return true;
                }
            }
        }
        $res = Db::getInstance()->execute('
		DELETE FROM ' . _DB_PREFIX_ . 'stock_available
		WHERE id_product = ' . (int) $id_product . ($id_product_attribute ? ' AND id_product_attribute = ' . (int) $id_product_attribute : '') . StockAvailable::addSqlShopRestriction(null, $shop));
        if ($id_product_attribute) {
            if ($shop === null || !Validate::isLoadedObject($shop)) {
                $shop_datas = array();
                StockAvailable::addSqlShopParams($shop_datas);
                $id_shop = (int) $shop_datas['id_shop'];
            } else {
                $id_shop = (int) $shop->id;
            }
            $stock_available = new StockAvailable();
            $stock_available->id_product = (int) $id_product;
            $stock_available->id_product_attribute = (int) $id_product;
            $stock_available->id_shop = (int) $id_shop;
            $stock_available->postSave();
        }
        return $res;
    }