// add check stock value for color list if needed

This commit is contained in:
Rémi Gaillard
2013-10-11 14:11:12 +02:00
parent 809eac6308
commit 1316bd7e1f
4 changed files with 64 additions and 6 deletions
+11
View File
@@ -268,6 +268,17 @@ class CombinationCore extends ObjectModel
return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query);
}
public function getColorsAttributes()
{
return Db::getInstance()->executeS('
SELECT a.id_attribute
FROM '._DB_PREFIX_.'product_attribute_combination pac
JOIN '._DB_PREFIX_.'attribute a ON (pac.id_attribute = a.id_attribute)
JOIN '._DB_PREFIX_.'attribute_group ag ON (ag.id_attribute_group = a.id_attribute_group)
WHERE pac.id_product_attribute='.(int)$this->id.' AND ag.is_color_group = 1
');
}
/**
* Retrive the price of combination
+30 -4
View File
@@ -3049,17 +3049,21 @@ class ProductCore extends ObjectModel
{
if (!count($products))
return array();
$check_stock = !Configuration::get('PS_DISP_UNAVAILABLE_ATTR');
if (!$res = Db::getInstance()->executeS('
SELECT pa.id_product, a.color, pac.id_product_attribute
SELECT pa.id_product, a.color, pac.id_product_attribute,'.($check_stock ? 'SUM(IF(stock.quantity > 0, 1, 0)' : '').') qty
FROM '._DB_PREFIX_.'product_attribute pa
'.Shop::addSqlAssociation('product_attribute', 'pa').'
'.Shop::addSqlAssociation('product_attribute', 'pa').
($check_stock ? Product::sqlStock('pa', 'pa') : '').'
JOIN '._DB_PREFIX_.'product_attribute_combination pac ON (pac.id_product_attribute = product_attribute_shop.id_product_attribute)
JOIN '._DB_PREFIX_.'attribute a ON (a.id_attribute = pac.id_attribute)
JOIN '._DB_PREFIX_.'attribute_group ag ON (ag.id_attribute_group = ag.id_attribute_group)
WHERE pa.id_product IN ('.implode(array_map('intval', $products), ',').') AND ag.is_color_group = 1
GROUP BY pa.id_product, color
'))
'.($check_stock ? 'HAVING qty > 0' : '')
)
)
return false;
$colors = array();
@@ -5467,4 +5471,26 @@ class ProductCore extends ObjectModel
Pack::addItem($this->id, (int)$item['id'], (int)$item['quantity']);
return true;
}
public function isColorUnavailable($id_attribute, $id_shop)
{
return Db::getInstance()->getValue('
SELECT sa.id_product_attribute
FROM '._DB_PREFIX_.'stock_available sa
WHERE id_product='.(int)$this->id.' AND quantity <= 0
'.StockAvailable::addSqlShopRestriction(null, $id_shop, 'sa').'
AND id_product_attribute IN (
SELECT pa.id_product_attribute
FROM '._DB_PREFIX_.'product_attribute pa
JOIN '._DB_PREFIX_.'product_attribute_shop product_attribute_shop ON (product_attribute_shop.id_product_attribute = pa.id_product_attribute AND product_attribute_shop.id_shop='.(int)$id_shop.')
JOIN '._DB_PREFIX_.'product_attribute_combination pac ON (pac.id_product_attribute AND product_attribute_shop.id_product_attribute)
WHERE pa.id_product='.(int)$this->id.' AND pac.id_attribute='.(int)$id_attribute.'
)'
);
}
public static function getColorsListCacheId($id_product)
{
return 'productlist_colors|'.(int)$id_product.'|'.(int)Context::getContext()->shop->id;
}
}
+1 -1
View File
@@ -1184,6 +1184,6 @@ class FrontControllerCore extends Controller
protected function getColorsListCacheId($id_product)
{
return 'productlist_colors|'.(int)$id_product.'|'.$this->context->shop->id;
return Product::getColorsListCacheId($id_product);
}
}
+22 -1
View File
@@ -379,7 +379,28 @@ class StockAvailableCore extends ObjectModel
if (!$result = parent::update($null_values))
return false;
$result &= $this->postSave();
$result &= $this->postSave();
if (!Configuration::get('PS_DISP_UNAVAILABLE_ATTR'))
{
$combination = new Combination((int)$this->id_product_attribute);
if ($colors = $combination->getColorsAttributes())
{
$product = new Product((int)$this->id_product);
foreach ($colors as $color)
{
if ($product->isColorUnavailable((int)$color['id_attribute'], (int)$this->id_shop))
{
// Change template dir if called from the BackOffice
$current_template_dir = Context::getContext()->smarty->getTemplateDir();
Context::getContext()->smarty->setTemplateDir(_PS_THEME_DIR_.'tpl');
Tools::clearCache(null, 'product-list-colors.tpl', Product::getColorsListCacheId((int)$product->id));
Context::getContext()->smarty->setTemplateDir($current_template_dir);
break;
}
}
}
}
return $result;
}