Php WooCommerce电子邮件模板中的ACF自定义字段

Php WooCommerce电子邮件模板中的ACF自定义字段,php,wordpress,woocommerce,advanced-custom-fields,acfpro,Php,Wordpress,Woocommerce,Advanced Custom Fields,Acfpro,我想在order complete电子邮件中显示产品中的自定义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_u

我想在order complete电子邮件中显示产品中的自定义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;
}
add_filter('woocmerce_order_item_name','custom_order_item_name',10,2);
函数自定义\订单\项目\名称($item\名称,$item){
//仅针对电子邮件通知
如果(是_wc_endpoint_url())
返回$item_name;
//获取WC_产品对象(从订单项)
$product=$item->get_product();
如果($date=get_字段('date',$product->get_id())){
$item_name.='
'.\u('Date','woocommerce')。:'.$Date; } 如果($location=get_字段('location',$product->get_id())){ $item_name.='
'.\u('Location','woocommerce')。:'.$Location; } 返回$item_name; }
但是,虽然它可以在电子邮件中显示简单产品的自定义字段(日期和位置),但不能显示可变产品的自定义字段

我似乎不明白为什么?我找到了解决办法

当它是一个简单的产品时,产品ID是post ID。但是当它是一个可变产品时,他们使用可变的产品ID,而不是post ID。这意味着ACF字段没有查看产品的post 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;
}
//在电子邮件通知中显示项目发货ACF自定义字段值
添加过滤器('woocommerce\u order\u item\u name','custom\u order\u item\u name',10,2);
函数自定义\订单\项目\名称($item\名称,$item){
//仅针对电子邮件通知
如果(是_wc_endpoint_url())
返回$item_name;
//获取WC_产品对象(从订单项)
$product=$item->get_product();
$parent_id=$product->get_parent_id();
//如果是可变产品,则获取父ID
if($parent\u id){
$product\U id=$parent\U id;
//否则,它是一个简单的产品,获取产品ID
}否则{
$product_id=$product->get_id();
}
如果($date=get_字段($date',$product_id)){
$item_name.='
'.\u('Date','woocommerce')。:'.$Date; } 如果($location=get_字段($location',$product_id)){ $item_name.='
'.\u('Location','woocommerce')。:'.$Location; } 返回$item_name; }