Home > Mobile >  Can I use an IF statment in PHP to display text based on return value?
Can I use an IF statment in PHP to display text based on return value?

Time:09-08

I am using a custom packing slip / invoice template which will be sent to customers who order through Amazon. The default Amazon shipping method that comes through is: 'Std UK Dom_1'

I would like to change that so it states 'Royal Mail' in its place. I've gone in to the php of the template to try and mess around to get it to work myself but it is not quite working how I would like. I was thinking for some kind of IF statement to say something like if the label equals 'Std UK Dom_1' then display 'Royal Mail' but I am really not sure if that is possible or what format it would be in at all.

The data that is showing the 'Std UK Dom_1' is <th ><?php echo $total['label']; ?>

I'm new to this kind of thing so anything that anyone could help with would be great.

<td  colspan="3">
    <table >
        <tfoot>
            <?php foreach ( $this->get_woocommerce_totals() as $key => $total ) : ?>
                <tr >
                    <th ><?php echo $total['label']; ?></th>
                    <td ><span ><?php echo $total['value']; ?></span></td>
                </tr>
            <?php endforeach; ?>
        </tfoot>
    </table>
</td>

CodePudding user response:

This should do the thing:

<th ><?php echo ($total['label'] === 'Std UK Dom_1' ? 'Royal Mail' : $total['label']); ?></th>

$total['label'] === 'Std UK Dom_1' checks if the label is "Std UK Dom_1".

If it is, it returns "Royal Mail". Otherwise, it returns the label saved in the variable $total['label'].

Basic construction of this condition is:
CONDITION ? <VALUE IF TRUE> : <VALUE IF FALSE>;

CodePudding user response:

you can check your data using $total['label'] == 'Std UK Dom_1' this condition if your condition is true then pass data which you want. please check below code

<td  colspan="3">
  <table >
    <tfoot>
      <?php foreach ( $this->get_woocommerce_totals() as $key => $total ) : ?>
      <tr >
        <th >
          <?php echo ($total['label'] == 'Std UK Dom_1' ? 'Royal Mail' : $total['label']); ?>
        </th>
        <td ><span ><?php echo $total['value']; ?></span></td>
      </tr>
      <?php endforeach; ?>
    </tfoot>
  </table>
</td>

== Operator is used to check the given values are equal or not. If yes, it returns true, otherwise it returns false. === Operator is used to check the given values and its data type are equal or not. If yes, then it returns true, otherwise it returns false.

  • Related