Search code examples
phpjoomlajoomla2.5joomla1.5virtuemart

Detect multiple category ID of a product in Virtuemart


We have such a statement that check if product is in this category then show some HTML but the problem is that this product is placed in multiple categories for example in fashion, men and sale so:

fashion category id is 16
men category id is 12
and sale category is 64

I try to identify if product is in category SALE as well in order to add some additional HTML but it works only if I set if($this->product->virtuemart_category_id == 16)

statement wich identify only first category

if($this->product->virtuemart_category_id == 64){

   echo 'your Custom HTML';
}
else{
 //nothing
}

HOw can we identify if this product is in SALE category too?


Solution

  • Try this,

    In VM You can add more than one category for a product,

    It will give the category ids as an array like below.

    print_r($this->product->categories );
    

    So if you have predefined list of categories that required your custom styles.

    if(in_array(16,$this->product->categories) || in_array(64,$this->product->categories) || in_array(12,$this->product->categories)){
    echo 'your styles';
    }
    

    for more better method, you can try something like this

    $custom_cats          = array(12,16,64);//your custom categories that required additional styles
    $current_product_cats = $this->product->categories;
    $checkstatus          = array_intersect($custom_cats , $current_product_cats);
    
    if(sizeof($checkstatus)>0){ // check your first array element found in second array
    echo 'your custom styles';
    }
    

    Hope its works..