VirtueMart Forum

VirtueMart 2 + 3 + 4 => Templating & Layouts => Topic started by: Alexikakos on February 09, 2021, 11:01:32 AM

Title: Display category description inside products on category view
Post by: Alexikakos on February 09, 2021, 11:01:32 AM
Hi all,

I want to display category description inside product item, on category view.

I tried edit products.php (\public_html\templates\mytemplate\html\com_virtuemart\sublayouts\products.php).
I added inside "foreach ( $products as $product ){...}"
the follow line:
<?php echo $product->category_description?>
<?php echo $product->category->category_description ?></div>
<?php echo $this->category_description ?></div>

But not Working. Any idea?

PS: The wired that this code its working inside product foreach: <?php echo $product->virtuemart_category_id;  ?>, <?php echo $product->category_name?>, but not category description.

Joomla Version: 3.9.24
VirtueMart Version: 3.8.4
PHP 7.3

Title: Re: Display category description inside products on category view
Post by: Jörgen on February 09, 2021, 12:40:27 PM
The category description should be the same for all products in the sam category. Is there not cote to display the category description earlier in the file ? Code that is not linked to the product but to the current category ?

Jörgen @ Kreativ Fotografi
Title: Re: Display category description inside products on category view
Post by: Alexikakos on February 09, 2021, 13:53:06 PM
Hi,
Yes it does.

In catagory view file (\public_html\templates\mytemplate\html\com_virtuemart\category\default.php) it use this echo:
<?php echo $this->category->category_description?>
But this code it work in \public_html\templates\mytemplate\html\com_virtuemart\category\default.php not in \public_html\templates\mytemplate\html\com_virtuemart\sublayouts\products.php

The products layout (in category view) is in product.php file in this path: \public_html\templates\mytemplate\html\com_virtuemart\sublayouts\products.php
the <?php echo $this->category->category_description?> not working in product.php only in default.php.

That is my problem
Title: Re: Display category description inside products on category view
Post by: Jörgen on February 09, 2021, 15:25:01 PM
You can transfer the data to the products.php when the sublayout is rendered. If You can not figure it out, I will show an example later tonight.

Jörgen @ Kreativ Fotografi
Title: Re: Display category description inside products on category view
Post by: Alexikakos on February 10, 2021, 08:20:59 AM
Yes please
if u can, post the example
Title: Re: Display category description inside products on category view
Post by: Jörgen on February 10, 2021, 11:41:37 AM
Hello this shows the fundamentals:
echo shopFunctionsF::renderVmSubLayout($this->productsLayout,array('products'=>$this->products,
'currency'=>$this->currency,
'products_per_row'=>$this->perRow,
'showRating'=>$this->showRating,
'categoryDescription'=>$this->category->category_description));

That code transfers the data categoryDescription into the array viewData[]

In the layoutfile products.php You will have to use this code to get data from viewData:
$categoryDescription = $viewData['categoryDescription'];

You can add as many items to the viewData as You like.

I hope you get the idea.

Jörgen @ Kreativ Fotografi
Title: Re: Display category description inside products on category view
Post by: Alexikakos on February 10, 2021, 13:18:24 PM
I think i understand you,

but i think i need and an "echo" to display it.

i tried <?php echo shopFunctionsF::renderVmSubLayout('categories',array('product'=>$product,'categoryDescription'=>$categoryDescription)); ?> but not results,

How to echo "shopFunctionsF::renderVmSubLayout" for "categoryDescription"
Title: Re: Display category description inside products on category view
Post by: Jörgen on February 10, 2021, 14:00:06 PM
I tried to show You the basics, not a cut and paste code.

In the present rendersublayout call you have to add Your new item with correct data for $categoryDescription. This will carry the data through to the sublayout.
In the sublayout (products.php) , You have to collect the $categoryDescription from the $viewData array. Then You can echo the $categoryDescription.

I use this technique in all my overrides, so I know it works, You just have to figure out where to adjust the code.

Jörgen @ Kreativ Fotografi
Title: Re: Display category description inside products on category view
Post by: Dimi on February 15, 2021, 12:33:38 PM
Hello,
I'am trying to make the same thing, but I have a small problem.

My category tree is:
Products -> Women -> Shirts, Jackets, Dresses, etc.

I have a menu item Women where I display all the products of the subcategories.
I used Jörgen's code to display inside every product box the description of the category.

For example
In every dress I want to display the Dresses (subcategory) description, but the code I used fetches the Women (parent category) description.

Any ideas on what I should change?

Thank you.