【问题标题】:Remove selected checkout fields with woocommerce depending on shipping method根据运输方式,使用 woocommerce 删除选定的结帐字段
【发布时间】:2018-11-03 00:50:02
【问题描述】:

您好,我正在尝试了解如何使用 woocommerce 结帐功能删除一些帐单字段,具体取决于所选的运输方式。因此,当客户选择本地运输但此代码不起作用时,我尝试使用此代码取消设置帐单地址、帐单城市、帐单状态和帐单邮政编码。任何帮助,将不胜感激。

add_filter('woocommerce_checkout_fields', 'xa_remove_billing_checkout_fields');

function xa_remove_billing_checkout_fields($fields) {
$shipping_method ='local_pickup:1'; // Set the desired shipping method to hide the checkout field(s).

global $woocommerce;
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];

if ($chosen_shipping == $shipping_method) {
    unset($fields['billing']['billing_address_1']); // Add/change filed name to be hide
    unset($fields['billing']['billing_address_2']);
    unset($fields['billing']['billing_city']);
    unset($fields['billing']['billing_state']);
    unset($fields['billing']['billing_postcode']);
}
return $fields;
}

【问题讨论】:

  • 这应该适用于从购物车到结帐页面。但如果不重新加载页面,将无法单独在结帐页面上工作。
  • 所以你觉得代码是正确的?我已经从购物车中选择了本地取货,然后继续结帐,但字段仍然存在。
  • 试着输入print_r($chosen_methods);,看看是不是真的是'local_pickup:1'...因为我的不是1。
  • 知道了,是 3。谢谢
  • 所以我把代码放在了我的functions.php中但是当我在购物车页面上选择运输方式时你是对的然后它可以工作但是当我在结帐时改变它不会改变,我猜我需要添加一些 java 或 jquery?

标签: wordpress woocommerce


【解决方案1】:

以下是我解决此问题的方法。
这将涉及 php、css 和 javascript (jQuery)。

PHP

add_filter( 'woocommerce_checkout_fields', 'xa_remove_billing_checkout_fields' );
function xa_remove_billing_checkout_fields( $fields ) {
    // change below for the method
    $shipping_method ='local_pickup:1'; 
    // change below for the list of fields
    $hide_fields = array( 'billing_address_1', 'billing_address_2', 'billing_city', 'billing_state', 'billing_postcode' );

    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    // uncomment below line and reload checkout page to check current $chosen_methods
    // print_r($chosen_methods);
    $chosen_shipping = $chosen_methods[0];

    foreach($hide_fields as $field ) {
        if ($chosen_shipping == $shipping_method) {
            $fields['billing'][$field]['required'] = false;
            $fields['billing'][$field]['class'][] = 'hide';
        }
        $fields['billing'][$field]['class'][] = 'billing-dynamic';
    }

    return $fields;
}

我们不会取消设置字段,而是将其更改为requiredness。 这意味着,如果选择的方法是我们想要检查的方法,我们将不会使其成为必需。然后我们将添加一个hide 类。有了这个,我们可以使用 css 隐藏这些字段。并且 woocommerce 不会抛出所需的错误。使用 jQuery,我们可以显示/隐藏这些字段。因此,如果我们在第一次运行时取消设置,则不会显示任何内容,因为字段一开始就不存在并且页面需要重新加载。

这是 javascript 和 css 部分。

add_action( 'wp_footer', 'cart_update_script', 999 );
function cart_update_script() {
    if (is_checkout()) :
    ?>
    <style>
        .hide {display: none!important;}
    </style>
    <script>
        jQuery( function( $ ) {

            // woocommerce_params is required to continue, ensure the object exists
            if ( typeof woocommerce_params === 'undefined' ) {
                return false;
            }

            $(document).on( 'change', '#shipping_method input[type="radio"]', function() {
                // change local_pickup:1 accordingly 
                $('.billing-dynamic').toggleClass('hide', this.value == 'local_pickup:1');
            });
        });
    </script>
    <?php
    endif;
}

【讨论】:

  • 很高兴我能帮助@user2012105
  • 令人敬畏和干净的代码。谢谢!有什么办法可以修改它以考虑到多种运输方式?
猜你喜欢
  • 2021-11-22
  • 2017-09-20
  • 2018-03-19
  • 2019-01-20
  • 2017-09-04
  • 1970-01-01
  • 2020-10-03
  • 1970-01-01
相关资源
最近更新 更多