Jump to content

How to know if the category has promotions? (Optimize)


MFLM

Recommended Posts

Hello everyone

I have a PHP file that generates a result of the categories in store,here the code:

if(Tools::getValue("boutique"))
{

	
	Shop::setIdShop(Tools::getValue('boutique'));
	$cntxt =  Context::getContext();
	$cntxt->shop = new Shop(Tools::getValue('boutique'));
	
	$categories = Category::getCategories($context->language->id);
	
	$resultat = array();
	$row = array();
	
	foreach($categories as $cat)
	{
		
		foreach($cat as $c)
		{
			
			$row = array();
			$categorie = New Category($c["infos"]["id_category"]); 
			$row['id_category']=$c["infos"]["id_category"];
			$row['id_parent']=$c["infos"]["id_parent"];
			$row['titre']=$c["infos"]["name"];
			$row['level_depth']=$c["infos"]["level_depth"];
			$row['nbProduct'] = $categorie->getProducts($context->language->id,0,1000000 , null ,null , true);
			
			foreach ($row as $key => $value) {
				if ($value == NULL) {
					$row[$key]='';
				}
			}
			$resultat[]=$row;
		}
	}
	echo json_encode($resultat);
}  

Now I would like to know, what categories have promotion? 

  $query='
	         SELECT COUNT(p.`id_product`) as totalProducts
	         FROM `'._DB_PREFIX_.'product` p , `'._DB_PREFIX_.'product_shop` ps
	         WHERE p.`active` = 1
	         AND p.`id_product` = ps.`id_product`
	         AND ps.`id_shop` = '.$cntxt->shop->id.'';
			 

		
			$result_product_count = Db::getInstance()->ExecuteS($query);
	        $countt = $result_product_count[0]['totalProducts'];
			
			$promos = Product::getPricesDrop($id_lang = 1, $page_number = 0, $nb_products = $countt, $count = false,
			
			$order_by = null, $order_way = null, $beginning = false, $ending = false,  $context = $cntxt);
			
			$products = $categorie->getProducts($context->language->id,0,$countt+1 , null ,null , false , $active = true, $random = false, $random_number_products = 1, $check_access = true, $context = $cntxt);
			
			$products = $categorie->getProducts($context->language->id,0,$countt+1 , null ,null , false , $active = true, $random = false, $random_number_products = 1, $check_access = true, $context = $cntxt);
			$row['hasSold'] = 0; 
		
			foreach($products as $p)
			{
				if (count(promos)>0) {
					foreach($promos as $promo)
					{
						if ($promo['id_product'] == $p['id_product']){
						$row['hasSold']= 1; 
							
						}
					}
				}
			}

I have the previous code that goes inside in foreach ($ cat as $ c) but it takes more than a minute to get the result when obtaining categories gives it in 2 seconds.

  Is there a way to optimize this variable get? O Is it the only way?   Thanks

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...