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

Use unformatted price in edit page #9644

Open
xoex opened this issue May 2, 2023 · 1 comment
Open

Use unformatted price in edit page #9644

xoex opened this issue May 2, 2023 · 1 comment
Labels
type-bug workflow-needs-replication workflow-pending-triage Items that need to be triaged and reviewed before they are worked on.

Comments

@xoex
Copy link

xoex commented May 2, 2023

Bug Report

Expected behavior

When I want to edit a download custom type, price field is formatted and this creates a problem.
For my currency (for display purposes only) I divide amount by 10,

add_filter('edd_format_amount', function ($formatted, $amount, $decimals, $decimal_separator, $thousands_separator, $code) {
    if ($code === 'TMN')
    {
		return number_format( ((float) $amount) / 10, 0, $decimal_separator, $thousands_separator );
    }
    return $formatted;
} , 10 , 6);

This means that if price is 100, I display 10. But you can guess what happens when you edit a download post. Price that is actually 100 will be formatted to 10, and When I save the page, price will be saved as 10 and displays as 1.
Problem is in metabox.php file,

			$price_args = array(
				'name'  => 'edd_price',
				'id'    => 'edd_price',
				'value' => isset( $price ) ? esc_attr( edd_format_amount( $price ) ) : '',
				'class' => 'edd-form-group__input edd-price-field',
			);
			if ( 'before' === $currency_position ) {
				?>
				<span class="edd-amount-control__currency is-before"><?php echo esc_html( edd_currency_filter( '' ) ); ?></span>
				<?php
				echo EDD()->html->text( $price_args );
			} else {
				echo EDD()->html->text( $price_args );
				?>
				<span class="edd-amount-control__currency is-after"><?php echo esc_html( edd_currency_filter( '' ) ); ?></span>
				<?php
			}

First there's no filter or anything that I can override the price, And also it shouldn't use edd_format_amount for price, in fact you can see this in their definition :

 * Format a numeric value.
 *
 * Uses the decimal & thousands separator settings, and the number of decimals,
 * to format any numeric value.
 *
 * (Most commonly, this is used to apply site or user preferences to a numeric
 * value for output to the page.)

You should use edd_sanitize_amount function that doesn't put any separator in input box.

* Sanitize a numeric value.
 *
 * Use this function to "unformat" a previously formatted numeric value.
 *
 * (Most commonly, this is when accepting input from a form field where the
 * value is likely to derived from the site or user preferences.)
@xoex xoex added type-bug workflow-needs-replication workflow-pending-triage Items that need to be triaged and reviewed before they are worked on. labels May 2, 2023
@robincornett
Copy link
Contributor

@xoex thank you for the report, research, and proposed fix. I've copied the information over to the repository where we are working now and will hope to have an update/fix in a future release.

You may be able to work around it for the time being by returning $formatted early if is_admin() is true and edd_doing_ajax() is not true (both would be needed as ajax calls are considered admin).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug workflow-needs-replication workflow-pending-triage Items that need to be triaged and reviewed before they are worked on.
Projects
None yet
Development

No branches or pull requests

2 participants