根据Woocommerce中的运输区域和产品类别显示自定义消息

埃德·泰勒

我一直在使用Woocommerce尝试编写PHP函数(作为新手hack东西,与此处和其他地方的其他文章一起使用)...

:)我似乎终于做到了这一点(在任何人甚至没有回复我的帖子之前!),所以在这里,以防万一它对任何人都有用-随时告诉我任何改进,我真的不是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>';    
  }
}

我的代码主要来自此答案线程:
根据Woocommerce中的客户运输区域显示自定义消息

LoicTheAztec

您的代码中存在一些错误和错误,例如:

  • $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文件中。经过测试和工作。

相关:根据Woocommerce中的客户运送区域显示自定义消息

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在自定义家庭和产品类别档案中显示WooCommerce产品属性

根据Woocommerce中的产品类别添加自定义结帐字段

在Woocommerce商店和产品类别页面上显示高级自定义字段

从Woocommerce档案库中的自定义显示按钮中排除产品类别

从产品类别和自定义meta_key对Woocommerce产品进行排序

根据WooCommerce结帐页面上的产品类别应用自定义数量参数

根据 WooCommerce 电子邮件通知中的产品类别添加自定义文本“每个项目”

产品类别的WooCommerce产品价格中的自定义小数

在Woocommerce中显示基于自定义文本的产品类别订单商品名称

根据产品类别自定义购物车总计和结帐总计文本

WooCommerce 中特定产品类别的自定义条款和条件复选框

获取 WooCommerce 中顶级产品类别的自定义数组

在Woocommerce中为特定产品类别添加自定义按钮

通过主题自定义Woocommerce中的产品类别面包屑链接

在 WooCommerce 中输出特定产品类别的自定义短代码

根据产品类别和运输方式添加费用WooCommerce

在 WooCommerce 循环产品标题后显示来自产品类别的自定义字段

购物车中有2种特定产品类别时显示自定义消息

根据产品类别自动设置WooCommerce产品运输类别

Woocommerce 3中按产品类别的自定义购物车项目计数

在WooCommerce中为特定产品类别自定义“添加到购物车”按钮

基于Woocommerce中产品类别的条件自定义结帐字段

产品类别内的“高级自定义字段中继器”(woocommerce)

基于WooCommerce中产品类别的自定义“缺货”文本

WooCommerce自定义简码产品类别下拉列表

Woocommerce自定义产品类别下拉问题

从Woocommerce的自定义动态定价中排除产品类别

在Woocommerce中根据产品类别更改货币符号

根据WooCommerce购物车中的运输类别显示自定义文本