// id_category_default is now for multishop

This commit is contained in:
vChabot
2012-02-27 13:53:52 +00:00
parent 7e0e9e2adb
commit 77774d0d94
22 changed files with 172 additions and 49 deletions
+7 -2
View File
@@ -376,7 +376,7 @@ class CartCore extends ObjectModel
// Build SELECT
$sql->select('cp.`id_product_attribute`, cp.`id_product`, cp.`quantity` AS cart_quantity, cp.id_shop, pl.`name`, p.`is_virtual`,
pl.`description_short`, pl.`available_now`, pl.`available_later`, p.`id_product`, p.`id_category_default`, p.`id_supplier`,
pl.`description_short`, pl.`available_now`, pl.`available_later`, p.`id_product`, ps.`id_category_default`, p.`id_supplier`,
p.`id_manufacturer`, p.`on_sale`, p.`ecotax`, p.`additional_shipping_cost`, p.`available_for_order`, p.`price`, p.`weight`,
stock.`quantity` quantity_available, p.`width`, p.`height`, p.`depth`, stock.`out_of_stock`, p.`active`, p.`date_add`,
p.`date_upd`, t.`id_tax`, tl.`name` AS tax, t.`rate`, IFNULL(stock.quantity, 0) as quantity, pl.`link_rewrite`, cl.`link_rewrite` AS category,
@@ -406,8 +406,12 @@ class CartCore extends ObjectModel
t.`id_tax` = tl.`id_tax`
AND tl.`id_lang` = '.(int)$this->id_lang
);
$sql->leftJoin('product_shop', 'ps', '
ps.`id_product` = p.`id_product`
AND ps.`id_shop` = '.(int)Context::getContext()->shop->id
);
$sql->leftJoin('category_lang', 'cl', '
p.`id_category_default` = cl.`id_category`
ps.`id_category_default` = cl.`id_category`
AND cl.`id_lang` = '.(int)$this->id_lang.Shop::addSqlRestrictionOnLang('cl')
);
@@ -419,6 +423,7 @@ class CartCore extends ObjectModel
if ($id_product)
$sql->where('cp.`id_product` = '.(int)$id_product);
$sql->where('p.`id_product` IS NOT NULL');
$sql->where('ps.`id_shop` = '.(int)Context::getContext()->shop->id);
// Build GROUP BY
$sql->groupBy('unique_id');
+12 -11
View File
@@ -353,10 +353,10 @@ class CategoryCore extends ObjectModel
}
}
/* Set category default to 1 where categorie no more exists */
/* Set category default to Home category where categorie no more exists */
$result = Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'product`
SET `id_category_default` = 1
UPDATE `'._DB_PREFIX_.'product_shop`
SET `id_category_default` = '.(int)Configuration::get('PS_HOME_CATEGORY').'
WHERE `id_category_default`
NOT IN (SELECT `id_category` FROM `'._DB_PREFIX_.'category`)
');
@@ -638,7 +638,7 @@ class CategoryCore extends ObjectModel
'.Shop::addSqlAssociation('product', 'p').'
'.Product::sqlStock('p', 'pa', false, $context->shop).'
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl
ON (p.`id_category_default` = cl.`id_category`
ON (asso_shop_product.`id_category_default` = cl.`id_category`
AND cl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').')
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
ON (p.`id_product` = pl.`id_product`
@@ -663,7 +663,8 @@ class CategoryCore extends ObjectModel
AND tl.`id_lang` = '.(int)$id_lang.')
LEFT JOIN `'._DB_PREFIX_.'manufacturer` m
ON m.`id_manufacturer` = p.`id_manufacturer`
WHERE cp.`id_category` = '.(int)$this->id
WHERE asso_shop_product.`id_shop` = '.(int)Context::getContext()->shop->id.'
AND cp.`id_category` = '.(int)$this->id
.($active ? ' AND p.`active` = 1' : '')
.($id_supplier ? ' AND p.id_supplier = '.(int)$id_supplier : '');
@@ -696,7 +697,7 @@ class CategoryCore extends ObjectModel
*/
public static function getHomeCategories($id_lang, $active = true)
{
return self::getChildren(1, $id_lang, $active);
return self::getChildren(Configuration::get('PS_HOME_CATEGORY'), $id_lang, $active);
}
public static function getRootCategory($id_lang = null, Shop $shop = null)
@@ -844,7 +845,7 @@ class CategoryCore extends ObjectModel
public static function checkBeforeMove($id_category, $id_parent)
{
if ($id_category == $id_parent) return false;
if ($id_parent == 1) return true;
if ($id_parent == Configuration::get('PS_HOME_CATEGORY')) return true;
$i = (int)$id_parent;
while (42)
@@ -852,7 +853,7 @@ class CategoryCore extends ObjectModel
$result = Db::getInstance()->getRow('SELECT `id_parent` FROM `'._DB_PREFIX_.'category` WHERE `id_category` = '.(int)$i);
if (!isset($result['id_parent'])) return false;
if ($result['id_parent'] == $id_category) return false;
if ($result['id_parent'] == 1) return true;
if ($result['id_parent'] == Configuration::get('PS_HOME_CATEGORY')) return true;
$i = $result['id_parent'];
}
}
@@ -920,8 +921,8 @@ class CategoryCore extends ObjectModel
ON (c.`id_category` = cl.`id_category`
AND `id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').')
WHERE `name` LIKE \'%'.pSQL($query).'%\'
AND c.`id_category` != 1
');
AND c.`id_category` != '.(int)Configuration::get('PS_HOME_CATEGORY')
);
}
/**
@@ -941,7 +942,7 @@ class CategoryCore extends ObjectModel
ON (c.`id_category` = cl.`id_category`
AND `id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').')
WHERE `name` LIKE \''.pSQL($category_name).'\'
AND c.`id_category` != 1
AND c.`id_category` != '.(int)Configuration::get('PS_HOME_CATEGORY').'
AND c.`id_parent` = '.(int)$id_parent_category
);
}
+9 -9
View File
@@ -59,9 +59,9 @@ class GroupReductionCore extends ObjectModel
public function delete()
{
$products = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT p.`id_product`
FROM `'._DB_PREFIX_.'product` p
WHERE p.`id_category_default` = '.(int)$this->id_category
SELECT ps.`id_product`
FROM `'._DB_PREFIX_.'product_shop` ps
WHERE ps.`id_category_default` = '.(int)$this->id_category
);
$ids = array();
@@ -81,9 +81,9 @@ class GroupReductionCore extends ObjectModel
protected function _setCache()
{
$products = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT p.`id_product`
FROM `'._DB_PREFIX_.'product` p
WHERE p.`id_category_default` = '.(int)$this->id_category
SELECT ps.`id_product`
FROM `'._DB_PREFIX_.'product_shop` ps
WHERE ps.`id_category_default` = '.(int)$this->id_category
);
$query = 'INSERT INTO `'._DB_PREFIX_.'product_group_reduction_cache` (`id_product`, `id_group`, `reduction`) VALUES ';
@@ -102,9 +102,9 @@ class GroupReductionCore extends ObjectModel
protected function _updateCache()
{
$products = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT p.`id_product`
FROM `'._DB_PREFIX_.'product` p
WHERE p.`id_category_default` = '.(int)$this->id_category,
SELECT ps.`id_product`
FROM `'._DB_PREFIX_.'product_shop` ps
WHERE ps.`id_category_default` = '.(int)$this->id_category,
false);
$ids = array();
+6 -4
View File
@@ -109,7 +109,7 @@ class PackCore extends Product
if (!Pack::isFeatureActive())
return array();
$sql = 'SELECT p.*, pl.*, i.`id_image`, il.`legend`, t.`rate`, cl.`name` AS category_default, a.quantity AS pack_quantity
$sql = 'SELECT p.*, pl.*, i.`id_image`, il.`legend`, t.`rate`, cl.`name` AS category_default, a.quantity AS pack_quantity, asso_shop_product.`id_category_default`
FROM `'._DB_PREFIX_.'pack` a
LEFT JOIN `'._DB_PREFIX_.'product` p ON p.id_product = a.id_product_item
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
@@ -117,17 +117,19 @@ class PackCore extends Product
AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').'
LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang.')
'.Shop::addSqlAssociation('product', 'p').'
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl
ON p.`id_category_default` = cl.`id_category`
ON asso_shop_product.`id_category_default` = cl.`id_category`
AND cl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').'
LEFT JOIN `'._DB_PREFIX_.'product_tax_rules_group_shop` ptrgs ON (p.`id_product` = ptrgs.`id_product`
AND ptrgs.id_shop='.(int)Context::getContext()->shop->id.')
LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (ptrgs.`id_tax_rules_group` = tr.`id_tax_rules_group`
AND tr.`id_country` = '.(int)Context::getContext()->country->id.'
AND tr.`id_state` = 0)
LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = tr.`id_tax`)
LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = tr.`id_tax`)
LEFT JOIN `'._DB_PREFIX_.'tax_lang` tl ON (t.`id_tax` = tl.`id_tax` AND tl.`id_lang` = '.(int)$id_lang.')
WHERE a.`id_product_pack` = '.(int)$id_product;
WHERE asso_shop_product.`id_shop` = '.(int)Context::getContext()->shop->id.'
AND a.`id_product_pack` = '.(int)$id_product;
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
if (!$full)
return $result;
+51 -5
View File
@@ -422,6 +422,10 @@ class ProductCore extends ObjectModel
$this->loadStockData();
}
// update id_category_default
if ($this->id && !is_null($id_shop))
$this->id_category_default = $this->getDefaultCategory($id_shop);
if ($this->id_category_default)
$this->category = Category::getLinkRewrite((int)$this->id_category_default, (int)$id_lang);
}
@@ -442,6 +446,8 @@ class ProductCore extends ObjectModel
{
if (!parent::add($autodate, $null_values))
return false;
// add id_category_default to product_shop
$this->updateCategoryDefault();
Hook::exec('actionProductSave', array('id_product' => $this->id));
return true;
}
@@ -449,6 +455,8 @@ class ProductCore extends ObjectModel
public function update($null_values = false)
{
$return = parent::update($null_values);
// add id_category_default to product_shop
$this->updateCategoryDefault(Context::getContext()->shop);
Hook::exec('actionProductSave', array('id_product' => $this->id));
return $return;
}
@@ -1941,12 +1949,13 @@ class ProductCore extends ObjectModel
$sql = 'SELECT p.*, stock.`out_of_stock` out_of_stock, pl.`description`, pl.`description_short`,
pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`,
p.`ean13`, p.`upc`, i.`id_image`, il.`legend`, t.`rate`
p.`ean13`, p.`upc`, i.`id_image`, il.`legend`, t.`rate`, asso_shop_product.`id_category_default`
FROM `'._DB_PREFIX_.'product` p
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (
p.`id_product` = pl.`id_product`
AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').'
)
'.Shop::addSqlAssociation('product', 'p').'
LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang.')
LEFT JOIN `'._DB_PREFIX_.'product_tax_rules_group_shop` ptrgs ON (p.`id_product` = ptrgs.`id_product`
@@ -2931,7 +2940,7 @@ class ProductCore extends ObjectModel
AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').'
)
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (
p.`id_category_default` = cl.`id_category`
asso_shop_product.`id_category_default` = cl.`id_category`
AND cl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').'
)
LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
@@ -2940,11 +2949,12 @@ class ProductCore extends ObjectModel
LEFT JOIN `'._DB_PREFIX_.'product_tax_rules_group_shop` ptrgs ON (p.`id_product` = ptrgs.`id_product`
AND ptrgs.id_shop='.(int)$context->shop->id.')
LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (ptrgs.`id_tax_rules_group` = tr.`id_tax_rules_group`
AND tr.`id_country` = '.(int)Context::getContext()->country->id.'
AND tr.`id_country` = '.(int)$context->country->id.'
AND tr.`id_state` = 0)
LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = tr.`id_tax`)
'.Product::sqlStock('p', 0).'
WHERE `id_product_1` = '.(int)$this->id.
WHERE asso_shop_product.`id_shop` = '.(int)$context->shop->id.'
AND `id_product_1` = '.(int)$this->id.
($active ? ' AND p.`active` = 1' : '');
if (!$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql))
return false;
@@ -4035,8 +4045,9 @@ class ProductCore extends ObjectModel
SELECT pl.`id_lang`, pl.`link_rewrite`, p.`ean13`, cl.`link_rewrite` AS category_rewrite
FROM `'._DB_PREFIX_.'product` p
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product`'.Shop::addSqlRestrictionOnLang('pl').')
'.Shop::addSqlAssociation('product', 'p').'
LEFT JOIN `'._DB_PREFIX_.'lang` l ON (pl.`id_lang` = l.`id_lang`)
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (cl.`id_category` = p.`id_category_default` AND cl.`id_lang` = pl.`id_lang`'.Shop::addSqlRestrictionOnLang('cl').')
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (cl.`id_category` = asso_shop_product.`id_category_default` AND cl.`id_lang` = pl.`id_lang`'.Shop::addSqlRestrictionOnLang('cl').')
WHERE p.`id_product` = '.(int)$id_product.'
AND l.`active` = 1
');
@@ -4794,4 +4805,39 @@ class ProductCore extends ObjectModel
$this->out_of_stock = StockAvailable::outOfStock($this->id);
$this->depends_on_stock = StockAvailable::dependsOnStock($this->id);
}
/**
* get the default category according to the shop
*/
public function getDefaultCategory($id_shop = null)
{
$context = Context::getContext();
if (!$id_shop)
$id_shop = Shop::getContext() == Shop::CONTEXT_SHOP ? $context->shop->id : Configuration::get('PS_SHOP_DEFAULT');
return Db::getInstance()->getValue('
SELECT asso_shop_product.`id_category_default`
FROM `'._DB_PREFIX_.'product` p
'.Shop::addSqlAssociation('product', 'p').'
WHERE asso_shop_product.`id_shop` = '.(int)$id_shop);
}
public static function getShopsByProduct($id_product)
{
return Db::getInstance()->executeS('
SELECT `id_shop`
FROM `'._DB_PREFIX_.'product_shop`
WHERE `id_product` = '.(int)$id_product);
}
public function updateCategoryDefault($shop = null)
{
if (is_null($shop))
$shop = new Shop(Configuration::get('PS_SHOP_DEFAULT'));
return Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'product_shop`
SET `id_category_default` = '.(int)$this->id_category_default.'
WHERE `id_shop` = '.(int)$shop->id.'
AND `id_product` = '.(int)$this->id);
}
}
+1 -1
View File
@@ -145,7 +145,7 @@ class ProductSaleCore
LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang.')
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl
ON cl.`id_category` = p.`id_category_default`
ON cl.`id_category` = asso_shop_product.`id_category_default`
AND cl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').'
WHERE p.`active` = 1
AND p.`id_product` IN (
+4 -2
View File
@@ -254,8 +254,9 @@ class SearchCore
p.`id_product` = pl.`id_product`
AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').'
)
'.Shop::addSqlAssociation('product', 'p').'
INNER JOIN `'._DB_PREFIX_.'category_lang` cl ON (
p.`id_category_default` = cl.`id_category`
asso_shop_product.`id_category_default` = cl.`id_category`
AND cl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').'
)
WHERE p.`id_product` '.$product_pool.'
@@ -374,8 +375,9 @@ class SearchCore
FROM '._DB_PREFIX_.'product p
LEFT JOIN '._DB_PREFIX_.'product_lang pl
ON p.id_product = pl.id_product
'.Shop::addSqlAssociation('product', 'p').'
LEFT JOIN '._DB_PREFIX_.'category_lang cl
ON (cl.id_category = p.id_category_default AND pl.id_lang = cl.id_lang)
ON (cl.id_category = asso_shop_product.id_category_default AND pl.id_lang = cl.id_lang)
LEFT JOIN '._DB_PREFIX_.'manufacturer m
ON m.id_manufacturer = p.id_manufacturer
WHERE p.indexed = 0