简体   繁体   English

根据送货方式在结帐字段中要求填写账单电话 - Woocommerce

[英]Make Billing Phone required on Checkout Field Based on Shipping Method - Woocommerce

I want to set the billing phone number required or not required based on the shipping method.我想根据送货方式设置需要或不需要的账单电话号码。

I have set the shipping method as an array and checked it from the session selected shipping method but it not working我已将送货方式设置为数组,并从 session 选择的送货方式中进行了检查,但它不起作用

add_filter('woocommerce_checkout_fields', 'fire_remove_billing_checkout_fields');

function fire_remove_billing_checkout_fields($fields) {
   
    global $woocommerce;
    $methods = array('flat_rate:2', 'flat_rate:3', 'flat_rate:17', 'flat_rate:20');// Set shipping method to hide the checkout field(s).
    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    $chosen_shipping = $chosen_methods[0];




    if(in_array($chosen_shipping, $methods)){

       $fields['billing']['billing_phone'][ 'required' ] = true;
    }else{
       $fields['billing']['billing_phone'][ 'required' ] = false;
    }
    return $fields;
}

Your code works well for me.您的代码对我来说效果很好。 I've added my shipping rate IDs and it works as expected, however refresh of the page is necessary , because WooCommerce doesn't refresh the billing fields natively on update_checkout trigger.我已经添加了我的运费 ID,它按预期工作,但是需要刷新页面,因为 WooCommerce 不会在update_checkout触发器上本地刷新账单字段。

You should either reload the page using Javascript/jQuery on updated_checkout event, or reload only the billing fields using AJAX like so:您应该在updated_checkout事件上使用 Javascript/jQuery 重新加载页面,或者使用 AJAX 仅重新加载账单字段,如下所示:

add_action( 'wp_footer', 'reload_billing_fields_on_updated_checkout' );
function reload_billing_fields_on_updated_checkout() {
    ?>
    <script>
        jQuery(document.body).on('updated_checkout', () => {
            jQuery('.woocommerce-billing-fields').load(`${location.href} .woocommerce-billing-fields>*`, '');
        });
    </script>
    <?php
}

Keep in mind that it might take a short while for those fields to reload, so you might want to add some loading CSS classes, or maybe a spinner.请记住,重新加载这些字段可能需要一段时间,因此您可能需要添加一些加载 CSS 类,或者添加一个微调器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 基于运输方式的必填结帐字段 - Woocommerce - Required Checkout Field Based on Shipping Method - Woocommerce 根据运输国家/地区选择Woocommerce结帐电话字段 - Make Woocommerce checkout phone field optional based on shipping country Woocommerce根据运输方式显示/隐藏结帐字段,并在显示时使其成为必填字段 - Woocommerce Show / Hide Checkout Fields based on shipping method and making it a required field when shown 如何在 WooCommerce 结帐时根据多个国家/地区制作自定义字段? - How to make a custom field required based on multi countries on WooCommerce checkout? 如何在 WooCommerce 结账时根据国家/地区制作所需的自定义字段? - How to make a custom field required based on country on WooCommerce checkout? Woocommerce 根据选择的运输类别在结账时更改运输方式标题 - Woocommerce Change shipping method title on checkout based on shipping class selected 在 Woocommerce 结帐账单电话字段上使用 str_replace - Using str_replace on Woocommerce checkout billing phone field WooCommerce 根据帐单地址字段更改更新送货方式 - WooCommerce updating the shipping method depending on the billing address field changes 在 WooCommerce 中设置爱尔兰所需的邮政编码结帐字段 - Make postcode checkout field required for Ireland in WooCommerce "如何在 WooCommerce 结帐中制作所需的表单字段" - How to make a form field required in WooCommerce checkout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM