Change text in PI module depending on customers country.

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

Change text in PI module depending on customers country.

Post by 14Steve14 »

This is for use on 1.8.0.20

I altered the PI Price module to show a bit of text under the products price explaining to customers that our prices included 20% VAT. The problem now is that a customer from outside the UK has complained that VAT has been charged on his order. When he logged in the price changed as it should but the bit of text under the price stayed the same, so I need a way to change the text that shows depending on the customers country.

The module code is very simple as I am not a coder and only contains the following which is mainly the standard price module

Code: Select all

<div class="col-sm-<?= $content_width ?> pi-price mt-0">
  <h2 class="list-group-item d-flex justify-content-between align-items-center">
    <?=$GLOBALS['product']->get('is_special')
    ? sprintf(PI_PRICE_DISPLAY_SPECIAL, $GLOBALS['product']->format(), $GLOBALS['product']->format('price'))
    : sprintf(PI_PRICE_DISPLAY, $GLOBALS['product']->format())
    ?></h2>
    <?php echo PI_PRICE_VAT_NOTICE_TEXT_UK; ?>
</div>
I have found different suggestions online that suggest to use the customers browser language, but is this the best way or change it once the customer has logged in and up until then assume that they are a UK customer as the store is in the UK.

I don't understand how to use IF ELSE statements so need a bit of help with that and also how best to find the customers country.


Join The Code Co-op to get access to your library in the Code Co-op Forum
heatherbell
Senior Contributor
Posts: 2540
Joined: Mon Oct 07, 2019 4:39 am
Phoenix Version:
Has thanked: 35 times
Been thanked: 243 times

Re: Change text in PI module depending on customers country.

Post by heatherbell »

Might be simpler to just change the text to, for example, "Prices include VAT (if applicable)"
That's worked for us for years no matter if visitor signed in or not.
User avatar
burt
Core Team
Posts: 4551
Joined: Tue Oct 29, 2019 9:37 am
Phoenix Version: v1.1.0.8
Has thanked: 252 times
Been thanked: 412 times

Re: Change text in PI module depending on customers country.

Post by burt »

If/Else is very simple, in essence, it's like a lightswitch.
If a light switch is on, you get light ( assuming you've paid your electricity bill :D ), if the switch is off you don't.

Code: Select all

IF (something = true)
  do THIS
ELSE
  do THAT
To see if a customer is logged in while looking at a page;

Code: Select all

if ( isset($_SESSION['customer_id']) ) {
  // logged in
}
else {
  // not  
}
If you have a logged in customer you can get some details about him/her from the $customer object.
To see what country a customer is in;

Code: Select all

$customer->get('iso_code_2')
iso_code_2 is good as you know it will never change. Never use the country name (as country names do change, not often but it is a gotcha). Never use the country ID (especially if the code is shared to others) as (eg) GB in your database might be ID 222, but in someone elses database might be 223 or 1 or 56 (or whatever).

Putting that all together;

Code: Select all

if ( isset($_SESSION['customer_id']) ) {
  // is logged in
  if ( 'GB' == $customer->get('iso_code_2') ) {
    // is in GB     
  }
  else {
    // is not in GB
  }
}
else {
  // not logged in  
}
If you want to extend this out to multiple countries;

Code: Select all

if ( isset($_SESSION['customer_id']) ) {
  // is logged in
  $country_array = ['GB', 'BE', 'DE'];
  if ( in_array($customer->get('iso_code_2'), $country_array) ) {
    // is in GB or Belgium or Germany    
  }
  else {
    // is elsewhere in the world
  }
}
else {
  // not logged in  
}
Note: none of the above is tested, but it looks about right.
And if/else can be a lot more complicated than this, or a lot more simpler. In general I would advise to just stick with;

Code: Select all

if ( isset($_SESSION['customer_id']) ) {
  // logged in
}
else {
  // not  
}
as it's easy to remember, and can't really go wrong.
User avatar
burt
Core Team
Posts: 4551
Joined: Tue Oct 29, 2019 9:37 am
Phoenix Version: v1.1.0.8
Has thanked: 252 times
Been thanked: 412 times

Re: Change text in PI module depending on customers country.

Post by burt »

There are different examples of if/else structures from live code, which is written in a more "codey" way, but does the same thing as a longer written if/else;

https://github.com/CE-PhoenixCart/Phoen ... ct.php#L22

Written out the long way would be;

Code: Select all

if ( empty($_POST['qty']) ) {
  $qty = 1;
}
else {
  $qty = (int)$_POST['qty'];
}
And another;

https://github.com/CE-PhoenixCart/Phoen ... hp#L27-L36

In the if/else way this would be

Code: Select all

if ( 'm' == $customer->get('gender') ) {
  $gdpr_gender = MODULE_CONTENT_GDPR_PERSONAL_DETAILS_GENDER_M;
}
elseif (  'f' == $customer->get('gender') ) {
  $gdpr_gender = MODULE_CONTENT_GDPR_PERSONAL_DETAILS_GENDER_F;  
}
else {
  $gdpr_gender = MODULE_CONTENT_GDPR_PERSONAL_DETAILS_UNKNOWN; 
}
Anyway, ignore this post as it's irrelevant to your needs, but may be relevant in the future for someone elses.
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Change text in PI module depending on customers country.

Post by ecartz »

Code: Select all

<div class="col-sm-<?= $content_width ?> pi-price mt-0">
  <h2 class="list-group-item d-flex justify-content-between align-items-center">
    <?=$GLOBALS['product']->get('is_special')
    ? sprintf(PI_PRICE_DISPLAY_SPECIAL, $GLOBALS['product']->format(), $GLOBALS['product']->format('price'))
    : sprintf(PI_PRICE_DISPLAY, $GLOBALS['product']->format())
    ?></h2>
    <?= ($GLOBALS['product']->get('tax_rate') == 0) ? '' : PI_PRICE_VAT_NOTICE_TEXT_UK ?>
</div>
And as Robert already suggested, adding (where applicable) would help for the not-logged-in case. Alternately, consider "Price includes applicable taxes (e.g. VAT in UK)." That's the most precise description of the logic that covers all cases. If there's an applicable tax, it's included in the price.

Logic: if the tax rate is zero, hide the message. If the tax rate is non-zero, show the message.

You probably don't need to check if logged in, as the tax rate calculation already does that.
raiwa
Certified Developer
Posts: 1640
Joined: Sat Dec 21, 2019 8:08 am
Phoenix Version: 1.1.0.6
Has thanked: 70 times
Been thanked: 152 times

Re: Change text in PI module depending on customers country.

Post by raiwa »

My Tax info addon should just do what you need:
app.php/addons/free_addon/prices_with_tax_info
Public Phoenix Change Log Cheat Set on Google Sheets
https://docs.google.com/spreadsheets/d/ ... sp=sharing

Need Help?viewtopic.php?f=10&t=27
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

Re: Change text in PI module depending on customers country.

Post by 14Steve14 »

raiwa wrote: Fri Jan 19, 2024 11:30 am My Tax info addon should just do what you need:
app.php/addons/free_addon/prices_with_tax_info
We tried using that but it also showed prices with and without taxes on all the other prices that we have on the product page including things like the recommended retail price, savings when showing discounts and it just made the page look all wrong and complicated so we uninstalled it and added the little bit of text.


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