Skip to content

Commit

Permalink
[-] Core: fix cache product group reduction #PSCFV-4974
Browse files Browse the repository at this point in the history
  • Loading branch information
rGaillard committed Oct 16, 2012
1 parent 100501c commit a17bf5b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 39 deletions.
2 changes: 1 addition & 1 deletion classes/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ public function delete()
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);
}
}
Expand Down
72 changes: 45 additions & 27 deletions classes/GroupReduction.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,41 +149,57 @@ public static function doesExist($id_group, $id_category)
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)
Expand All @@ -196,23 +212,25 @@ public static function deleteProductReduction($id_product)

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;
}
Expand Down
14 changes: 3 additions & 11 deletions classes/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -467,13 +467,15 @@ public function add($autodate = true, $null_values = false)
else
StockAvailable::setProductOutOfStock((int)$this->id, 2);

$this->setGroupReduction();
Hook::exec('actionProductSave', array('id_product' => $this->id));
return true;
}

public function update($null_values = false)
{
$return = parent::update($null_values);
$this->setGroupReduction();
Hook::exec('actionProductSave', array('id_product' => $this->id));
return $return;
}
Expand Down Expand Up @@ -803,8 +805,6 @@ public function updateCategories($categories, $keeping_current_pos = false)
if (!$this->addToCategories($categories))
return false;

if (!$this->setGroupReduction())
return false;
SpecificPriceRule::applyAllRules(array((int)$this->id));
return true;
}
Expand Down Expand Up @@ -4522,15 +4522,7 @@ public static function resetEcoTax()
*/
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);
}

/**
Expand Down

0 comments on commit a17bf5b

Please sign in to comment.