News:

Looking for documentation? Take a look on our wiki

Main Menu

In main category, display products separated by subcategory

Started by Dimi, March 08, 2021, 12:57:35 PM

Previous topic - Next topic

Dimi

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:

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

Hello again,
I am trying another way to display the subcategories and their products.

In the category/default.php I wrote the following:

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

www.minijoomla.org  - new portal for Joomla!, Virtuemart and other extensions
XML Easy Feeder - feeds for FB, GMC,.. from products, categories, orders, users, articles, acymailing subscribers and database table
Virtuemart Email Manager - customs email templates
Import products for Virtuemart - from CSV and XML
Rich Snippets - Google Structured Data
VirtueMart Products Extended - Slider with products, show Others bought, Products by CF ID and others filtering products

Dimi

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.