[*] FO ProductSale: improves calculation of most selled products when there are a lot of products in shop
This commit is contained in:
1 parent
bc38b542fe
commit
5fd3ad5adf
1 file changed
+43
-18
+43
-18
@@ -75,10 +75,21 @@ class ProductSaleCore
|
|||||||
$sql_groups = (count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1');
|
$sql_groups = (count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1');
|
||||||
$interval = Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20;
|
$interval = Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20;
|
||||||
|
|
||||||
$prefix = '';
|
//Subquery: get product ids in a separate query to (greatly!) improve performances and RAM usage
|
||||||
if ($order_by == 'date_add')
|
$sql = 'SELECT cp.`id_product`
|
||||||
$prefix = 'p.';
|
FROM `'._DB_PREFIX_.'category_group` cg
|
||||||
|
LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_category` = cg.`id_category`)
|
||||||
|
WHERE cg.`id_group` '.$sql_groups;
|
||||||
|
$products = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
|
||||||
|
$ids = array();
|
||||||
|
foreach ($products as $product)
|
||||||
|
$ids[$product['id_product']] = 1;
|
||||||
|
$ids = array_keys($ids);
|
||||||
|
sort($ids);
|
||||||
|
$ids = count($ids) > 0 ? implode(',', $ids) : 'NULL';
|
||||||
|
|
||||||
|
//Main query
|
||||||
|
if ($order_by == 'date_add')
|
||||||
$sql = 'SELECT p.*, product_shop.*, stock.out_of_stock, IFNULL(stock.quantity, 0) as quantity,
|
$sql = 'SELECT p.*, product_shop.*, stock.out_of_stock, IFNULL(stock.quantity, 0) as quantity,
|
||||||
pl.`description`, pl.`description_short`, pl.`link_rewrite`, pl.`meta_description`,
|
pl.`description`, pl.`description_short`, pl.`link_rewrite`, pl.`meta_description`,
|
||||||
pl.`meta_keywords`, pl.`meta_title`, pl.`name`,
|
pl.`meta_keywords`, pl.`meta_title`, pl.`name`,
|
||||||
@@ -103,13 +114,8 @@ class ProductSaleCore
|
|||||||
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`)
|
||||||
'.Product::sqlStock('p').'
|
'.Product::sqlStock('p').'
|
||||||
WHERE product_shop.`active` = 1
|
WHERE product_shop.`active` = 1
|
||||||
AND product_shop.`visibility` != \'none\'
|
AND p.`visibility` != \'none\'
|
||||||
AND p.`id_product` IN (
|
AND p.`id_product` IN ('.$ids.')
|
||||||
SELECT cp.`id_product`
|
|
||||||
FROM `'._DB_PREFIX_.'category_group` cg
|
|
||||||
LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_category` = cg.`id_category`)
|
|
||||||
WHERE cg.`id_group` '.$sql_groups.'
|
|
||||||
)
|
|
||||||
GROUP BY product_shop.id_product
|
GROUP BY product_shop.id_product
|
||||||
ORDER BY '.(!empty($order_table) ? '`'.pSQL($order_table).'`.' : '').'`'.pSQL($order_by).'` '.pSQL($order_way).'
|
ORDER BY '.(!empty($order_table) ? '`'.pSQL($order_table).'`.' : '').'`'.pSQL($order_by).'` '.pSQL($order_way).'
|
||||||
LIMIT '.(int)($page_number * $nb_products).', '.(int)$nb_products;
|
LIMIT '.(int)($page_number * $nb_products).', '.(int)$nb_products;
|
||||||
@@ -141,11 +147,27 @@ class ProductSaleCore
|
|||||||
$groups = FrontController::getCurrentCustomerGroups();
|
$groups = FrontController::getCurrentCustomerGroups();
|
||||||
$sql_groups = (count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1');
|
$sql_groups = (count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1');
|
||||||
|
|
||||||
|
//Subquery: get product ids in a separate query to (greatly!) improve performances and RAM usage
|
||||||
|
$sql = 'SELECT cp.`id_product`
|
||||||
|
FROM `'._DB_PREFIX_.'category_group` cg
|
||||||
|
LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_category` = cg.`id_category`)
|
||||||
|
WHERE cg.`id_group` '.$sql_groups.' AND cp.`id_product` IS NOT NULL';
|
||||||
|
$products = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
|
||||||
|
$ids = array();
|
||||||
|
foreach ($products as $product)
|
||||||
|
$ids[$product['id_product']] = 1;
|
||||||
|
$ids = array_keys($ids);
|
||||||
|
sort($ids);
|
||||||
|
$ids = count($ids) > 0 ? implode(',', $ids) : 'NULL';
|
||||||
|
|
||||||
|
//Main query
|
||||||
$sql = 'SELECT p.id_product, pl.`link_rewrite`, pl.`name`, pl.`description_short`, MAX(image_shop.`id_image`) id_image, il.`legend`,
|
$sql = 'SELECT p.id_product, pl.`link_rewrite`, pl.`name`, pl.`description_short`, MAX(image_shop.`id_image`) id_image, il.`legend`,
|
||||||
ps.`quantity` AS sales, p.`ean13`, p.`upc`, cl.`link_rewrite` AS category
|
ps.`quantity` AS sales, p.`ean13`, p.`upc`, cl.`link_rewrite` AS category, p.show_price, p.available_for_order, p.quantity, p.customizable,
|
||||||
|
IFNULL(pa.minimal_quantity, p.minimal_quantity) as minimal_quantity, p.out_of_stock
|
||||||
FROM `'._DB_PREFIX_.'product_sale` ps
|
FROM `'._DB_PREFIX_.'product_sale` ps
|
||||||
LEFT JOIN `'._DB_PREFIX_.'product` p ON ps.`id_product` = p.`id_product`
|
LEFT JOIN `'._DB_PREFIX_.'product` p ON ps.`id_product` = p.`id_product`
|
||||||
'.Shop::addSqlAssociation('product', 'p').'
|
'.Shop::addSqlAssociation('product', 'p').'
|
||||||
|
LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa ON (ps.`id_product` = pa.`id_product` AND pa.default_on = 1)
|
||||||
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
|
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
|
||||||
ON p.`id_product` = pl.`id_product`
|
ON p.`id_product` = pl.`id_product`
|
||||||
AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').'
|
AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').'
|
||||||
@@ -156,16 +178,12 @@ class ProductSaleCore
|
|||||||
ON cl.`id_category` = product_shop.`id_category_default`
|
ON cl.`id_category` = product_shop.`id_category_default`
|
||||||
AND cl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').'
|
AND cl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').'
|
||||||
WHERE product_shop.`active` = 1
|
WHERE product_shop.`active` = 1
|
||||||
AND product_shop.`visibility` != \'none\'
|
AND p.`visibility` != \'none\'
|
||||||
AND p.`id_product` IN (
|
AND p.`id_product` IN ('.$ids.')
|
||||||
SELECT cp.`id_product`
|
|
||||||
FROM `'._DB_PREFIX_.'category_group` cg
|
|
||||||
LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_category` = cg.`id_category`)
|
|
||||||
WHERE cg.`id_group` '.$sql_groups.'
|
|
||||||
)
|
|
||||||
GROUP BY product_shop.id_product
|
GROUP BY product_shop.id_product
|
||||||
ORDER BY sales DESC
|
ORDER BY sales DESC
|
||||||
LIMIT '.(int)($page_number * $nb_products).', '.(int)$nb_products;
|
LIMIT '.(int)($page_number * $nb_products).', '.(int)$nb_products;
|
||||||
|
|
||||||
if (!$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql))
|
if (!$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@@ -173,6 +191,13 @@ class ProductSaleCore
|
|||||||
{
|
{
|
||||||
$row['link'] = $context->link->getProductLink($row['id_product'], $row['link_rewrite'], $row['category'], $row['ean13']);
|
$row['link'] = $context->link->getProductLink($row['id_product'], $row['link_rewrite'], $row['category'], $row['ean13']);
|
||||||
$row['id_image'] = Product::defineProductImage($row, $id_lang);
|
$row['id_image'] = Product::defineProductImage($row, $id_lang);
|
||||||
|
$row['allow_oosp'] = Product::isAvailableWhenOutOfStock($row['out_of_stock']);
|
||||||
|
$row['price_tax_exc'] = Product::getPriceStatic(
|
||||||
|
(int)$row['id_product'],
|
||||||
|
false,
|
||||||
|
((isset($row['id_product_attribute']) && !empty($row['id_product_attribute'])) ? (int)$row['id_product_attribute'] : null),
|
||||||
|
(Product::$_taxCalculationMethod == PS_TAX_EXC ? 2 : 6)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user