如果在WooCommerce Checkout中选中了“自定义”复选框,则删除运输成本

如果在WooCommerce Checkout中选中了“自定义”复选框,则删除运输成本,第1张

如果在WooCommerce Checkout中选中了“自定义”复选框,则删除运输成本

要在结帐页面上的自定义javascript事件上通过ajax更改运输方式的费用,要比有条件地更改费用或其他任何事情要复杂得多,这很安静。

为了存储ajax数据,我使用了WC_Sessions并 更改了运输方式的成本,* 只有在 您 *** 纵运输时段数据的情况下, 才起作用 *

完整的工作代码:

// Add a Custom checkbox field for shipping options (just for testing)add_action( 'woocommerce_after_checkout_billing_form', 'custom_billing_checkbox_for_testing', 10, 1 );function custom_billing_checkbox_for_testing( $checkout ) {    $field_id = 'billing_ups_yn';    // Get the checked state if exist    $billing_ups = WC()->session->get('billing_ups' );    if(empty($billing_ups))        $billing_ups = $checkout->get_value( $field_id );    // Add the custom checkout field (checkbox)    woocommerce_form_field( $field_id, array(        'type' => 'checkbox',        'class' => array( 'form-row-wide' ),        'label' => __('Billing UPS'),    ), $billing_ups );}// function that gets the Ajax dataadd_action( 'wp_ajax_woo_get_ajax_data', 'woo_get_ajax_data' );add_action( 'wp_ajax_nopriv_woo_get_ajax_data', 'woo_get_ajax_data' );function woo_get_ajax_data() {    if ( $_POST['billing_ups'] == '1' ){        WC()->session->set('billing_ups', '1' );    } else {        WC()->session->set('billing_ups', '0' );    }    echo json_enpre( WC()->session->get('billing_ups' ) );    die(); // Alway at the end (to avoid server error 500)}// Conditionally changing the shipping methods costsadd_filter( 'woocommerce_package_rates','conditional_custom_shipping_cost', 90, 2 );function conditional_custom_shipping_cost( $rates, $package ) {    if ( WC()->session->get('billing_ups' ) == '1' ){        foreach ( $rates as $rate_key => $rate_values ) { // Not for "Free Shipping method" (all others only) if ( 'free_shipping' !== $rate_values->method_id ) {     // Set the rate cost     $rates[$rate_key]->cost = 0;     // Set taxes rate cost (if enabled)     $taxes = array();     foreach ($rates[$rate_key]->taxes as $key => $tax)         if( $rates[$rate_key]->taxes[$key] > 0 ) // set the new tax cost  $taxes[$key] = 0;     $rates[$rate_key]->taxes = $taxes; }        }    }    return $rates;}// Enabling, disabling and refreshing session shipping methods dataadd_action( 'woocommerce_checkout_update_order_review', 'refresh_shipping_methods', 10, 1 );function refresh_shipping_methods( $post_data ){    $bool = true;    if ( WC()->session->get('billing_ups' ) == '1' ) $bool = false;    // Mandatory to make it work with shipping methods    foreach ( WC()->cart->get_shipping_packages() as $package_key => $package ){        WC()->session->set( 'shipping_for_package_' . $package_key, $bool );    }    WC()->cart->calculate_shipping();}// The Jquery scriptadd_action( 'wp_footer', 'custom_checkout_script' );function custom_checkout_script() {    ?>    <script type="text/javascript">        jQuery( function($){ // update cart on delivery location checkbox option $('#billing_ups_yn_field input').change( function () {     var checked = 0;     if ( $('#billing_ups_yn').is(':checked') )         checked = 1;     $.ajax({         type: 'POST',         url: wc_checkout_params.ajax_url,         data: {  'action': 'woo_get_ajax_data',  'billing_ups': checked,         },         success: function (result) {  $('body').trigger('update_checkout');  console.log('response: '+result); // just for testing         },         error: function(error){  console.log(error); // just for testing         }     }); });        });    </script>    <?php}

代码进入您的活动子主题(或活动主题)的function.php文件中。

经过测试和工作。


与您的评论有关的更新:

如果希望始终在加载时始终取消选中此复选框,则将第一个功能替换为该功能:

// Add a Custom checkbox field for shipping options (just for testing)add_action( 'woocommerce_after_checkout_billing_form', 'custom_billing_checkbox_for_testing', 10, 1 );function custom_billing_checkbox_for_testing( $checkout ) {    $field_id = 'billing_ups_yn';    // Add the custom checkout field (checkbox)    woocommerce_form_field( $field_id, array(        'type' => 'checkbox',        'class' => array( 'form-row-wide' ),        'label' => __('Billing UPS'),    ), '' );}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5089121.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-16
下一篇 2022-11-16

发表评论

登录后才能评论

评论列表(0条)

保存