Jump to content

  • You cannot start a new topic
  • You cannot reply to this topic

Override Fn_Format_Price Function Rate Topic   - - - - -

 
  • sddev
  • Member
  • Trial users
  • Join Date: 05-Dec 18
  • 17 posts

Posted 04 April 2020 - 12:50 AM #1

Hello, I need to round the price before it gets displayed. The issue is I cant create an addon to override fn_format_price function in the fn.common.php file. It worked when I directly edit the function in the file, but I don't wanna edit core files. What's the solution?

I tried creating an addon and install it but didn't work

init.php:

fn_register_hooks(
'fn_format_price'
);

 

 

func.php


function fn_round_prices_fn_format_price($price = 0, $currency .....
 

 



 
  • soft-solid
  • Junior Member
  • Authorized Reseller
  • Join Date: 19-Apr 10
  • 1131 posts

Posted 04 April 2020 - 10:13 AM #2

Hello

 

In function fn_format_price you have only 1 hook "format_price_pre" and you can refer to this hook (no hook post what could be the problem)

 

The name function should have name fn_"addon_id"_format_price_pre.

 

If you want change value in hooks you should precede the variable char "&"

 

e.g.

function  fn_"addon_id"_format_price_pre(&$price, $currency ......

 

Best regards

Robert

 

 


Team of SoftSolid
cs-cart.pl, marketplace

We specialize in creating the highest quality addons, safe, useful and flexible


 
  • tbirnseth
  • CS Cart Expert
  • Authorized Reseller
  • Join Date: 08-Nov 08
  • 11561 posts

Posted 04 April 2020 - 06:53 PM #3

Yes you can use 'format_price_pre' hook like:

function fn_my_changes_format_price_pre(&$price, $currency) {
  // if you want currency dependent
  if( $currency == 'usd' ) {
    // do whatever
  }
  // to round up
  $price = ceil($price);
  // to round down
  $price = floor($price);
  // Or you can do custom rounding here.
  $price = my_custom_price_adjustment($price, $currency);
}

I had to do this for a Dutch customer given they don't have 'pennies' so needed to round to appropriate 'nickle'.


EZ Merchant Solutions: Custom (USA based) B2B Development, Consulting, Development and Special Projects (get a quote here).
Commercial addons, payment methods and modifications to meet your business and operations needs.


 
  • sddev
  • Member
  • Trial users
  • Join Date: 05-Dec 18
  • 17 posts

Posted 04 April 2020 - 10:00 PM #4

Thanks a lot, my issues was the & sign



 
  • soft-solid
  • Junior Member
  • Authorized Reseller
  • Join Date: 19-Apr 10
  • 1131 posts

Posted 05 April 2020 - 08:43 AM #5

Hello

 

You are welcome :)

 

Best regards

Robert


Team of SoftSolid
cs-cart.pl, marketplace

We specialize in creating the highest quality addons, safe, useful and flexible


 
  • sddev
  • Member
  • Trial users
  • Join Date: 05-Dec 18
  • 17 posts

Posted 05 April 2020 - 09:33 PM #6

one more thing, I have two currencies, when I need to round my currency, the $currency value in the format_price_pre function is showing a blank value ' '.
why is that?



 
  • tbirnseth
  • CS Cart Expert
  • Authorized Reseller
  • Join Date: 08-Nov 08
  • 11561 posts

Posted 06 April 2020 - 01:10 AM #7

Because some cs-cart functions do not pass the currency.  In that case you should do something like:

if( empty($currency) )
  $currency = CART_PRIMARY_CURRENCY;

EZ Merchant Solutions: Custom (USA based) B2B Development, Consulting, Development and Special Projects (get a quote here).
Commercial addons, payment methods and modifications to meet your business and operations needs.


 
  • sddev
  • Member
  • Trial users
  • Join Date: 05-Dec 18
  • 17 posts

Posted 06 April 2020 - 01:58 PM #8

Now we only have two currencies, and I'm rounding the secondary currency. But later if we had more currencies, I wanna round only one of them. How can I get which currency is being passed to the format_price_pre function?



 
  • tbirnseth
  • CS Cart Expert
  • Authorized Reseller
  • Join Date: 08-Nov 08
  • 11561 posts

Posted 06 April 2020 - 03:24 PM #9

It is in the currency parameter.  If it's empty, it is using the primary currency.  if not, it is set to the current customer currency.


EZ Merchant Solutions: Custom (USA based) B2B Development, Consulting, Development and Special Projects (get a quote here).
Commercial addons, payment methods and modifications to meet your business and operations needs.


 
  • eComLabs
  • CS-Cart Expert
  • Authorized Reseller
  • Join Date: 27-Jan 14
  • 20539 posts

Posted 07 April 2020 - 06:56 AM #10

Now we only have two currencies, and I'm rounding the secondary currency. But later if we had more currencies, I wanna round only one of them. How can I get which currency is being passed to the format_price_pre function?

 

Try the following condition

if ($currency == CART_PRIMARY_CURRENCY) {
   ...code for primary currency only....
}

GET A FREE QUOTE | CS-Cart Add-ons | CS-Cart Licenses | CS-Cart Development | CS-Cart Design | Server Configuration | UniTheme and YOUPI
CS-Cart                USD 345     Multi-Vendor              USD 1250    Multi-Vendor PLUS           USD 3100 (2775)
CS-Cart Ultimate  USD 775     CS-Cart + YOUPI      USD 545      Multi-Vendor Ultimate       USD 7500 (6000)

 
  • tbirnseth
  • CS Cart Expert
  • Authorized Reseller
  • Join Date: 08-Nov 08
  • 11561 posts

Posted 07 April 2020 - 07:41 PM #11

Ecom, that won't work for the condition where the $currency value is empty (which it many times is).  Hence the conditional statement would need to be something akin to:

if( empty($currency) || $currency == CART_PRIMARY_CURRENCY )

EZ Merchant Solutions: Custom (USA based) B2B Development, Consulting, Development and Special Projects (get a quote here).
Commercial addons, payment methods and modifications to meet your business and operations needs.


 
  • eComLabs
  • CS-Cart Expert
  • Authorized Reseller
  • Join Date: 27-Jan 14
  • 20539 posts

Posted 08 April 2020 - 06:26 AM #12

 

Ecom, that won't work for the condition where the $currency value is empty (which it many times is).  Hence the conditional statement would need to be something akin to:

if( empty($currency) || $currency == CART_PRIMARY_CURRENCY )

 

Look like it cannot be empty since it has default value ( CART_PRIMARY_CURRENCY ) 


GET A FREE QUOTE | CS-Cart Add-ons | CS-Cart Licenses | CS-Cart Development | CS-Cart Design | Server Configuration | UniTheme and YOUPI
CS-Cart                USD 345     Multi-Vendor              USD 1250    Multi-Vendor PLUS           USD 3100 (2775)
CS-Cart Ultimate  USD 775     CS-Cart + YOUPI      USD 545      Multi-Vendor Ultimate       USD 7500 (6000)

 
  • tbirnseth
  • CS Cart Expert
  • Authorized Reseller
  • Join Date: 08-Nov 08
  • 11561 posts

Posted 08 April 2020 - 06:22 PM #13

That must be a recent change then....


EZ Merchant Solutions: Custom (USA based) B2B Development, Consulting, Development and Special Projects (get a quote here).
Commercial addons, payment methods and modifications to meet your business and operations needs.


 
  • sddev
  • Member
  • Trial users
  • Join Date: 05-Dec 18
  • 17 posts

Posted 18 April 2020 - 02:32 PM #14

In my case, the price I'm rounding is not primary, and yet the currency in the function gives an empty currency, why?



 
  • tbirnseth
  • CS Cart Expert
  • Authorized Reseller
  • Join Date: 08-Nov 08
  • 11561 posts

Posted 18 April 2020 - 08:59 PM #15

In my case, the price I'm rounding is not primary, and yet the currency in the function gives an empty currency, why?

As stated above, on older versions, the currency parameter had a default value of '' versus now it's CART_PRIMARY_CURRENCY.

If it's empty (I.e. "") then you can assume the current call is using the CART_PRIMARY_CURRENCY.  See #11 above.


EZ Merchant Solutions: Custom (USA based) B2B Development, Consulting, Development and Special Projects (get a quote here).
Commercial addons, payment methods and modifications to meet your business and operations needs.