Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Price 0 / Zero / empty / php error in line 68 #5

Open
lukasprelovsky opened this issue Oct 15, 2023 · 11 comments
Open

Price 0 / Zero / empty / php error in line 68 #5

lukasprelovsky opened this issue Oct 15, 2023 · 11 comments

Comments

@lukasprelovsky
Copy link

Hi,

I use your module and ran into a minor problem.

I use the conversion of euros into Czech currency and subtract the price from the price of the product using the code:

<?php $price = EuroFxRef::convert( get_post_meta( get_the_ID(), '_price', true ), 'EUR', 'CZK' ); ?> <?php echo round( $price, 0 ); ?> Kč

the problem arises with a product with a price of 0, when instead of displaying an empty value, i.e. nothing, a PHP error is displayed

Warning: A non-numeric value encountered in /data/1/d/1xyz/test.com/web/wp-content/plugins/euro-fxref-currency-converter/eurofxref.php on line 68

zero price problem

would it be possible to modify the logic so that the php error is displayed only to the site administrator and not to the users?

or treat the value 0 - zero so that the code does not appear at all?

Thanks

lukasprelovsky added a commit to lukasprelovsky/wp_eurofxref that referenced this issue Oct 15, 2023
Add fce  

if ($amount == 0) return 0;

to line 55

to resolve my problem: 

joostdekeijzer#5
@lukasprelovsky
Copy link
Author

lukasprelovsky commented Oct 15, 2023

Hi,

Add fce

if ($amount == 0) return 0;

to line 55

to resolve my problem.

I send pool request

#6

to module improvement :)

@joostdekeijzer
Copy link
Owner

Hi Luka,

Thanks for your report and PR. But... I'm having a hard time replicating.

Is it possible the issue is in your code? I'm wondering what the output of get_post_meta( get_the_ID(), '_price', true ) is. Knowing meta_data it might be an empty value or a string zero ('0'). You might want to cast your get_post_meta to float and test again?

@lukasprelovsky
Copy link
Author

Hi,

get_post_meta( get_the_ID(), '_price', true )

is the price of the product from woocommerce....

If the woocommerce product does not have a price (empty price) (the shop admin forgot to fill it in or has a product without a price filled in)

the php error on line 58 will appear

Warning: A non-numeric value encountered

My pull request fixes this issue and instead of showing a php error it shows a value of 0

I use example:

i add this code to:
woocommerce_after_shop_loop_item_title

1

2

@lukasprelovsky
Copy link
Author

example of an incomplete price on a product and the following php error

0

@joostdekeijzer
Copy link
Owner

Hi Lukáš,

I understand your PR fixes your issue but handling all kinds of unexpected input is not the correct way to develop.

The convert method expects a float value as input and nothing else.

In your example you best rewrite:

$price = get_post_meta( get_the_ID(), '_price', true );
if ( is_numeric( $price ) ) {
  $price = EuroFxRef::convert( $price, 'EUR', 'CZK' );
}
// etc.

You can also just do
echo do_shortcode( sprintf( '[currency amount="%f" from="EUR" to="CZK"]', get_post_meta( get_the_ID(), '_price', true ) ) );

and have the shortcode sort it all out 😄

@joostdekeijzer
Copy link
Owner

Oops, I see that the shortcode defaults to amount=1 so that might not work for you. You'll need to validate and check the price value and act accordingly...

@lukasprelovsky
Copy link
Author

it would be good to figure it out somehow, because this way of converting the woocommerce price rate is perfect.

However, I don't want to show users a php error.

the problem is when the price of the product is 0 or not filled in...

have you not come across this usage in woocommerce?

wouldn't it be a solution to apply the condition that if the value is less than 1, the result of the code will not appear at all?

@joostdekeijzer
Copy link
Owner

Did you test echo do_shortcode( sprintf( '[currency amount="%f" from="EUR" to="CZK"]', get_post_meta( get_the_ID(), '_price', true ) ) ); ?

I see that with sprintf %f the output is always a float, when fed an empty string it becomes [currency amount="0.000000" from="EUR" to="CZK"].

@joostdekeijzer
Copy link
Owner

But I now see another issue...

My plugin expects the converted amount to be > 0. I don't know why anymore... See lines 114 and 132 and some more.

With this rule the shortcode [currency amount="0" from="EUR" to="CZK"] will output "€ 0,=" and not "0 CZK" which would be more logical, I guess.

I'll want to think about how to fix this because it will change the current working and I don't want to confuse current users.

@lukasprelovsky
Copy link
Author

ok, no problem :) we'll definitely figure it out somehow.

@joostdekeijzer
Copy link
Owner

For your current implementation I would suggest you use my longer example above, use floatval or cast to float.

$price = EuroFxRef::convert( (float) get_post_meta( get_the_ID(), '_price', true ) , 'EUR', 'CZK' ); should work fine (but please test 😄 )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants