Help with some php math in WordPress

I’ve made a woocommerce store and I am using a dropshipper – so every product has an price and a trade price for me – which I am importing and storing as a custom field ‘trade_price’ on the product.

I wanted to know if it would be possible to include some php on my single product template which would show me 1) profit per item (price minus trade price) and 2) margin on said product (percentage difference between trade price and sale price)

Solutions Collecting From Web of "Help with some php math in WordPress"

You just need to echo it.

<?php
echo $price - $trade;
echo $trade / $sale;
?>

or you can go more advanced by going

<?php
if ( ! is_admin() ) {
// is admin then
echo $price - $trade;
echo $trade / $sale;
} else {
// is not admin then
}
?>

Thanks @WPvolunteer. Here’s what I settled on ..

<?php
if ( ! is_admin() ) {
  // is admin then
  $trade_price = get_post_meta( $product->id, 'Trade Price', true );
  $sale_price = get_post_meta( $product->id, 'Price Inc Vat', true );

  $profit_percent = $trade_price / $sale_price;

  echo "Trade price: ";
  echo $sale_price - $trade_price; 
        echo "<br>";
  echo "Percent profit: ";
  echo substr($profit_percent, 0, 4);
        echo "<br>";

} else {
  // is not admin then
}
?>

I added some truncation since the percentage value went on forever. I’d be interested in adding a function for these equations so I can see them for all my products on one page if anyone can help?