VirtueMart Forum

VirtueMart 2 + 3 + 4 => Templating & Layouts => Topic started by: Jack Stiles on August 21, 2018, 20:30:01 PM

Title: Different Background for Each Product
Post by: Jack Stiles on August 21, 2018, 20:30:01 PM
So I have one more thing here... Lets say I only have like 5 products on a whole shop but I really need every one of them to be well designed. Is there a way to use a different background color for each product? Didn't find such a thing here on forum...

Iam on latest Joomla and latest VM
Title: Re: Different Background for Each Product
Post by: PRO on August 21, 2018, 21:07:10 PM
for category page?

sublayouts/products.php

change this
    // Show Products ?>
   <div class="product vm-col<?php echo ' vm-col-' . $products_per_row . $show_vertical_separator ?>">

to this

    // Show Products ?>
   <div class="color<?php echo $product->virtuemart_product_id ?> product vm-col<?php echo ' vm-col-' . $products_per_row . $show_vertical_separator ?>">

then you will have a class set for each product


.colorTHE_PRODUCT_ID

Then just style it in your style sheet like you want.
Title: Re: Different Background for Each Product
Post by: Jack Stiles on August 22, 2018, 11:31:48 AM
OK great  :) - one question though. I am not really sure how sublayouts work but - if I change anything in it - does the update overwrite it?
Title: Re: Different Background for Each Product
Post by: Jack Stiles on August 22, 2018, 11:39:34 AM
Quote from: PRO on August 21, 2018, 21:07:10 PM
for category page?

Nah not really - I need to be able to change is on a product detail page - the whole page background.
Title: Re: Different Background for Each Product
Post by: GJC Web Design on August 22, 2018, 12:31:51 PM
same idea but on the over ridden product details template
Title: Re: Different Background for Each Product
Post by: Jack Stiles on August 22, 2018, 14:07:06 PM
So this doesn't work. It always adds "color" to a class but no ID behind it :-(
Title: Re: Different Background for Each Product
Post by: Jack Stiles on August 22, 2018, 14:11:22 PM
I should add that I cannot add this to a product details because I need to adjust the whole background on a whole product page so I added this as a body class to my template index.php. But it wasnt working in product description as well. I always adds only COLOR which means its unable to understand this: <?php echo $product->virtuemart_product_id ?>
Title: Re: Different Background for Each Product
Post by: Jörgen on August 22, 2018, 14:54:35 PM
It should work in sublayout products.php. You must be in the right context. Within this for example:

foreach ( $products as $product ) {

Otherwise $product may be meaningless.

use

print_r($product);

to see the contents of product.


Jörgen @ Kreativ Fotografi
Title: Re: Different Background for Each Product
Post by: Jack Stiles on August 22, 2018, 16:22:02 PM
Well I used another approach. But thank you guys cause this thread help me to understand how to proceed.

What I did was this - each product has a unique url so I did this:

color<?php if(strpos($_SERVER['REQUEST_URI'], 'productname') !== false):?>productname<?php else: ?><?php endif; ?>

Title: Re: Different Background for Each Product
Post by: Studio 42 on August 22, 2018, 22:59:02 PM
Quote from: Jack Stiles on August 22, 2018, 16:22:02 PM

color<?php if(strpos($_SERVER['REQUEST_URI'], 'productname') !== false):?>productname<?php else: ?><?php endif; ?>

One of the poorest code i have see!
Title: Re: Different Background for Each Product
Post by: GJC Web Design on August 23, 2018, 00:01:06 AM
<div class="color<?php echo $this->product->virtuemart_product_id ?>" >

if your on details
Title: Re: Different Background for Each Product
Post by: Jack Stiles on August 23, 2018, 08:22:06 AM
Quote from: Studio 42 on August 22, 2018, 22:59:02 PM
One of the poorest code i have see!

Well thank you for letting me know it was really helpful  :o
Title: Re: Different Background for Each Product
Post by: Studio 42 on August 23, 2018, 10:25:31 AM
Jack, i think this helps you.
You have solution using virtuemart product id, this is set in all case when you show a product.
IF you need to use it in the template directly and not in virtuemart, you can get it from the input query.
see in general for joomla https://docs.joomla.org/Retrieving_request_data_using_JInput
in your case :
<?php 
$jinput 
JFactory::getApplication()->input;
$id$jinput->getInt('virtuemart_product_id',null);
if(empty(
$id)) $id='';
?>

<div class="color<?php echo $id ?>">




Title: Re: Different Background for Each Product
Post by: Jack Stiles on August 24, 2018, 10:41:48 AM
Quote from: Studio 42 on August 23, 2018, 10:25:31 AM
Jack, i think this helps you.
You have solution using virtuemart product id, this is set in all case when you show a product.

Thanks! Works as a charm!  ;)