为了账号安全,请及时绑定邮箱和手机立即绑定

“伍商务电子邮件”模板中的 ACF 自定义字段

“伍商务电子邮件”模板中的 ACF 自定义字段

PHP
翻过高山走不出你 2022-09-12 13:11:52
我想在订单完整电子邮件中的产品中显示自定义ACF字段,我有这个钩子,它非常适合非变量产品:add_filter( 'woocommerce_order_item_name', 'custom_order_item_name', 10, 2 );function custom_order_item_name( $item_name, $item ) {  // Targeting email notifications only  if( is_wc_endpoint_url() )      return $item_name;  // Get the WC_Product object (from order item)  $product = $item->get_product();  if( $date = get_field('date', $product->get_id()) ) {    $item_name .= '<br /><strong>' . __( 'Date', 'woocommerce' ) . ': </strong>' . $date;  }  if( $location = get_field('location', $product->get_id()) ) {    $item_name .= '<br /><strong>' . __( 'Location', 'woocommerce' ) . ': </strong>' . $location;  }  return $item_name;}但是,虽然它对电子邮件中的简单产品显示我的自定义字段(日期和位置)很好,但对于可变产品却没有。我似乎不明白为什么?
查看完整描述

1 回答

?
江户川乱折腾

TA贡献1851条经验 获得超5个赞

我找到了解决方案。


当它是简单产品时,产品 ID 是发布 ID。但是,当它是可变产品时,它们将使用可变产品 ID,而不是发布 ID。这意味着 ACF 字段不会查看产品的帖子 ID,因此不会显示。


要为可变产品修复此问题,您必须从数组中获取父 ID:


 $parent_id=$product->get_parent_id();

  // If it is a variable product, get the parent ID

  if($parent_id){

    $product_id = $parent_id;

  // else, it is a simple product, get the product ID

  }else{

    $product_id = $product->get_id();

  }

完整代码为:


// Display Items Shipping ACF custom field value in email notification

add_filter( 'woocommerce_order_item_name', 'custom_order_item_name', 10, 2 );

function custom_order_item_name( $item_name, $item ) {

  // Targeting email notifications only

  if( is_wc_endpoint_url() )

      return $item_name;


  // Get the WC_Product object (from order item)

  $product = $item->get_product();


 $parent_id=$product->get_parent_id();

  // If it is a variable product, get the parent ID

  if($parent_id){

    $product_id = $parent_id;

  // else, it is a simple product, get the product ID

  }else{

    $product_id = $product->get_id();

  }


  if( $date = get_field('date', $product_id) ) {

    $item_name .= '<br /><strong>' . __( 'Date', 'woocommerce' ) . ': </strong>' . $date;

  }

  if( $location = get_field('location', $product_id) ) {

    $item_name .= '<br /><strong>' . __( 'Location', 'woocommerce' ) . ': </strong>' . $location;

  }

  return $item_name;

}


查看完整回答
反对 回复 2022-09-12
  • 1 回答
  • 0 关注
  • 99 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号