$sale) { $categories = array($sale['id_category']); $children = Category::getChildren($sale['id_category'] , 1); if(count($children) > 0){ foreach ($children as $key => $child) { $categories[] = $child['id_category']; } } $sale_product = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' SELECT DISTINCT(p.id_product), p.`out_of_stock`, p.`id_category_default`, p.`ean13`, pl.`description`, pl.`description_short`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, m.`name` AS manufacturer_name, p.`id_manufacturer` as id_manufacturer, i.`id_image`, il.`legend`, ps.`quantity` AS sales, t.`rate`, pl.`meta_keywords`, pl.`meta_title`, pl.`meta_description`, DATEDIFF(p.`date_add`, DATE_SUB(NOW(), INTERVAL '.(Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20).' DAY)) > 0 AS new 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).') 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_.'manufacturer` m ON (m.`id_manufacturer` = p.`id_manufacturer`) LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (p.`id_tax_rules_group` = tr.`id_tax_rules_group` AND tr.`id_country` = '.(int)Country::getDefaultCountryId().' AND tr.`id_state` = 0) LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = tr.`id_tax`) LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_product` = p.`id_product`) LEFT JOIN `'._DB_PREFIX_.'product_sale` ps ON (ps.`id_product` = p.`id_product`) WHERE p.`active` = 1 AND p.`quantity` > 0 AND p.id_product = ( SELECT ps.id_product FROM `'._DB_PREFIX_.'product_sale` ps INNER JOIN `'._DB_PREFIX_.'category_product` cp ON ( cp.id_product = ps.id_product ) WHERE cp.id_category IN ('. implode(',', $categories).') ORDER BY ps.quantity DESC LIMIT 1 ) '); if (!$sale_product) continue; $data = Product::getProductsProperties($id_lang, $sale_product, false); if (isset($filterPrice) && is_int($filterPrice) && $data[0]['price']<$filterPrice) { continue; } $result[] = $data[0]; unset($categories); } $final_array = self::bubble_sort($result); $final_array = array_reverse($final_array); return array_slice($final_array,0,$nbProducts); } public static function bubble_sort($array) { $i = count($array); if ($i <= 0) return false; $ok = false; do { $ok = false; for($j=$i-1; $j!=0; $j--) { if ($array[$j]['sales'] < $array[$j-1]['sales']) { $tmp = $array[$j]; $array[$j] = $array[$j-1]; $array[$j-1] = $tmp; $ok = true; } } } while($ok); return $array; } private static function _getIdCategoryPrivateSales() { global $site_version_front; $sale_exception = Configuration::get('ANT_BESTSALE_EXCEPTION'); $query = ' SELECT p.`id_category` FROM `'._DB_PREFIX_.'privatesale` p LEFT JOIN `'._DB_PREFIX_.'privatesale_site_version` v ON v.`id_sale` = p.`id_sale` WHERE p.`date_start` <= NOW() AND p.`date_end` >= NOW() AND v.`version` = "'.pSql($site_version_front).'" '.((!empty($sale_exception) && $sale_exception!='')?'AND p.`id_sale` NOT IN ('.$sale_exception.')':'').' '; return $sales = Db::getInstance()->ExecuteS($query); } public static function getSaleFamily( $id_sale, $id_lang ) { $query = ' SELECT DISTINCT a.`id_category_family`, c.`name` FROM `'._DB_PREFIX_.'category_family_association` a JOIN `'._DB_PREFIX_.'category_family_lang` c ON c.`id_category_family` = a.`id_category_family` JOIN `'._DB_PREFIX_.'privatesale_category` b ON b.`id_category` = a.`id_category` WHERE b.`id_sale` = '.pSql($id_sale).' AND c.`id_lang` = '.pSql($id_lang); return Db::getInstance()->getRow($query); } public static function getCategoryFamily($id_category, $id_sale, $id_lang) { $result = Db::getInstance()->getRow(' SELECT DISTINCT a.`id_category_family`, c.`name` FROM `'._DB_PREFIX_.'category_family_association` a JOIN `'._DB_PREFIX_.'category_family_lang` c ON c.`id_category_family` = a.`id_category_family` WHERE a.`id_category` = '.(int)$id_category.' AND c.`id_lang` = '.(int)$id_lang ); if($result) { return $result; } else { $query = ' SELECT DISTINCT a.`id_category_family`, c.`name` FROM `'._DB_PREFIX_.'category_family_association` a JOIN `'._DB_PREFIX_.'category_family_lang` c ON c.`id_category_family` = a.`id_category_family` JOIN `'._DB_PREFIX_.'privatesale_category` b ON b.`id_category` = a.`id_category` WHERE b.`id_sale` = '.(int)$id_sale.' AND c.`id_lang` = '.(int)$id_lang; return Db::getInstance()->getRow($query); } } public static function getFamilyBestSales($id_category_family, $id_lang, $nb_products_max = 3, $filter_price=0, $exclude_id_product) { if ($nb_products_max < 1) { $nb_products_max = 3; } $categories = self::_getPrivateSalesCategoriesOfFamily($id_category_family); if (empty($categories)) { return []; } $query = ' SELECT DISTINCT(p.`id_product`), p.`out_of_stock`, p.`id_category_default`, p.`ean13`, pl.`description`, pl.`description_short`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, m.`name` AS manufacturer_name, p.`id_manufacturer` AS id_manufacturer, i.`id_image`, il.`legend`, ps.`quantity` AS sales, t.`rate`, pl.`meta_keywords`, pl.`meta_title`, pl.`meta_description`, DATEDIFF(p.`date_add`, DATE_SUB(NOW(), INTERVAL '.(Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20).' DAY)) > 0 AS new FROM `'._DB_PREFIX_.'product` p LEFT JOIN `'._DB_PREFIX_.'product_sale` ps ON (ps.`id_product` = p.`id_product`) LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_product` = p.`id_product`) LEFT JOIN `'._DB_PREFIX_.'category_family_association` cfa ON cfa.id_category = cp.id_category LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)($id_lang).') 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_.'manufacturer` m ON (m.`id_manufacturer` = p.`id_manufacturer`) LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (p.`id_tax_rules_group` = tr.`id_tax_rules_group` AND tr.`id_country` = '.(int)Country::getDefaultCountryId().' AND tr.`id_state` = 0) LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = tr.`id_tax`) WHERE p.`active` = 1 AND p.`quantity` > 0 AND cp.`id_category` IN ('. pSql(implode(',', array_column($categories, 'id_category'))).') AND cfa.`id_category_family` = '.(int)$id_category_family.' AND p.`id_product` <> '.(int)$exclude_id_product.' ORDER BY sales DESC '; $best_sale_products = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($query); if (empty($best_sale_products)) { return []; } $filtered_best_sale_products = array(); foreach ($best_sale_products as $product) { $properties = Product::getProductProperties($id_lang, $product, false); if (isset($filter_price) && is_int($filter_price) && $properties['price']<$filter_price) { continue; } if ($nb_products_max==0) { break; } $filtered_best_sale_products[] = $properties; $nb_products_max--; } return $filtered_best_sale_products; } /** * display all categories (parents/children) having opened private sales the current version (lang) * and for the given family */ private static function _getPrivateSalesCategoriesOfFamily($id_category_family) { global $site_version_front; $query = ' SELECT `sale_cat`.`id_category` FROM `'._DB_PREFIX_.'privatesale_category` `sale_cat` JOIN `'._DB_PREFIX_.'privatesale` `sale` ON `sale`.`id_sale` = `sale_cat`.`id_sale` JOIN `'._DB_PREFIX_.'privatesale_site_version` `vers` ON `vers`.`id_sale` = `sale`.`id_sale` JOIN `'._DB_PREFIX_.'category_family_association` `cat_fam` ON `cat_fam`.`id_category` = `sale_cat`.`id_category` WHERE `cat_fam`.`id_category_family` = '.$id_category_family.' AND `sale`.`date_start` <= NOW() AND `sale`.`date_end` >= NOW() AND `vers`.`version` = "'.pSql($site_version_front).'" '; return Db::getInstance()->ExecuteS($query); } }