1

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)

1

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 } ?>

1

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?

  • is_admin() checks if you'r in a dashboard.. use current_user_can( 'manage_options' ) instead. Also if you have 'trade_price' as a key for Trade Price - use it in get_post_meta( $product->id, 'trade_price', true ); – Maikal Sep 5 '15 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.