How to use multiple filtering options that will af

2019-09-24 06:00发布

问题:

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?

回答1:

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'] );
    }
}