// Stock Available : bug fix
This commit is contained in:
+5
-53
@@ -334,8 +334,8 @@ class ProductCore extends ObjectModel
|
||||
|
||||
// By default, the product quantity correspond to the available quantity to sell in the current shop
|
||||
$this->quantity = StockAvailable::getQuantityAvailableByProduct($id_product, 0, Context::getContext()->shop->getID());
|
||||
$this->out_of_stock = $this->getOutOfStock();
|
||||
$this->depends_on_stock = $this->getDependsOnStock();
|
||||
$this->out_of_stock = StockAvailable::outOfStock($this->id);
|
||||
$this->depends_on_stock = StockAvailable::dependsOnStock($this->id);
|
||||
|
||||
if ($this->id_category_default)
|
||||
$this->category = Category::getLinkRewrite((int)$this->id_category_default, (int)$id_lang);
|
||||
@@ -944,7 +944,7 @@ class ProductCore extends ObjectModel
|
||||
return false;
|
||||
|
||||
//Try to set available quantitiy if product quantity not depend on stock
|
||||
$depend_on_stock = $this->getDependsOnStock();
|
||||
$depend_on_stock = StockAvailable::dependsOnStock($this->id);
|
||||
|
||||
if (!$depend_on_stock)
|
||||
if (!StockAvailable::updateQuantity($this->id, $id_product_attribute, $quantity))
|
||||
@@ -954,7 +954,7 @@ class ProductCore extends ObjectModel
|
||||
$stock_available->id_product_attribute = (int)$id_product_attribute;
|
||||
$stock_available->id_shop = (int)$context->shop->getID();
|
||||
$stock_available->quantity = (int)$quantity;
|
||||
$stock_available->out_of_stock = $this->getOutOfStock();
|
||||
$stock_available->out_of_stock = StockAvailable::outOfStock($this->id);
|
||||
$stock_available->depends_on_stock = 0;
|
||||
$stock_available->save();
|
||||
}
|
||||
@@ -2314,54 +2314,6 @@ class ProductCore extends ObjectModel
|
||||
return $sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of "out of stock" of current product
|
||||
* "ouf of stock" allow to order product without stock
|
||||
*
|
||||
* @since 1.5.0
|
||||
* @param Context $context
|
||||
* @return int
|
||||
*/
|
||||
public function getOutOfStock(Context $context = null)
|
||||
{
|
||||
if (!$this->id)
|
||||
return 0;
|
||||
if (!$context)
|
||||
$context = Context::getContext();
|
||||
|
||||
$id_shop = $context->shop->getID(true);
|
||||
$sql = 'SELECT out_of_stock
|
||||
FROM '._DB_PREFIX_.'stock_available
|
||||
WHERE id_product = '.$this->id.'
|
||||
AND id_product_attribute = 0'.
|
||||
$context->shop->addSqlRestriction();
|
||||
return (int)Db::getInstance()->getValue($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of "depends on stock" of current product
|
||||
* "depends on stock" allow managing quantity available manually
|
||||
*
|
||||
* @since 1.5.0
|
||||
* @param Context $context
|
||||
* @return int
|
||||
*/
|
||||
public function getDependsOnStock(Context $context = null)
|
||||
{
|
||||
if (!$this->id)
|
||||
return 0;
|
||||
if (!$context)
|
||||
$context = Context::getContext();
|
||||
|
||||
$id_shop = $context->shop->getID(true);
|
||||
$sql = 'SELECT depends_on_stock
|
||||
FROM '._DB_PREFIX_.'stock_available
|
||||
WHERE id_product = '.$this->id.'
|
||||
AND id_product_attribute = 0'.
|
||||
$context->shop->addSqlRestriction();
|
||||
return (int)Db::getInstance()->getValue($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated since 1.5.0
|
||||
*
|
||||
@@ -2458,7 +2410,7 @@ class ProductCore extends ObjectModel
|
||||
if (Pack::isPack((int)$this->id) && !Pack::isInStock((int)$this->id))
|
||||
return false;
|
||||
|
||||
if ($this->isAvailableWhenOutOfStock($this->getOutOfStock()))
|
||||
if ($this->isAvailableWhenOutOfStock(StockAvailable::outOfStock($this->id)))
|
||||
return true;
|
||||
|
||||
if (isset($this->id_product_attribute))
|
||||
|
||||
@@ -191,6 +191,31 @@ class StockAvailableCore extends ObjectModel
|
||||
'id_product = '.(int)$id_product.' AND id_shop = '.(int)$id_shop
|
||||
);
|
||||
|
||||
$existing_id = self::getStockAvailableIdByProductId((int)$id_product, 0, (int)$id_shop);
|
||||
|
||||
if ($existing_id > 0)
|
||||
{
|
||||
Db::getInstance()->autoExecute(
|
||||
_DB_PREFIX_.'stock_available',
|
||||
array('depends_on_stock' => (bool)$depends_on_stock),
|
||||
'UPDATE',
|
||||
'id_product = '.(int)$id_product.' AND id_product_attribute = 0 AND id_shop = '.(int)$id_shop
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
Db::getInstance()->autoExecute(
|
||||
_DB_PREFIX_.'stock_available',
|
||||
array(
|
||||
'depends_on_stock' => (bool)$depends_on_stock,
|
||||
'id_product' => (int)$id_product,
|
||||
'id_product_attribute' => 0,
|
||||
'id_shop' => (int)$id_shop
|
||||
),
|
||||
'INSERT'
|
||||
);
|
||||
}
|
||||
|
||||
// depends on stock.. hence synchronizes
|
||||
if ($depends_on_stock)
|
||||
StockAvailable::synchronize($id_product);
|
||||
@@ -354,6 +379,29 @@ class StockAvailableCore extends ObjectModel
|
||||
$query->select('depends_on_stock');
|
||||
$query->from('stock_available');
|
||||
$query->where('id_product = '.(int)$id_product);
|
||||
$query->where('id_product_attribute = 0');
|
||||
$query->where('id_shop = '.(int)$id_shop);
|
||||
|
||||
return (bool)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* For a given product, get its "out of stock" flag
|
||||
*
|
||||
* @param int $id_product
|
||||
* @param int $id_shop Optional : gets context if null @see Context::getContext()
|
||||
* @return bool : depends on stock @see $depends_on_stock
|
||||
*/
|
||||
public function outOfStock($id_product, $id_shop = null)
|
||||
{
|
||||
if (is_null($id_shop))
|
||||
$id_shop = Context::getContext()->shop->getID(true);
|
||||
|
||||
$query = new DbQuery();
|
||||
$query->select('out_of_stock');
|
||||
$query->from('stock_available');
|
||||
$query->where('id_product = '.(int)$id_product);
|
||||
$query->where('id_product_attribute = 0');
|
||||
$query->where('id_shop = '.(int)$id_shop);
|
||||
|
||||
return (bool)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query);
|
||||
|
||||
Reference in New Issue
Block a user