0

Ok the last time I asked a question like this, more people got on my case for not knowing something this simple rather than actually helping me out. I don't want any of that. I just need help and would greatly appreciate it.

I am trying to style the search results on an ecommerce site. What I HAVE gotten to work so far is this statement.

<?php if(wpsc_product_normal_price() == wpsc_the_product_price()):  ?>              
    <p class="s_price" style="margin:0 0 10px 30px;"><?php  echo wpsc_the_product_price(); ?></p>
    <?php else: ?>
    <p class="s_price s_promo_price"><span class="s_old_price"><?php  echo wpsc_product_normal_price(); ?></span><?php  echo wpsc_the_product_price(); ?></p>
    <?php endif; ?>

So as far as I understand and what I see happening, that snippet returns the price of the product being returned in the search.

BUT, not all of the results that come up are products for sale, sometimes they are blog posts, blah blah. So I tried the following code but I couldn't get it to work. What I want to happen is that if the price is 0, or if there isn't a price, there won't be anything displayed.

<?php $searchprice=wpsc_product_normal_price();
 if $searchprice > "0"  ?> 
<?php if(wpsc_product_normal_price() == wpsc_the_product_price()):  ?>              
<p class="s_price" style="margin:0 0 10px 30px;"><?php  echo wpsc_the_product_price(); ?></p>
<?php else: ?>
<p class="s_price s_promo_price"><span class="s_old_price"><?php  echo wpsc_product_normal_price(); ?></span><?php  echo wpsc_the_product_price(); ?></p>
<?php endif; ?>
<?php endif; ?> 

Again, I know I am not as awesome as you all are so I would appreciate that you didn't point that out, that won't solve my problem.

Thanks!

I tried this but the prices don't show up

<?php $searchprice=wpsc_product_normal_price();
if ($searchprice > "0") :  ?> 
<?php if(wpsc_product_normal_price() == wpsc_the_product_price()):  ?>              
<p class="s_price" style="margin:0 0 10px 30px;"><?php  echo wpsc_the_product_price(); ?></p>
<?php else: ?>
<p class="s_price s_promo_price"><span class="s_old_price"><?php  echo wpsc_product_normal_price(); ?></span><?php  echo wpsc_the_product_price(); ?></p>
<?php endif; ?>
<?php endif; ?>
2
  • And you're sure your running your code inside the product loop? while ( wpsc_have_products() ) : wpsc_the_product();? Commented Jul 5, 2013 at 23:01
  • Yes I am sure. It works fine until I add the extra if statement. Commented Jul 5, 2013 at 23:07

4 Answers 4

2

This line:

 if $searchprice > "0"  ?> 

should be:

 if ($searchprice > "0") :  ?> 

You're missing the parentheses around the condition, and the : to indicate where the then-block starts.

Here's the whole thing. I replaced your function calls with variables because I don't have the rest of your code, but that should make no difference.

<?php
  $searchprice = 20;
  $productprice = 10;
if ($searchprice > "0"):  ?> 
<?php if($searchprice == $productprice):  ?>              
  <p class="s_price" style="margin:0 0 10px 30px;"><?php  echo "Product: " . $productprice; ?></p>
<?php else: ?>
  <p class="s_price s_promo_price"><span class="s_old_price"><?php  echo "Normal: " . $searchprice; ?></span><?php  echo " Product:" . $productprice; ?></p>
<?php endif; ?>
<?php endif; ?> 

It displays:

Normal: 20 Product:10
Sign up to request clarification or add additional context in comments.

6 Comments

Here is the whole thing I tried <?php $searchprice=wpsc_product_normal_price(); if ($searchprice > "0") : ?> <?php if(wpsc_product_normal_price() == wpsc_the_product_price()): ?> <p class="s_price" style="margin:0 0 10px 30px;"><?php echo wpsc_the_product_price(); ?></p> <?php else: ?> <p class="s_price s_promo_price"><span class="s_old_price"><?php echo wpsc_product_normal_price(); ?></span><?php echo wpsc_the_product_price(); ?></p> <?php endif; ?> <?php endif; ?>
I can't read that. See what I wrote above about trying to put code in comments.
Alright I'll try implementing all of that. Thanks a ton. I really appreciate it.
You might have another typo somewhere. Check your PHP error log.
Ok yeah I still can't get the prices to appear but I'll keep trying. It works when I copy your code exactly but as soon as I try switching mine in it fails. You rock though man.
|
1

I would suggest you try to use the more standard syntax for code blocks, and use echo instead of dropping in and out of PHP:

