wordpress 根据运输方式使用woocommerce删除选定的结帐字段

yc0p9oo0  于 9个月前  发布在  WordPress
关注(0)|答案(2)|浏览(144)

你好,所以我试图找出如何使用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;
}

字符串

eyh26e7m

eyh26e7m1#

我会这样解决这个问题。
这将涉及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;
}

字符串
而不是取消设置字段,我们将只是改变它的required ness。这意味着,如果选择的方法是我们想要检查的方法,我们将不需要它。然后我们将添加一个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;
}

avwztpqn

avwztpqn2#

add_filter('woocommerce_checkout_fields', 
   'xa_remove_billing_checkout_fields');

     function xa_remove_billing_checkout_fields($fields) {
    // Change below for the list of shipping methods
    $allowed_shipping_methods = array('montonio_dpd_courier:15', 
 'montonio_dpd_courier:11');
// 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) {
    // Add the hide class by default
    $fields['billing'][$field]['class'][] = 'hide';
    $fields['billing'][$field]['class'][] = 'billing-dynamic';

    // Remove the hide class and set 'required' attribute if the chosen shipping method is in the allowed list
    if (in_array($chosen_shipping, $allowed_shipping_methods)) {
        unset($fields['billing'][$field]['class'][array_search('hide', $fields['billing'][$field]['class'])]);
        $fields['billing'][$field]['required'] = true;
    }
}

return $fields;
  }
    add_action('wp_footer', 'cart_update_script', 999);
 function cart_update_script() {
if (is_checkout()) :
?>
<style>
    .hide {display: none!important;}
</style>
<script>
    jQuery(function ($) {
        // Initial check on page load
        toggleBillingFields();

        // Listen for changes in shipping method
        $(document).on('change', '#shipping_method input[type="radio"]', function () {
            toggleBillingFields();
        });

        function toggleBillingFields() {
            // change montonio_dpd_courier:15 and montonio_dpd_courier:11 accordingly
            var allowedMethods = ['montonio_dpd_courier:15', 'montonio_dpd_courier:11'];
            var chosenMethod = $('#shipping_method input[type="radio"]:checked').val();

            // Hide or show fields based on the chosen shipping method
            $('.billing-dynamic').toggleClass('hide', allowedMethods.indexOf(chosenMethod) === -1);

            // Update 'required' attribute for billing fields
            $('input[name^="billing_"]').each(function () {
                var fieldName = $(this).attr('name').replace('billing_', ''); // Extract field name
                var isRequired = allowedMethods.indexOf(chosenMethod) !== -1 && !$(this).hasClass('hide');
                $(this).prop('required', isRequired);
            });
        }
    });
</script>
<?php
endif;
}

字符串
我修改了像这样的最后一个片段.我想这些字段将被启用和活动,只有这两种运输方法(他们是为不同的运输国家.隐藏部分工作良好,但他们仍然需要.我应该做什么不同?

相关问题