WooCommerce 有条件地显示订单评论并使其成为必需的
WooCommerce conditionally show order comments and make it required
On Checkout page, I would like to show the comment field, only if a coupon code is applied. In this case this comment field should be a required field.
除了必需状态变为可选状态之外,下面的示例有效。
我将评论设置为默认要求,然后我假设在取消设置后,要求的状态将被忽略。
这是需要评论的片段:
$fields['order']['order_comments']['required'] = true;
此代码段查找优惠券代码,然后显示一条消息。我不需要该消息,所以我将其留空,然后我添加了隐藏评论的行:
add_action( 'woocommerce_before_checkout_form' , 'product_checkout_custom_content' );
function product_checkout_custom_content() {
global $woocommerce;
$msgs = array('mycouponcode'=>'');
$applied_coupon = $woocommerce->cart->applied_coupons;
if( ! array_key_exists($applied_coupon[0], $msgs) ) {
// Hides the order comments
unset( $fields['order']['order_comments'] );
add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
// Here I need to make the order_comments optional, not required
// echo $msgs[$applied_coupon[0]];
}
}
如何在同一操作中将订单评论设置为可选?
To make that work, you don't need function product_checkout_custom_content()
. Instead you have to make some change in the function where is included $fields['order']['order_comments']['required'] = true;
.
我想这是一个挂钩在 woocommerce_checkout_fields
中的函数。因此,在该函数中,您必须将 $fields['order']['order_comments']['required'] = true;
替换为函数内的代码:
// CHECKOUT PAGE - CUSTOMIZING comment field (conditional behavior).
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
//Set your coupon slug here:
$coupon = 'coupon_slug';
// Coupon is applied: Changing Comment field Label, placeholder and setting "REQUIRED"
if ( in_array( '$coupon, WC()->cart->applied_coupons ) ){
$fields['order']['order_comments']['label'] = __('Your comment label…', 'my_theme_slug');
$fields['order']['order_comments']['placeholder'] = __('Enter here something', 'my_theme_slug');
$fields['order']['order_comments']['required'] = true;
} else {
// Removes the comment field + block title
unset($fields['order']['order_comments']);
add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
}
return $fields;
}
你不需要其他任何东西……
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
此代码已经过测试并且有效。
参考:Remove the Additional Information and Order Notes fields in WooCommerce
On Checkout page, I would like to show the comment field, only if a coupon code is applied. In this case this comment field should be a required field.
除了必需状态变为可选状态之外,下面的示例有效。
我将评论设置为默认要求,然后我假设在取消设置后,要求的状态将被忽略。
这是需要评论的片段:
$fields['order']['order_comments']['required'] = true;
此代码段查找优惠券代码,然后显示一条消息。我不需要该消息,所以我将其留空,然后我添加了隐藏评论的行:
add_action( 'woocommerce_before_checkout_form' , 'product_checkout_custom_content' );
function product_checkout_custom_content() {
global $woocommerce;
$msgs = array('mycouponcode'=>'');
$applied_coupon = $woocommerce->cart->applied_coupons;
if( ! array_key_exists($applied_coupon[0], $msgs) ) {
// Hides the order comments
unset( $fields['order']['order_comments'] );
add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
// Here I need to make the order_comments optional, not required
// echo $msgs[$applied_coupon[0]];
}
}
如何在同一操作中将订单评论设置为可选?
To make that work, you don't need
function product_checkout_custom_content()
. Instead you have to make some change in the function where is included$fields['order']['order_comments']['required'] = true;
.
我想这是一个挂钩在 woocommerce_checkout_fields
中的函数。因此,在该函数中,您必须将 $fields['order']['order_comments']['required'] = true;
替换为函数内的代码:
// CHECKOUT PAGE - CUSTOMIZING comment field (conditional behavior).
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
//Set your coupon slug here:
$coupon = 'coupon_slug';
// Coupon is applied: Changing Comment field Label, placeholder and setting "REQUIRED"
if ( in_array( '$coupon, WC()->cart->applied_coupons ) ){
$fields['order']['order_comments']['label'] = __('Your comment label…', 'my_theme_slug');
$fields['order']['order_comments']['placeholder'] = __('Enter here something', 'my_theme_slug');
$fields['order']['order_comments']['required'] = true;
} else {
// Removes the comment field + block title
unset($fields['order']['order_comments']);
add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
}
return $fields;
}
你不需要其他任何东西……
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
此代码已经过测试并且有效。
参考:Remove the Additional Information and Order Notes fields in WooCommerce