<?php
$searchprice = wspc_product_normal_price();
if($searchprice) {
  $productprice = wspc_the_product_price();
  if( $searchprice == $productprice) {
    echo '<p class="s_price" style="margin:0 0 10px 30px;">'.$searchprice.'</p>';
  }
  else {
    echo '<p class="s_price s_promo_price"><span class="s_old_price">'.$searchprice.'</span>'.$productprice.'</p>';
  }
}

6 Comments

Only if you're dropping out of PHP. You don't have to.
Ok let me see if I can add it correctly. Thanks for your help.
Ok so I tried this <?php $searchprice = wspc_product_normal_price(); if($searchprice) { $productprice = wspc_the_product_price(); if( $searchprice == $productprice) { echo '<p class="s_price" style="margin:0 0 10px 30px;">'.$searchprice.'</p>'; } else { echo '<p class="s_price s_promo_price"><span class="s_old_price">'.$searchprice.'</span>'.$productprice.'</p>'; } } endif; endif;?> but that didn't work. Sorry, I am learning.
@user1470242 It's recommended to omitted the closing ?> tag from files containing only PHP.
As I written above, this is right but might fail if some strange character came inside of $searchprice. By doing the comparison with a number, php knows what we talking about.
|
0

I think you have two choices:

if($searchprice>0)

Or the ugly

if($searchprice)

Might not work, but php converts 0 to false. I'll use the first one.

Comments

0

First of all, I've no idea what wpsc_product_normal_price() and wpsc_the_product_price() return, so I will just focus on your code, and then I'll focus on what you want to happen. I hope that by using one or a combination of both my explanations you will figure out what to do.

Your code needs a bit of revision, there's is a syntax mistake (don't know if it is a typo) and not so common syntax uses. I'll address that in the code comments. There is nothing wrong in echoing the html outside the php snippets like you do, I'll just write it the other way for clarity, and I will not pay attention to html. The if syntax I use is probably the standard practice among programmers, I suggest you get accostumed to using it; but is not mandatory, of course.

// We get the normal price of the product and store it 
// in $searchprice, 
// we don't need to call that function again
$searchprice=wpsc_product_normal_price();

// If is not 0, I guess it means we have a price? 
// This is a better syntax, you were not using parenthesis 
// and you don't need to quote a number 
if ($searchprice > 0) {

     // we compare the normal price to the product price and
     // if they're equal we output "either one" since they're equal, 
     // and we already have wpsc_product_normal_price() stored 
     //in $searchprice so we output $searchprice       
     if($searchprice == wpsc_the_product_price()) {
         echo $searchprice;
     }

   // this else means that $searchprice, that is,  
   // wpsc_product_normal_price(), is 
   // either 0 or a negative price?
   // then we output $searchprice? remember it will be 0 or less 
 } else {

// echo $searchprice since it already has the value returned 
// by wpsc_product_normal_price(), no need to call that 
// function again
 echo 'The normal price is '. $searchprice  . ', and this is the product price:' .  wpsc_the_product_price();

}

Now, improving this code we would:

// We store both since we're going to use them more than once
 // This way we call these functions only once (it is better and 
 // faster to reuse data in variables than calculating that data 
 // everytime we need it)
 $searchprice= wpsc_product_normal_price();
 $productprice= wpsc_the_product_price();

// We check both conditions at the same time.
// This means $searchprice is a number higher than zero AND it is 
// the same number as in  $productprice (calculated by 
// wpsc_the_product_price)
if ($searchprice > 0 && $searchprice == $productprice) {
    // We output either one
    echo $searchprice;
} else {
    // $searchprice is 0 or less and it is not the same as $productprice.
    // We echo both. They are 2 different prices but the first is 0 or less.
    echo 'The normal price/search is '. $searchprice  . ', and this is the product price:' .  $productprice;
}

That was about the code you've shown, but what you want to happen is

that if the price is 0, or if there isn't a price, there won't be anything displayed.

For that precise request the code would be something like (again I've no idea what your functions do or where $searchprice comes from).

// Check if $searchprice is greater than zero
// or not empty, for instance, or you could 
// also check for null, false depending on 
// what $searchprice is
if ($searchprice > 0 || $searchprice!="") {
    // We output $searchprice
    echo $searchprice;
} else {
    // $searchprice is 0 or less
    // if you don't want anything 
    // to happen when $searchprice is 0 
    // just remove this 
    // entire else statement.
    // Nothing will happen.
}

So, according to what you want it could even be shorter:

if ($searchprice > 0) echo $searchprice;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.