我一直在使用Woocommerce尝试编写PHP函数(作为新手从这里和其他地方的其他文章中窃取的东西)...
:)我似乎终于解决了这个问题(在任何人甚至还没有回复我的帖子之前!),所以在这里,以防万一它对任何人都有用-随时让我知道任何改进,我真的不是一个PHP人员!
当“ 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
答案 0 :(得分:0)
您的代码中存在一些错误和错误,例如:
此
$special_cat = '(CATEGORY 'A')';
出错,应改为:
$special_cat = "(CATEGORY 'A')";
在
if ( has_term( $special_cat, '(CATEGORY 'A')', $item->id ) )
$item->id
中需要改为$item->get_id()
…
和'(CATEGORY 'A')'
需要替换为'product_cat'
的正确的产品类别分类,因此: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