simple PHP If Else problem, can anyone help?

eog

Free Member
Jul 22, 2009
249
58
Hopefully someone can help me. My knowledge of PHP doesnt go any further than looking at identifying areas of code and copy and paste - as basic as you get!

Basically i have a php script installed that produces an RSS feed for me to use with google base. There is a little problem were it does not pull the sale price(known as special) into googlebase and it will instead show the original price. I have identified what needs to be changed but i dont know the correct syntax.


I somehow need to get something similar to the following IF ELSE into the code below (this is taken from the same php script that shows the special price on the actual rss feed)

foreach ($product_data as $result) {
$special = $model_catalog_product->getProductSpecial($result['product_id']);
if ($special) {
$price = $special;
} else {
$price = $result['price'];
}
if ($useTax) {
$price = $currency->format($tax->calculate($price, $result['tax_class_id'], $config->get('config_tax')), $currency->getCode());
} else {
$price = $currency->format($price, $currency->getCode());
}

The code below is were i need the If Else inserted.


<?php foreach ($products as $product) { ?>
<item>
<title><?php echo htmlspecialchars($product['name'], ENT_QUOTES, 'utf-8'); ?></title>
<g:brand><?php echo htmlspecialchars($product['brand'], ENT_QUOTES, 'utf-8'); ?></g:brand>
<g:condition>new</g:condition>
<g:product_type><?php echo $taxonomy; ?></g:product_type>
<description><?php echo htmlspecialchars($product['desc'], ENT_QUOTES, 'utf-8'); ?></description>
<link><?php echo str_replace('&', '&amp;', $product['href']); ?></link>
<pubDate><?php echo $product['add_date']; ?></pubDate>
<!--guid isPermaLink="true"><?php echo $product['href']; ?></guid-->
<g:id><?php echo (int)$product['id']; ?></g:id>
<!--g:upc><?php echo sprintf("%012s", $product['id']); ?></g:upc-->
<g:image_link><?php echo $product['thumb']; ?></g:image_link>
<!--g:expiration_date>2010-01-01</g:expiration_date-->
<g:price><?php echo $product['price']; ?></g:price>
<g:mpn><?php echo htmlspecialchars($product['model'], ENT_QUOTES, 'utf-8'); ?></g:mpn>
<g:weight><?php echo $product['weight']; ?></g:weight>
<g:currency><?php echo $config->get('config_currency'); ?></g:currency>
</item>
<?php } ?>


The <g:price> is what googlebase reads to get the price of the product. The problem seems to be that there is no if else statement here to output the special price if there is one.


Any help would be greatly appreciated, i tired emailing the developer but i have been ignored and as you can see i'm pretty clueless
 
If you mean you simply want to insert the script at the top into the script at the bottom - all you want do is:

<g:price>
<?php

IF STATEMENT SCRIPT GOES HERE

?>
</g:price>

Any code can be placed between <? and ?> - they are tags used to denote blocks of PHP.

You will also need to amend the script slightly, so that:

$price = $special;

becomes

echo($special);

in order to output the values - does that make sense (too early in the morning)!
 
Last edited by a moderator:
  • Like
Reactions: eog
Upvote 0
Hi Matt

Not exactly, the code on top is how the developer has called the special price earlier in the script, so i included it as a reference.

Basically i need the <g:price> to display the special price if it is set, at the moment in will only ever show the original price.

I have tried the following code to get the <g:price> so show the special price but i get the error Notice Undefined index: product_id

<g:price>
<?php
$special = $model_catalog_product->getProductSpecial($product['product_id']);
if ($special) {
$price = $special;
} else {
$price = $product['price'];
}
echo $price;
?>
</g:price>

any ideas?
 
Upvote 0
I think (from looking - hard to tell) that it should be 'id' not 'product_id' - sorry! :)
 
  • Like
Reactions: eog
Upvote 0

Latest Articles