Пользовательские поля ACF в шаблоне электронной почты WooCommerce - PullRequest
2 голосов
/ 07 февраля 2020

Я хочу отображать настраиваемые поля 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 Ответ

1 голос
/ 10 февраля 2020

Я нашел решение.

Когда это простой продукт, идентификатор продукта - это идентификатор сообщения. Однако, когда это переменный продукт, они используют переменный идентификатор продукта, а не идентификатор поста. Это означает, что поля ACF не смотрят на идентификатор сообщения продукта, поэтому не будут отображаться.

Чтобы исправить это для переменных продуктов, вы должны получить родительский идентификатор из массива:

 $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;
}
...