Skip to content

Conversation

@YordanSoares
Copy link
Contributor

All Submissions:

Changes proposed in this Pull Request:

This PR link the price decimal separator to the product weight and dimensions decimal separator. Since a decimal separator is used to separate an integer, no matter if this is used for a price, weight, dimension, etc., it has sense to have a consistence in the whole site using this, settings for weight and dimensions too.

If, for some reason, a user would need to use different decimal separators for prices and weight/dimensions in the admin dashboard or front-end, they can use the following filter hooks:

  • woocommerce_format_localized_decimal
  • woocommerce_format_weight
  • woocommerce_format_dimensions

Please note that this only modify the display, not the storage of the data that remains using the dot (.) as decimal separator.

This change will benefit many users, considering that most countries use a comma as decimal separator than a dot.

Closes #25864.

How to test the changes in this Pull Request:

  1. Set your thousand separator as a dot (.) and decimal separator as a comma (,):
    image
  2. Edit a product or create a new, and try to add a dot as decimal in the weight and any of dimensions fields (you shouldn't be able to):
    image
  3. Save your changes and check if they're still using the comma as separator:
    image
  4. Set your thousand separator as a comma (,) and decimal separator as a dot (.) and check if the weight and dimensions values follows your new settings:
    image
  5. Now, the warning will appear if you try to use a comma instead:
    image

Other information:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully run tests with your changes locally?

@github-actions github-actions bot added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label Apr 23, 2022
@vedanshujain vedanshujain requested a review from Konamiman April 29, 2022 10:25
@Konamiman Konamiman merged commit fbd38e5 into woocommerce:trunk May 2, 2022
@github-actions github-actions bot added this to the 6.6.0 milestone May 2, 2022
@github-actions
Copy link
Contributor

github-actions bot commented May 2, 2022

Hi @Konamiman, thanks for merging this pull request. Please take a look at these follow-up tasks you may need to perform:

  • Add the release: add testing instructions label

@imagocreative
Copy link

imagocreative commented Jun 20, 2022

I need to reset the separator (.) For the size in the backend without changing the decimal separator on the frontend (,). How can I do?

Copy link

@imagocreative imagocreative left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to reset the separator (.) For the size in the backend without changing the decimal separator on the frontend (,). How can I do?

@Konamiman
Copy link
Contributor

@imagocreative
Copy link

Hi @imagocreative, I think you could use the formatted_woocommerce_price filter for that: https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce/includes/wc-formatting-functions.php#L595-L605

I've seen this before but can't figure out how to make it work

@Konamiman
Copy link
Contributor

Here's a possible way to use it. Assume you have configured the decimal separator as . in WooCommerce settings, if you want to display it as , in the shop instead:

add_filter( 'formatted_woocommerce_price', function( $formatted_price, $price, $decimals, $decimal_separator ) {
	if( !is_admin() ) {
		$formatted_price = str_replace( $decimal_separator, ',', $formatted_price );
	}
	return $formatted_price;
}, 10, 4 );

You can add this code by using the code snippets plugin.

@imagocreative
Copy link

Here's a possible way to use it. Assume you have configured the decimal separator as . in WooCommerce settings, if you want to display it as , in the shop instead:

add_filter( 'formatted_woocommerce_price', function( $formatted_price, $price, $decimals, $decimal_separator ) {
	if( !is_admin() ) {
		$formatted_price = str_replace( $decimal_separator, ',', $formatted_price );
	}
	return $formatted_price;
}, 10, 4 );

You can add this code by using the code snippets plugin.

The problem with this code is that the thousands separator also changes.
I need to have the price 1.000,00, instead I have 1,000,00

@Konamiman
Copy link
Contributor

@imagocreative Hum... so you need to replace only the last occurrence of the separator.

What about this then:

add_filter( 'formatted_woocommerce_price', function( $formatted_price, $price, $decimals, $decimal_separator ) {
	if( !is_admin() ) {
		$pos = strrpos( $formatted_price, $decimal_separator );
		if($pos !== false) {
			$formatted_price = substr_replace( $formatted_price, ',', $pos, strlen($decimal_separator) );
		}
	}
	return $formatted_price;
}, 10, 4 );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

plugin: woocommerce Issues related to the WooCommerce Core plugin.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Product weight does not accept "4,5" values although product price does

3 participants