WooCommerce: custom variation price input not save value correctly

I’m using these two functions to show and save a custom price for my product variations:

add_action(‘woocommerce_variation_options_pricing’, function($loop, $variation_data, $variation) {
woocommerce_wp_text_input(array(
‘id’ => ‘preco_revendedor’,
‘wrapper_class’ => ‘form-row form-row-first’,
‘label’ => __( ‘Preço revendedor’, ‘textdomain’ ),
‘value’ => get_post_meta($variation->ID, ‘preco_revendedor’, true),
‘data_type’ => ‘price’,
)
);
}, 10, 3);

add_action(‘woocommerce_save_product_variation’, function($variation_id, $i) {
$preco_revendedor = $_POST[‘preco_revendedor’][$i];
if (!empty($preco_revendedor)) {
update_post_meta($variation_id, ‘preco_revendedor’, esc_attr($preco_revendedor));
} else {
delete_post_meta($variation_id, ‘preco_revendedor’);
}
}, 10, 2);

The problem is: it only save the first number. If I type 20, it save 2. If I type 23, it save 2.

Read more here:: WooCommerce: custom variation price input not save value correctly

Leave a Reply

Your email address will not be published. Required fields are marked *