I have a page that displays products based on a previous user query. The search results contain sets of metadata, most importantly a "product rating" on a 1-10 scale.
Here's the code that pulls the product rating metadata
<?php echo get_post_meta( get_the_ID(), 'product_rating', true ); ?>
This pulls the 'rating' metadata from our dB and simply displays the rating as a numeral (1, 1.5, 2, 2.5 ect).
I'm looking to take the value of the 'product_rating' and set an image to display for each value. A rating of 1 will display one image, while a rating of 2 display a different image ect ect.
The current code I have in place substitutes an image for the value, but the image is displaying multiple times (up to 10 times for a single value). My code is below, using a rating value of "4" as an example.
<?php $rating = get_post_meta( get_the_ID(), 'product_rating', true ); ?>
<?php For($i=4; $i <= $rating; $i++){
echo "<img src='http://www.domain.com/lpimage/lock50.png' />";
}
?>
Is my code close to where I need to be, or am I completely off base for the function I want?
Assuming your rating variable is correctly defined I think you can handle the conditional display of various images as follows:
<?php if ($rating == 1) echo '<img src="urlofimage1.jpg"></img>';
elseif ($rating == 2) echo '<img src="urlofimage2.jpg"></img>';
elseif ($rating == 3) echo '<img src="urlofimage3.jpg"></img>';
elseif ($rating == 4) echo '<img src="urlofimage4.jpg"></img>';
?>
You use the double equal sign when comparing a value, you only use the single equal sign (=) when setting a variable's value.
Use >= and <= for ranges, ie. 1.3 or 2.7
Related
I want to build a layout where I show teasers of two different content type (A-B).
The teasers will be just images, where 1 teaser of content type A is the half in height of content type B and I would like to have for each row 3 columns.
So first and last columns with 2 teaser each of content type A and the center column with one teaser of content type B. 1A-1A -- 1B -- 1A-1A.
I used the following code in the views-view-unformatted.tpl.php to have the correct layout structure (probably I should have done this on template.php)
<?php foreach ($rows as $id => $row): ?>
<?php if($id % 5 == 0){ print '<div class="row">'; } ?>
<?php if($id % 5 == 2){ print '<div class="col-center">'; } ?>
<div class="<?php print $classes_array[$id]; ?>"><?php print $row; ?></div>
<?php if($id % 5 == 2){ print '</div>'; } ?>
<?php if($id % 5 == 4){ print '</div>'; } ?>
<?php endforeach; ?>
Now I get stuck on giving the order to the content type to display, so telling views that two first teasers have to be content type A, center one is content type B, last two content type A.
Here a screenshot of how should look like the layout, so maybe is easier to understand :) screenshot layout
Any suggestion will be appreciate! Thanks for reading
That`s a wrong way of doing it.
Do your operations in pre render function and then render variables in tpl file.
URL: hook_views_pre_render
I would like to put a a message to say "available in other colors" on the category page if there is more than one color you can select on the product. Right now I have some code in content.product.php that detects if it's a variable product, but not if there are more than one option for an attribute. Color is a custom attribute.
Right now I have:
<?php if($product->product_type == "variable") { ?>
<h4>Available in other colors.</h4>
<?php } else { ?>
<?php } ?>
It's showing all variable product type with this message. I want to show it only if there are 2 or more colors attached to the product. The products has two attributes on it - size and color. Size always has three or four wheras color sometimes only has one
I guess you are looking for something like this?
<?php
if ($product->product_type == "variable") {
print '<h4>Variable products message.</h4>';
}
else if (count($product->color) > 2) {
print '<h4>This product is avilable in other colors!</h4>';
}
else {
#Optional print here..
}
?>
I might be wrong all you need to do is test by writing
<?php print_r($product); exit;?>
see where the colors are beign stored and just replace the ->color with the proper name. Good luck!
This is what ended up working
<?php
$fabric_values = get_the_terms( $product->id, 'pa_color-swatch');
if (count($fabric_values) >= 2) {
print '<h4>Available in other fabrics.</h4>';
}
else {
#Optional print here..
}
?>
I have created a store (opencart) in which i have a product and user can choose the quantity and pack of product. So I have created custom option to choice quantity.
the price of product is shown like this :
I have to add price to to an original price for each pack :
now the result Current :
packs :
$1.9
100(0)
200(+$0.9)
500(+$1.3)
1000(+$2.9)
1200(+$3.9)
What I want:
$1.9
100($1.9)
200($2.8)
500($3.3)
.
.
.
You can check link here :
http://www.shopfairpackaging.co.uk/index.php?route=product/product&path=60_69&product_id=52
Opencart options are designed to be cumulative. Meaning that as the shopper selects difference options, the final price gets increased or decreased accordingly.
Opencart does not support the schema that you are seeking. However if you are desperate and need to switch to your own schema, then you could edit the theme files.
To achieve the results you are seeking do the following:
Locate the product.tpl file in /catalog/view/theme/default/template/product/ folder
Locate this line (<?php echo $option_value['price_prefix']; ?><?php echo $option_value['price']; ?>) around line number 195, depending on your version
replace it with this (<?php $additive=str_replace('$','',$option_value['price'])+str_replace('$','',$price);echo '$'.number_format($additive, 2);?>)
Scriptonomy Your answer is half right it was giving me result of 00 so I did this
(<?php
//get first currency symbol
$symbol = substr($price, 1);
//get float value form string
$x = floatval(ltrim(ltrim(ltrim($option_value['price'], '£'), '$'), '€'));
$y = floatval(ltrim(ltrim(ltrim($price, '£'), '$'), '€'));
if($option_value['price_prefix'] == "+"){
echo $symbol;
echo $x + $y;
}elseif ($option_value['price_prefix'] == "-") {
echo $symbol;
echo $x - $y;
}else{
echo $symbol;
echo $option_value['price_prefix'].$option_value['price'];;
}
?>)
Tell me if i did something worng
I've stored values in an array and I want to apply conditional formatting to the output when I access them.
I want the text of the values, less than or equal to 10, displayed in red or, greater than 10, displayed in green.
For example, below, I would like the resultant value of 5 to be displayed in red.
I'm a newbie to this but would assume I would need the help of CSS to achieve this.
Any advice would be appreciated.
<?php
$total_sql=array();
$total_sql[]=5;
$total_sql[]=10;
$total_sql[]=15;
$total_sql[]=20;
print($total_sql[0]);
?>
<?php
$total_sql=array();
$total_sql[]=5;
$total_sql[]=10;
$total_sql[]=15;
$total_sql[]=20;
print($total_sql[0]);
?>
foreach($total_sql as $item)
{
if($item >10)
{
echo '<p class="green">'.$item.'</p>';
} else {
echo '<p class="red">'.$item.'</p>';
}
.red{color:red;}
.green{color:green}
One liner:
echo '<div style="color: '.(($score == '5') ? 'red' : 'black').';">'.$score.'</div>';
Comparison operator could be:
$score == 'x'
> 'y'
>= 'z'
etc...
I've used an inline style here, but you can use class= too.
I have a foreach loop that builds me a product page basically it out puts my products in rows of 3.
See the code:
foreach ($product_sets as $product)
{
$currentRow = ceil($currentItem / 3);
$currentColumn = $currentItem - (($currentRow - 1) * 3);
if ($number_of_blanks == 2) :
if (($number_of_rows > 1 && $currentRow == ($number_of_rows - 1) && $currentColumn == 2) || ($number_of_rows == 1 && $currentColumn == 1)) :
?>
<li><img src="<?php echo site_url('assets/img/blocks/guarantee.png'); ?>" alt="5 Year Guarantee" width="242" height="156"></li>
<?php
$currentItem++;
endif;
endif;
?>
<li class="<?php if($currentItem % 3 == 0) echo 'endHomeBlock';?>">
<?php $this->load->view('blocks/product_small', array('product' => $product)); ?>
</li>
<?php
$currentItem++;
}
What I am wanting be able to do is place an image (A point of sale) at the end of the first row, and the randomly through the other rows, but maintaining 3 items (including image a point of sale) on a row. I have the image paths in an array called images which looks similar to this,
$images = array(
'iamge1.png',
'image2.png,
'image3.png,
'image4.png,
);
How can I achieve this? I have been scrathing around for a few hours now :(
Sorry I don't have much time in writing you the full code, but the following should work:
<ul>
<li><?php
foreach($items as $i=>$item){
// ...write item...
if(($i % 3)==0 && $i!=0){ // if multiple of 3 and not the first time..
?></li><li><?php
}
}
?></li>
</ul>
So what I would do is create a hashtable of your images and at what points in the table you want to display them. The key of the table would be the index and the value would be the image name.
Assuming $currentItem is zero-based, the first key you would have would be 2 for the third item in the first row.
Then in your loop, check and see if $currentItem is in the hashtable. If it is, print the image and increment $currentItem (and recalculate row and column) and then print $product. If it is not in the hashtable, just print $product like normal.