Author Topic: Filter $products by category  (Read 1185 times)

benedex82

  • Beginner
  • *
  • Posts: 4
  • VirtueMart Version: 2.0.20b
Filter $products by category
« on: May 31, 2013, 09:54:52 am »
I'm using the component cf filter that overwrite the product model and return a product object with all the filtered objects, nothing extremely special..

My goal is to have the category and all the product in that category, using the filtered object.

I managed to do a foreach in every category doing this:

Code: [Select]
<?php foreach ($this->category->children as $category) { 

echo '<h2 class="cat_title">'.($category->category_name).'</h2>'

         foreach ($products as $product) {

                if($product->categories[1]==$category->virtuemart_category_id){ ?>


<div class="product">
<div id="image">
<img src="<?php print_r($product->images[0]->file_url_thumb); ?>" />
</div>
<div id="description">
<h3><?php echo JHTML::link ($product->link$product->product_name); ?></h3>
<?php echo shopFunctionsF::limitStringByWord($product->product_desc150'...')?>
</div>

</div>
       <?php 
  }

}

}
?>

The problem here is that I have also categories without products...

I hope it sounds clear

Thanks

Milbo

  • Virtuemart Projectleader
  • Administrator
  • Super Hero
  • *
  • Posts: 10283
  • VM3.9 Eagle Owl
    • VM3 Extensions
  • VirtueMart Version: VirtueMart 3 on joomla 3
Re: Filter $products by category
« Reply #1 on: June 03, 2013, 22:47:44 pm »
No I do not understand it, sorry. Why do you not use what is already there? the standard object works already with a lot of filters, which you just set in the Request, thats it.
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

balai

  • 3rd party VirtueMart Developer
  • Full Member
  • *
  • Posts: 1483
Re: Filter $products by category
« Reply #2 on: June 04, 2013, 09:37:23 am »
Just for your info Custom Filter is using the Virtuemart's api and is loading in the view exactly the same vars as those loaded in the VM category page.
Also it is using the VM category layout for displaying the results.

So the code you are trying to use mainly concerns the use of the VM vars in the VM category page/view (Just like it happens after a search with the search module).

What is not very clear is what you are trying to do.

benedex82

  • Beginner
  • *
  • Posts: 4
  • VirtueMart Version: 2.0.20b
Re: Filter $products by category
« Reply #3 on: June 06, 2013, 15:31:59 pm »
Just for your info Custom Filter is using the Virtuemart's api and is loading in the view exactly the same vars as those loaded in the VM category page.
Also it is using the VM category layout for displaying the results.

So the code you are trying to use mainly concerns the use of the VM vars in the VM category page/view (Just like it happens after a search with the search module).

What is not very clear is what you are trying to do.

I'm trying to cycle through all the categories to get products divided into category.

I need to have this:

Category A
Product 1
Product 2
Product 3

Category B
Product 1

And the problem is that in this way I cannot hide a category without products but I get this:

Category A
Product 1
Product 2
Product 3

Category B
Product 1

Category C

Category D


Thanks

balai

  • 3rd party VirtueMart Developer
  • Full Member
  • *
  • Posts: 1483
Re: Filter $products by category
« Reply #4 on: June 07, 2013, 09:54:39 am »
Hi

First of all you have to sort the products by category.
So in the file: components\com_customfilters\models\products.php
at the end of the function sortSearchListQuery you should replace the:

Code: [Select]
$query->order($db->escape($orderBy.' '.$filter_order_Dir));
With something like this:

Code: [Select]
if(count($vm_categories)>0){
          $cat_order=implode(',',$vm_categories);
          $query->order($db->escape('FIELD(pc.`virtuemart_category_id`,'.$cat_order.'),'.$orderBy));
        }else $query->order($db->escape($orderBy.' '.$filter_order_Dir));

Having this code will group/sort your products by their category.

Then in your virtuemart category layout you should check the category of each product and when the category changes to print it.
You can do this within the
Code: [Select]
foreach ($this->products as $product) { .......}using the
Code: [Select]
$product->virtuemart_category_id;
To get the category name you need a code like:

Code: [Select]
if (!class_exists('VirtueMartModelCategory')) require(JPATH_VM_ADMINISTRATOR . DS . 'models' . DS . 'category.php');
if(empty($this->categoryModel))$this->categoryModel=new VirtueMartModelCategory();
$category=$this->categoryModel->getCategory($category_id);
$category_name=$category->category_name;

I hope i helped.
Following these you just have to write a little code to check the change of category