Author Topic: In main category, display products separated by subcategory  (Read 347 times)

Dimi

  • Beginner
  • *
  • Posts: 14
  • A beginner
In main category, display products separated by subcategory
« on: March 08, 2021, 12:57:35 pm »
Hello,
I am developing an eshop with food and the clients needs to have the following view:
In the main category view, he wnats to view all the products separated in subcategories and if a product has two categories, it should be displayed twice. You can see what I mean in the screenshot I have attached.

The category tree is Deli (main category) -> Salt, Pasta, Bread etc.
The products have subcategories but not the main category.

I am displaying all the products in the category Deli menu item which is set to display the products from the subcategories.

So, the structure I want to have in the Deli page is:
Deli
  ->Subcategory 1 (name and description)
        ->Product
        ->Product
        ->Product
  ->Subcategory 2 (name and description)
        ->Product
        ->Product
        ->Product


I have tried calling the products sublayout inside the categories foreach in the file sublayouts/categorires.php:
On around line 48 foreach ( $categories as $category ) {...}
I have added the following:
Code: [Select]
if (!empty($this->products)) {
if($this->fallback){
$p = $this->products;
$this->products = array();
$this->products[0] = $p;
vmdebug('Refallback');
}
echo shopFunctionsF::renderVmSubLayout($this->productsLayout,array('products'=>$this->products,'currency'=>$this->currency,'products_per_row'=>$this->perRow,'showRating'=>$this->showRating));
}

With this code I get a 0 - Using $this when not in object context error. I tried changing $this to $category or $product but nothing happens.

So, what I want to do is:
  • Display all the products of the main category, separated in subvcategories
  • A product that is is two categories, should be displayed twice - once for each category

Joomla Joomla! 3.9.25
Virtuemart 3.8.4
Php 7.3

Any help would be much appreciated.

 
 

Dimi

  • Beginner
  • *
  • Posts: 14
  • A beginner
Re: In main category, display products separated by subcategory
« Reply #1 on: March 09, 2021, 10:18:31 am »
Hello again,
I am trying another way to display the subcategories and their products.

In the category/default.php I wrote the following:
Code: [Select]
foreach ( $this->category->children as $category ) {
echo vmText::_($category->category_name);
echo vmText::_($category->category_description);
echo shopFunctionsF::renderVmSubLayout($this->productsLayout,array('products'=>$this->category->children->products,'currency'=>$this->category->children->currency,'products_per_row'=>$this->category->children->perRow,'showRating'=>$this->category->children->showRating));
}

So, my plan is:
1. get the subcategories using a foreach
2. inside the foreach I echo the subcategory name and description - they display correctly
3. for each subcategory get the products sublayout (sublayouts/products.php)

The code creates all the subcategories and brings all the products after each subcategory. So I have All Products * All Subcategories. I'm assuming there is something wrong in the part
Code: [Select]
echo shopFunctionsF::renderVmSubLayout($this->productsLayout,array...
Probably $this should be something else but I don't know what. I tried replacing it with $category, $this->category, $this->category->children but nothing happens.

Any ideas?

pinochico

  • 3rd party VirtueMart Developer
  • Full Member
  • *
  • Posts: 1137
    • MiniJoomla
  • Skype Name: support-easysoftware
  • VirtueMart Version: 3
Re: In main category, display products separated by subcategory
« Reply #2 on: March 09, 2021, 11:50:23 am »
You want this?
www.minijoomla.org  - new portal for Joomla!, Virtuemart and other extensions
XML Easy Feeder - feeds from products, orders and database table
Virtuemart Email Manager - customs email templates

Dimi

  • Beginner
  • *
  • Posts: 14
  • A beginner
Re: In main category, display products separated by subcategory
« Reply #3 on: March 09, 2021, 13:16:43 pm »
Hi pinochico,
Yes, this is exactly what I am looking for.

I just used the code from https://forum.virtuemart.net/index.php?topic=138904.0 and it works fine but I can't get the products to display twice. Some of the products have two categories (Patsta, Mushrooms) and I need to display them twice.