我已将以下代码添加到remove some checkout fields 在案例a中particular shipping method 被选中,并且在您change the shipping method on the checkout page.
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\'; // Here I want to specify an array of shipping methods
// 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;
}
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;
}
我想知道如何为
array of shipping methods. 假设我想让这项工作也适用于flat\\u rate:2,那么我如何优雅地循环数组以隐藏数组的所有装运方法的字段。
最合适的回答,由SO网友:BarrieO 整理而成
我为我的问题找到了以下代码:
/* HIDE "POSTKANTOOR" FIELD WHEN NON-POSTKANTOOR SHIPPING OPTION IS SELECTED */
/* --- */
add_filter( \'woocommerce_checkout_fields\', \'remove_billing_checkout_fields\' );
function remove_billing_checkout_fields( $fields ) {
// Postkantoor shipping methods
$shipping_method = array( \'flat_rate:6\',\'flat_rate:7\',\'flat_rate:8\' );
// Fields to hide if not postkantoor shipping method
$hide_fields = array( \'postkantoor\' );
$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 ( ! in_array ($chosen_shipping , $shipping_method) ) {
$fields[\'billing\'][$field][\'required\'] = false;
$fields[\'billing\'][$field][\'class\'][] = \'hide\';
}
$fields[\'billing\'][$field][\'class\'][] = \'billing-dynamic\';
}
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( $ ) {
// woocommerce_params is required to continue, ensure the object exists
if ( typeof woocommerce_params === \'undefined\' ) {
return false;
}
// Postkantoor shipping methods
var show = [\'flat_rate:6\',\'flat_rate:7\',\'flat_rate:8\'];
$(document).on( \'change\', \'#shipping_method input[type="radio"]\', function() {
// console.log($.inArray($(this).val(), show));
if ($.inArray($(this).val(), show) > -1) { // >-1 if found in array
$(\'.billing-dynamic\').removeClass(\'hide\');
// console.log(\'show\');
} else {
$(\'.billing-dynamic\').addClass(\'hide\');
// console.log(\'hide\');
}
});
});
</script>
<?php
endif;
}
SO网友:Falz
抱歉,这不是一个很好的答案。然而,我还没有足够的声望点来添加评论:(
我只是在看书this site, 我看到了下面的片段,它可能对您正在尝试的操作有用。
(这不是我的代码,我使用此页面可以获得许多好的想法和提示)
/**
* @snippet Simplify Checkout if Only Virtual Products
* @how-to Get CustomizeWoo.com FREE
* @sourcecode https://businessbloomer.com/?p=78351
* @author Rodolfo Melogli
* @compatible WooCommerce 3.5.4
* @donate $9 https://businessbloomer.com/bloomer-armada/
*/
add_filter( \'woocommerce_checkout_fields\' , \'bbloomer_simplify_checkout_virtual\' );
function bbloomer_simplify_checkout_virtual( $fields ) {
$only_virtual = true;
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// Check if there are non-virtual products
if ( ! $cart_item[\'data\']->is_virtual() ) $only_virtual = false;
}
if( $only_virtual ) {
unset($fields[\'billing\'][\'billing_company\']);
unset($fields[\'billing\'][\'billing_address_1\']);
unset($fields[\'billing\'][\'billing_address_2\']);
unset($fields[\'billing\'][\'billing_city\']);
unset($fields[\'billing\'][\'billing_postcode\']);
unset($fields[\'billing\'][\'billing_country\']);
unset($fields[\'billing\'][\'billing_state\']);
unset($fields[\'billing\'][\'billing_phone\']);
add_filter( \'woocommerce_enable_order_notes_field\', \'__return_false\' );
}
return $fields;
}
我希望你觉得它有用:)