Need a heads on on how to add VAT tpo a products price in a bit of code

Open to all! Ask other shopowners for help.
Post Reply
14Steve14
Senior Contributor
Posts: 923
Joined: Fri Oct 25, 2019 7:01 pm
Phoenix Version: v1.0.9.1
Has thanked: 17 times
Been thanked: 103 times

Need a heads on on how to add VAT tpo a products price in a bit of code

Post by 14Steve14 »

I need a bit of help with a small modification of a small bit of code in a admin - categories - hook file

The current code shows the product price, but less VAT at 20%. I want to show the price under the current price with the VAT added. So one row with net price the other gross price.

The current code I have is

Code: Select all

function listen_infoBox($parameters) {
      global $product; $this->load_lang();

      if ($product && ($product Instanceof Product)) {
        if (!is_null($product->get('cost') ?? null)) {
          $parameters['contents'][] = ['text' => COST_INPUT_TEXT . ' ' . $product->get('cost')];
        }
        if (!is_null($product->get('price') ?? null)) {
          $parameters['contents'][] = ['text' => NET_PRICE_INPUT_TEXT . ' ' . $product->get('price')];
        }

      }
    }
I take it I can just add another few lines of code with the new text in but what sum do I use to show the price with the VAT includes assuming that I need to start from products_price. Is there anything else that I will need to add.

Hopefully some understand what I am trying to do, and can give a bit of advice.


Join The Code Co-op to get access to your library in the Code Co-op Forum
User avatar
burt
Core Team
Posts: 4561
Joined: Tue Oct 29, 2019 9:37 am
Phoenix Version: v1.1.0.8
Has thanked: 252 times
Been thanked: 422 times

Re: Need a heads on on how to add VAT tpo a products price in a bit of code

Post by burt »

I haven't tested, but this looks like it would do it;

Code: Select all

$parameters['contents'][] = ['text' => WHATEVER_TEXT . ' ' . $currencies->format(Tax::add($product->get('price'), Tax::get_rate($product->get('tax_class_id'))))];
Edit:

$product->get('tax_class_id')
This bit gets the products tax_class_id (ie what you select in the product addin/editing page

Tax::get_rate(
This bit gets the actual rate (in this case 20%, but may be different)

$product->get('price')
Is the price

Tax::add(
Makes the calculation from Tax::get_rate( and $product->get('price') and returns the relevant value

$currencies->format(
Makes it look pretty [eg £1.23 or $1.23 rather than the calculated 1.234]
I am not here to build for you.
I am here to build with you. Let's help each other.


Join The Code Co-op to get access to your library in the Code Co-op Forum
Post Reply