In Woocommerce, I would like update base price depending on 2 selected custom fields.
With the help of LoicTheAztec I can update price of 1 custom field, so, how do a update 2 custom fields?
PHP code:
add_action( 'woocommerce_before_add_to_cart_button', 'custom_product_field' );
function custom_product_field() {
global $product;
if( $product->is_type('variable') ) return;
$options = array(
"" => __('Tipos'),
"20.00" => "Tipo 1 + 20,00",
"25.00" => "Tipo 2 + 25,00",
);
woocommerce_form_field('amostra', array(
'type' => 'select',
'class' => array('my-field-class form-row-wide'),
'label' => __('Tipos', $domain),
'required' => true,
'options' => $options,
),'');
$options_ra = array(
"" => __('Sem Modelos'),
"15.00" => "Modelo A + 15,00",
"25.00" => "Modelo B + 25,00",
);
woocommerce_form_field('amostraB', array(
'type' => 'select',
'class' => array('my-field-class_ra form-row-wide'),
'label' => __('Modelos', $domain),
'required' => true,
'options' => $options_ra,
),'');
$base_price = (float) wc_get_price_to_display( $product );
$prices = array(
'' => wc_price($base_price),
'20.00' => wc_price($base_price + 20),
'25.00' => wc_price($base_price + 25),
'15.00' => wc_price($base_price + 15),
'25.00' => wc_price($base_price + 25),
);
}
JS code:
jQuery(function($){
var a = <?php echo json_encode($prices); ?>,
b = 'p.price',
c = 'select[name="amostra"]';
$(c).on( 'change', function(){
$.each( a, function( key, value ){
if( $(c).val() == key )
$(b).html(value);
});
});
});
Any help please.
This is a bit much more complicated as the 2 select fields can interact on the calculated price at the same time and of because this case, this need to be handled differently.
I have added some custom formatting price functions, to be able to achieve that… Here is the code:
Code goes in function.php file of your active child theme (or active theme). Tested and works.