php - WooCommerce 运输文件所需的状态
问题描述
我正在使用以下功能根据客户是否选择本地取货来显示/隐藏结帐的送货地址部分。
它工作正常,但是运输部分中的某些字段是必需的,因此如果选择本地运输,结帐将不起作用。
如果选择本地取货,有没有办法使这些字段不需要?
add_action( 'woocommerce_after_checkout_form', 'bbloomer_disable_shipping_local_pickup' );
function bbloomer_disable_shipping_local_pickup( $available_gateways ) {
// Part 1: Hide shipping based on the static choice @ Cart
// Note: "#customer_details .col-1" strictly depends on your theme
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if ( 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
?>
<script type="text/javascript">
jQuery('#customer_details .col-1').fadeOut();
</script>
<?php
}
// Part 2: Hide shipping based on the dynamic choice @ Checkout
// Note: "#customer_details .col-1" strictly depends on your theme
?>
<script type="text/javascript">
jQuery('form.checkout').on('change','input[name^="shipping_method"]',function() {
var val = jQuery( this ).val();
if (val.match("^local_pickup")) {
jQuery('#customer_details .col-1').fadeOut();
} else {
jQuery('#customer_details .col-1').fadeIn();
}
});
</script>
<?php
}
解决方案
/* only hide shipping section using css/jquery is not the solution. you must need to remove fields as per your requirement */
/* please Add the following code to your child theme functions.php file */
add_filter( 'woocommerce_checkout_fields', 'change_shipping_field_optional');
function change_shipping_field_optional( $fields ) {
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$shipping_pickup = $chosen_methods[0];
if ($shipping_pickup == 'local_pickup:4') // here your shipping rate ID
{
/* add fields here as per your requirement */
$fields['shipping_first_name']['required'] = false;
$fields['shipping_last_name']['required'] = false;
$fields['shipping_company']['required'] = false;
$fields['shipping_country']['required'] = false;
$fields['shipping_address_1']['required'] = false;
$fields['shipping_address_2']['required'] = false;
$fields['shipping_city']['required'] = false;
$fields['shipping_state']['required'] = false;
$fields['shipping_postcode']['required'] = false;
unset($fields['shipping']['shipping_first_name']);
unset($fields['shipping']['shipping_last_name']);
unset($fields['shipping']['shipping_company']);
unset($fields['shipping']['shipping_country']);
unset($fields['shipping']['shipping_address_1']);
unset($fields['shipping']['shipping_address_2']);
unset($fields['shipping']['shipping_city']);
unset($fields['shipping']['shipping_state']);
unset($fields['shipping']['shipping_postcode']);
}
return $fields;
}
推荐阅读
- postgresql - 在 timescaleDB postgresql 上的 CAGG 之上实现(连续聚合)CAGG
- python - 带有数据库服务器的多个 django 服务器
- google-sheets - 将一行转换为共享一些相同数据的多行
- excel - Excel 筛选公式/函数 - 从筛选公式/函数返回第一行
- javascript - 使用另一个数组内的数组中的数据进行过滤
- oracle - 如何使用 sysdate arg 创建 oracle 作业?
- r - for循环和列表生成问题
- javascript - how to set up a check box to pick a level
- slf4j - Logback for different log levels
- reactjs - Version Mismatch Error, while processing and publishing an asset to link with an entry