在 Woocommerce 中显示基于运输区域和产品类别的自定义消息
Display a custom message based on shipping zone and product categories in Woocommerce
我一直在使用 Woocommerce 尝试编写一个 PHP 函数(作为新手,在这里和其他地方从其他 posts 中收集东西)...
:) 我似乎终于解决了这个问题(甚至在任何人回应我的 post 之前!),所以这里是为了以防它对任何人有用 - 请随时让我知道任何改进,我真的不是PHP人!
以下函数将在 cart/checkout 仅当类别 'A' 的产品在订单 中时在 的顶部显示通知]并且客户不在英国或欧盟。
add_action( 'woocommerce_before_checkout_form' , 'shipping_zone_targeted_postcodes_custom_notice' );
add_action( 'woocommerce_before_cart_table', 'shipping_zone_targeted_postcodes_custom_notice' );
function shipping_zone_targeted_postcodes_custom_notice() {
// HERE DEFINE YOUR SHIPPING ZONE NAME(S)
$targeted_zones_names = array('UK', 'EU'); // <====== <====== <====== <====== <======
// Get the customer shipping zone name
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' ); // The chosen shipping mehod
$chosen_method = explode(':', reset($chosen_methods) );
$shipping_zone = WC_Shipping_Zones::get_zone_by( 'instance_id', $chosen_method[1] );
$current_zone_name = $shipping_zone->get_zone_name();
// set your special category name, slug or ID here:
$special_cat = '(CATEGORY 'A')';
$bool = false;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$item = $cart_item['data'];
if ( has_term( $special_cat, '(CATEGORY 'A')', $item->id ) )
$bool = true;
}
if( !in_array( $current_zone_name, $targeted_zones_names ) && $bool ){
echo '<p class="message-text">(CUSTOM TEXT)</p>';
}
}
我的代码主要来自这个回答主题:
Display a custom message based on customer shipping zone in Woocommerce
There are some errors and mistakes in your code like:
This $special_cat = '(CATEGORY 'A')';
make an error and should need to be instead:
$special_cat = "(CATEGORY 'A')";
In if ( has_term( $special_cat, '(CATEGORY 'A')', $item->id ) )
$item->id
need to be instead $item->get_id()
…
and '(CATEGORY 'A')'
need to be replaced with the correct product category taxonomy which is 'product_cat'
, so:
if ( has_term( $special_cat, 'product_cat', $item->get_id() ) )
但是对于产品类别,您将改用(也可以处理产品变体) :
if ( has_term( $special_cat, 'product_cat', $cart_item['product_id'] ) )
所以功能完整的代码应该是:
add_action( 'woocommerce_before_checkout_form', 'shipping_zone_targeted_postcodes_custom_notice' );
add_action( 'woocommerce_before_cart_table', 'shipping_zone_targeted_postcodes_custom_notice' );
function shipping_zone_targeted_postcodes_custom_notice() {
// HERE DEFINE YOUR SHIPPING ZONE NAME(S)
$targeted_zones_names = array('UK', 'EU'); // <====== <====== <====== <======
// Get the customer shipping zone name
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' ); // The chosen shipping mehod
$chosen_method = explode(':', reset($chosen_methods) );
$shipping_zone = WC_Shipping_Zones::get_zone_by( 'instance_id', $chosen_method[1] );
$current_zone_name = $shipping_zone->get_zone_name();
// Set your special category name, slug or ID here:
$special_cat = "(CATEGORY 'A')";
$bool = false;
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( has_term( $special_cat, 'product_cat', $cart_item['product_id'] ) )
$bool = true;
}
if( ! in_array( $current_zone_name, $targeted_zones_names ) && $bool ){
echo '<p class="message-text">(CUSTOM TEXT)</p>';
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
相关:
Display a custom message based on customer shipping zone in Woocommerce
我一直在使用 Woocommerce 尝试编写一个 PHP 函数(作为新手,在这里和其他地方从其他 posts 中收集东西)...
:) 我似乎终于解决了这个问题(甚至在任何人回应我的 post 之前!),所以这里是为了以防它对任何人有用 - 请随时让我知道任何改进,我真的不是PHP人!
以下函数将在 cart/checkout 仅当类别 'A' 的产品在订单 中时在 的顶部显示通知]并且客户不在英国或欧盟。
add_action( 'woocommerce_before_checkout_form' , 'shipping_zone_targeted_postcodes_custom_notice' );
add_action( 'woocommerce_before_cart_table', 'shipping_zone_targeted_postcodes_custom_notice' );
function shipping_zone_targeted_postcodes_custom_notice() {
// HERE DEFINE YOUR SHIPPING ZONE NAME(S)
$targeted_zones_names = array('UK', 'EU'); // <====== <====== <====== <====== <======
// Get the customer shipping zone name
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' ); // The chosen shipping mehod
$chosen_method = explode(':', reset($chosen_methods) );
$shipping_zone = WC_Shipping_Zones::get_zone_by( 'instance_id', $chosen_method[1] );
$current_zone_name = $shipping_zone->get_zone_name();
// set your special category name, slug or ID here:
$special_cat = '(CATEGORY 'A')';
$bool = false;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$item = $cart_item['data'];
if ( has_term( $special_cat, '(CATEGORY 'A')', $item->id ) )
$bool = true;
}
if( !in_array( $current_zone_name, $targeted_zones_names ) && $bool ){
echo '<p class="message-text">(CUSTOM TEXT)</p>';
}
}
我的代码主要来自这个回答主题:
Display a custom message based on customer shipping zone in Woocommerce
There are some errors and mistakes in your code like:
This
$special_cat = '(CATEGORY 'A')';
make an error and should need to be instead:$special_cat = "(CATEGORY 'A')";
In
if ( has_term( $special_cat, '(CATEGORY 'A')', $item->id ) )
$item->id
need to be instead$item->get_id()
…
and'(CATEGORY 'A')'
need to be replaced with the correct product category taxonomy which is'product_cat'
, so:if ( has_term( $special_cat, 'product_cat', $item->get_id() ) )
但是对于产品类别,您将改用(也可以处理产品变体) :
if ( has_term( $special_cat, 'product_cat', $cart_item['product_id'] ) )
所以功能完整的代码应该是:
add_action( 'woocommerce_before_checkout_form', 'shipping_zone_targeted_postcodes_custom_notice' );
add_action( 'woocommerce_before_cart_table', 'shipping_zone_targeted_postcodes_custom_notice' );
function shipping_zone_targeted_postcodes_custom_notice() {
// HERE DEFINE YOUR SHIPPING ZONE NAME(S)
$targeted_zones_names = array('UK', 'EU'); // <====== <====== <====== <======
// Get the customer shipping zone name
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' ); // The chosen shipping mehod
$chosen_method = explode(':', reset($chosen_methods) );
$shipping_zone = WC_Shipping_Zones::get_zone_by( 'instance_id', $chosen_method[1] );
$current_zone_name = $shipping_zone->get_zone_name();
// Set your special category name, slug or ID here:
$special_cat = "(CATEGORY 'A')";
$bool = false;
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( has_term( $special_cat, 'product_cat', $cart_item['product_id'] ) )
$bool = true;
}
if( ! in_array( $current_zone_name, $targeted_zones_names ) && $bool ){
echo '<p class="message-text">(CUSTOM TEXT)</p>';
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
相关: Display a custom message based on customer shipping zone in Woocommerce