Commit bc3aa991 authored by Rémi Gaillard's avatar Rémi Gaillard

[-] Core: fix cache product group reduction #PSCFV-4974

parent 6feea507
......@@ -332,7 +332,7 @@ class CategoryCore extends ObjectModel
CartRule::cleanProductRuleIntegrity('categories', array($cat->id));
Category::cleanPositions($cat->id_parent);
/* Delete Categories in GroupReduction */
if (GroupReduction::getGroupReductionByCategoryId((int)$cat->id))
if (GroupReduction::getGroupsReductionByCategoryId((int)$cat->id))
GroupReduction::deleteCategory($cat->id);
}
}
......
......@@ -149,41 +149,57 @@ class GroupReductionCore extends ObjectModel
WHERE `id_group` = '.(int)$id_group.' AND `id_category` = '.(int)$id_category);
}
public static function getGroupsByCategoryId($id_category)
{
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT gr.`id_group` as id_group, gr.`reduction` as reduction, id_group_reduction
FROM `'._DB_PREFIX_.'group_reduction` gr
WHERE `id_category` = '.(int)$id_category
);
}
public static function getGroupByCategoryId($id_category)
{
Tools::displayAsDeprecated('Use GroupReduction::getGroupsByCategoryId($id_category)');
return Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
SELECT gr.`id_group` as id_group, gr.`reduction` as reduction
SELECT gr.`id_group` as id_group, gr.`reduction` as reduction, id_group_reduction
FROM `'._DB_PREFIX_.'group_reduction` gr
WHERE `id_category` = '.(int)$id_category,
false);
WHERE `id_category` = '.(int)$id_category
, false);
}
public static function getGroupsReductionByCategoryId($id_category)
{
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT gr.`id_group_reduction` as id_group_reduction, id_group
FROM `'._DB_PREFIX_.'group_reduction` gr
WHERE `id_category` = '.(int)$id_category
);
}
public static function getGroupReductionByCategoryId($id_category)
{
Tools::displayAsDeprecated('Use GroupReduction::getGroupsByCategoryId($id_category)');
return Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
SELECT gr.`id_group_reduction` as id_group_reduction
FROM `'._DB_PREFIX_.'group_reduction` gr
WHERE `id_category` = '.(int)$id_category,
false);
WHERE `id_category` = '.(int)$id_category
, false);
}
public static function setProductReduction($id_product, $id_group, $id_category, $reduction)
public static function setProductReduction($id_product, $id_group = null, $id_category, $reduction = null)
{
Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
SELECT pgr.`id_product`, pgr.`id_group`, pgr.`reduction`
FROM `'._DB_PREFIX_.'product_group_reduction_cache` pgr
WHERE pgr.`id_product` = '.(int)$id_product
);
if (Db::getInstance()->NumRows() == 0)
$query = 'INSERT INTO `'._DB_PREFIX_.'product_group_reduction_cache` (`id_product`, `id_group`, `reduction`)
VALUES ('.(int)$id_product.', '.(int)$id_group.', '.(float)$reduction.')';
else
$query = 'UPDATE `'._DB_PREFIX_.'product_group_reduction_cache`
SET `reduction` = '.(float)$reduction.'
WHERE `id_product` = '.(int)$id_product.' AND `id_group` = '.(int)$id_group;
GroupReduction::deleteProductReduction((int)$id_product);
$reductions = GroupReduction::getGroupsByCategoryId((int)$id_category);
if ($reductions)
{
$res = true;
foreach ($reductions as $reduction)
$res &= Db::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'product_group_reduction_cache` (`id_product`, `id_group`, `reduction`)
VALUES ('.(int)$id_product.', '.(int)$reduction['id_group'].', '.(float)$reduction['reduction'].')');
}
return (Db::getInstance()->execute($query));
return $res;
}
public static function deleteProductReduction($id_product)
......@@ -196,23 +212,25 @@ class GroupReductionCore extends ObjectModel
public static function duplicateReduction($id_product_old, $id_product)
{
$row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
$res = Db::getInstance(_PS_USE_SQL_SLAVE_)->executes('
SELECT pgr.`id_product`, pgr.`id_group`, pgr.`reduction`
FROM `'._DB_PREFIX_.'product_group_reduction_cache` pgr
WHERE pgr.`id_product` = '.(int)$id_product_old
);
if (!$row)
if (!$res)
return true;
$query = 'INSERT INTO `'._DB_PREFIX_.'product_group_reduction_cache` (`id_product`, `id_group`, `reduction`) VALUES ';
$query .= '('.(int)$id_product.', '.(int)$row['id_group'].', '.(float)$row['reduction'].')';
return (Db::getInstance()->execute($query));
foreach ($res as $row)
{
$query = 'INSERT INTO `'._DB_PREFIX_.'product_group_reduction_cache` (`id_product`, `id_group`, `reduction`) VALUES ';
$query .= '('.(int)$id_product.', '.(int)$row['id_group'].', '.(float)$row['reduction'].')';
}
return Db::getInstance()->execute($query);
}
public static function deleteCategory($id_category)
{
$query = 'DELETE FROM `'._DB_PREFIX_.'group_reduction` WHERE `id_category` = '.(int)$id_category;
if (Db::getInstance()->execute($query) === false)
if (Db::getInstance()->Execute($query) === false)
return false;
return true;
}
......
......@@ -467,6 +467,7 @@ class ProductCore extends ObjectModel
else
StockAvailable::setProductOutOfStock((int)$this->id, 2);
$this->setGroupReduction();
Hook::exec('actionProductSave', array('id_product' => $this->id));
return true;
}
......@@ -474,6 +475,7 @@ class ProductCore extends ObjectModel
public function update($null_values = false)
{
$return = parent::update($null_values);
$this->setGroupReduction();
Hook::exec('actionProductSave', array('id_product' => $this->id));
return $return;
}
......@@ -803,8 +805,6 @@ class ProductCore extends ObjectModel
if (!$this->addToCategories($categories))
return false;
if (!$this->setGroupReduction())
return false;
SpecificPriceRule::applyAllRules(array((int)$this->id));
return true;
}
......@@ -4522,15 +4522,7 @@ class ProductCore extends ObjectModel
*/
public function setGroupReduction()
{
$row = GroupReduction::getGroupByCategoryId($this->id_category_default);
if (!$row) // Remove
{
if (!GroupReduction::deleteProductReduction($this->id))
return false;
}
else if (!GroupReduction::setProductReduction($this->id, $row['id_group'], $this->id_category_default, (float)$row['reduction']))
return false;
return true;
return GroupReduction::setProductReduction($this->id, null, $this->id_category_default);
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment