2017-07-24 31 views
2

我正尝试更新自定义字段更改的运费总成本。我的jQuery代码工作正常,但我的功能wocommerce挂钩没有调用。如何在购物车中添加运输费用更改自定义字段的选择框

我使用这个jQuery代码:

<script> 
jQuery(document).ready(function($) { 

    jQuery('#myfield2').change(function(){ 
     var data = { 
      action: 'wp_ajax_woocommerce_cart_calculate_fees', 
      state: '200', 
      vals: jQuery(this).val() 
     }; 
     jQuery.ajax({ 
      type: 'POST', 
      url: wc_checkout_params.ajax_url, 
      data: data, 
      success: function (code) { 
       console.log(code); 
       jQuery('body').trigger('update_checkout'); 
      }, 
      dataType: 'html' 
     }); 

    }); 
}); 
</script> 

PHP代码:

add_action('wp_ajax_woocommerce_cart_calculate_fees','endo_handling_fee'); 
function endo_handling_fee() { 
    global $woocommerce; 
    $post_data="--"; 
    if (isset($_POST)) { 
     parse_str($_POST['vals'], $post_data); 
    } 
    print_r($post_data); 
    /* if (is_admin() && ! defined('DOING_AJAX')) 
      return;*/ 
    //$vals=$_POST['vals']; 
    $fee = 10.00; 
     if($vals=='Yes') 
    { 
     $fee = 50.00; 
    } 


    $woocommerce->cart->add_fee('Handling', $fee, true, 'standard'); 
} 

任何帮助表示赞赏。

+0

你得到任何错误? – Und3rTow

回答

0

增加一个线以下行后 -

add_action('wp_ajax_woocommerce_cart_calculate_fees','endo_handling_fee'); 

线添加 -

add_action('wp_ajax_nopriv_woocommerce_cart_calculate_fees','endo_handling_fee'); 
相关问题