I have set prices 7,09 and in cart it shows 7,00.
How I can remove this rounding? I have custom variation price field.
My code:
woocommerce_wp_text_input(
array(
'id' => '_number_field[' . $variation->ID . ']',
'label' => __( 'Aluse hind', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Sisesta aluse hind.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_number_field', true ),
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
add_filter('woocommerce_product_variation_get_price', 'custom_product_get_price', 10, 2 );
add_filter('woocommerce_show_variation_price', function() { return TRUE;});
function custom_product_get_price( $price, $product ){
if (!empty(get_post_meta( $product->get_id(), '_number_field', true))) {
return get_post_meta( $product->get_id(), '_number_field', true);
} else {
return get_post_meta( $product->get_id(), '_price', true);
}
}
2
Answers
As you can see here, looks like it will always be a small change in the zecimals
It is not a rounding problem. You’re just passing a string as a price and the float conversion truncates the decimals.
If the value of the custom field
_number_field
uses a comma it must be converted to a numeric value (float), replacing the comma with the decimal point.In your log file you will also find the notice:
Notice: A non well formed numeric value encountered
.Furthermore, the
woocommerce_product_variation_get_price
hook already returns the meta_price
of the product variation so the else declaration is not necessary.You can optimize the
custom_product_get_price
function like this:The code has been tested and works.