php-根据WooCommerce结帐字段值显示/隐藏送货方式

在WooCommerce中,我设置了不同的运输方式,但特别是公司必须专有的一种.

为此,我使用以下代码:

add_filter( 'woocommerce_package_rates',array( $this,'package_rates' ),10,2 );

public function package_rates( $rates,$package ) {
    $company_rate_id = 'flat_rate:7';

    if(!empty(WC()->customer->get_billing_company())){
        $company_rates = $rates[ $company_rate_id ];
        $rates = array( $company_rate_id => $company_rates );
    }else{
        unset( $rates[ $company_rate_id ] );
    }

    return $rates;
}

该解决方案有效,但前提是开票公司已经存在并保存在数据库中.因此,如果客户在结帐页面上更新此信息,则该信息将无效.

一种可能的解决方案是将该字段实时保存(billing_company).

我尝试了以下功能:

add_filter( 'woocommerce_checkout_fields','trigger_update_checkout_on_change' );
function trigger_update_checkout_on_change( $fields ) {

    $fields['billing']['billing_company']['class'][] = 'update_totals_on_change';

    return $fields;
}

这将更新运输方法,问题是,该字段仍未保存在数据库中,并且package_rates()函数无法实时找到它.

最佳答案
这比这要复杂一些……需要jQuery和Ajax代码才能允许基于结帐字段用户输入显示/隐藏运送方法.

以下代码将根据结帐公司的字段启用显示/隐藏预定义的运输方式:

// Conditionally show/hide shipping methods
add_filter( 'woocommerce_package_rates','shipping_package_rates_filter_callback',100,2 );
function shipping_package_rates_filter_callback( $rates,$package ) {
    // The defined rate id
    $company_rate_id = 'flat_rate:7';

    if( WC()->session->get('company' ) === '1' ) {
        $rates = array( $company_rate_id => $rates[ $company_rate_id ] );
    } else {
        unset( $rates[ $company_rate_id ] );
    }
    return $rates;
}

// function that gets the Ajax data
add_action( 'wp_ajax_get_customer_company','wc_get_customer_company' );
add_action( 'wp_ajax_nopriv_get_customer_company','wc_get_customer_company' );
function wc_get_customer_company() {
    if ( isset($_POST['company']) && ! empty($_POST['company']) ){
        WC()->session->set('company','1' );
    } else {
        WC()->session->set('company','0' );
    }
    die(); // (required)
}

// The Jquery Ajax script
add_action( 'wp_footer','custom_checkout_script' );
function custom_checkout_script() {
    if( WC()->session->__isset('company') ) 
        WC()->session->__unset('company');

    // Only on checkout when billing company is not defined
    if( is_checkout() && ! is_wc_endpoint_url() ):
    ?>
    <script type="text/javascript">
    jQuery( function($){
        if (typeof wc_checkout_params === 'undefined') 
            return false;

        var fieldId = 'input#billing_company';

        function companyTriggerAjax( company ){
            $.ajax({
                type: 'POST',url: wc_checkout_params.ajax_url,data: {
                    'action': 'get_customer_company','company': company,},success: function (result) {
                    // Trigger refresh checkout
                    $('body').trigger('update_checkout');
                }
            });
        }

        // On start
        if( $(fieldId).val() != '' ) {
            companyTriggerAjax( $(fieldId).val() );
        }

        // On change
        $(fieldId).change( function () {
            companyTriggerAjax( $(this).val() );
        });
    });
    </script>
    <?php
    endif;
}

// Enabling,disabling and refreshing session shipping methods data
add_action( 'woocommerce_checkout_update_order_review','refresh_shipping_methods',1 );
function refresh_shipping_methods( $post_data ){
    $bool = true;

    if ( WC()->session->get('company' ) === '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();
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试和工作.

相关:Remove shipping cost if custom checkbox is checked in WooCommerce Checkout

相关文章

1.第一步 设置响应头 header(&#39;Access-Control-Allow...
$.inArray()方法介绍 $.inArray()函数用于在数组中搜索指定的...
jquery.serializejson.min.js的妙用 关于这个jquery.seriali...
JS 将form表单数据快速转化为object对象(json对象) jaymou...
jQuery插件之jquery.spinner数字智能增减插件 参考地址:http...