diff --git a/classes/Combination.php b/classes/Combination.php index 461e6c78c..06e3ac6cc 100644 --- a/classes/Combination.php +++ b/classes/Combination.php @@ -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 diff --git a/classes/Product.php b/classes/Product.php index ffd025451..97981505c 100644 --- a/classes/Product.php +++ b/classes/Product.php @@ -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; + } } diff --git a/classes/controller/FrontController.php b/classes/controller/FrontController.php index 57e4b5f06..d8efa40ba 100644 --- a/classes/controller/FrontController.php +++ b/classes/controller/FrontController.php @@ -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); } } diff --git a/classes/stock/StockAvailable.php b/classes/stock/StockAvailable.php index dbe95389f..85fd0bd79 100644 --- a/classes/stock/StockAvailable.php +++ b/classes/stock/StockAvailable.php @@ -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; }