-2

I am using "Add a select field that will change price in Woocommerce simple products" answer code. It only uses a single selection field for filtering. I want to extend this code using jQuery to be able to use multiple fields for filtering.

Example: 2 select fields, one text, a checkbox and some radio buttons that will all determine the final calculated price.

// Select field
woocommerce_form_field('materials_pack', array(
    'type'          => 'select',
    'class'         => array('material-field form-row-wide'),
    'label'         => __('Select Materials:', $domain),
    'required'      => true,
    'options'       => $options,
),'');   

<script>
jQuery(function($){
    var a = <?php echo json_encode($prices); ?>,
        b = 'p.price',
        c = 'select[name="materials_pack"]';

    $(c).on( 'change', function(){
        $.each( a, function( key, value ){
            if( $(c).val() == key )
                $(b).html(value);
        });
    });
});
</script>

How to use it for multi fields? and how do the jquery script?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Amir
  • 1
  • 2
  • Thank you Mena for edited. Can we invite LoicTheAztec from this posts? https://stackoverflow.com/questions/49862061/add-a-select-field-that-will-change-price-in-woocommerce-simple-products – Amir May 14 '19 at 09:01
  • I just posted a comment on his answer. – Mena May 14 '19 at 10:13
  • My answer code just works for one defines field. to extend that to multiple fields of different types is a real development. Better use [existing related plugins](https://www.google.fr/search?q=woocommerce+product+options) that allow that. – LoicTheAztec May 14 '19 at 15:27
  • Please if you can help me i don't want to use plugins i hate plugins i create a complete theme without plugins. – Amir May 15 '19 at 05:30

1 Answers1

0

Ok i found my answer here now i just need to how to do it in this code?

add_action('woocommerce_before_calculate_totals', 'set_cutom_cart_item_price', 20, 1);

foreach (  $cart->get_cart() as $cart_item ) {
    if ( isset( $cart_item['material_data']['new_price'] ) ){
        $cart_item['data']->set_price( $cart_item['material_data']['new_price'] );
    }
}
Amir
  • 1
  • 2
  • here is multi prices coming from 2 select files how to add it to cart? both prices? – Amir May 20 '19 at 05:56