News:

You may pay someone to create your store, or you visit our seminar and become a professional yourself with the silver certification

Main Menu

Filter $products by category

Started by benedex82, May 31, 2013, 09:54:52 AM

Previous topic - Next topic

benedex82

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:

<?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

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

#2
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

Quote from: balai 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.

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

#4
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:

$query->order($db->escape($orderBy.' '.$filter_order_Dir));

With something like this:

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 foreach ($this->products as $product) { .......}
using the $product->virtuemart_category_id;
To get the category name you need a code like:

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