第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

“伍商務電子郵件”模板中的 ACF 自定義字段

“伍商務電子郵件”模板中的 ACF 自定義字段

PHP
翻過高山走不出你 2022-09-12 13:11:52
我想在訂單完整電子郵件中的產(chǎn)品中顯示自定義ACF字段,我有這個鉤子,它非常適合非變量產(chǎn)品: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;}但是,雖然它對電子郵件中的簡單產(chǎn)品顯示我的自定義字段(日期和位置)很好,但對于可變產(chǎn)品卻沒有。我似乎不明白為什么?
查看完整描述

1 回答

?
江戶川亂折騰

TA貢獻1851條經(jīng)驗 獲得超5個贊

我找到了解決方案。


當它是簡單產(chǎn)品時,產(chǎn)品 ID 是發(fā)布 ID。但是,當它是可變產(chǎn)品時,它們將使用可變產(chǎn)品 ID,而不是發(fā)布 ID。這意味著 ACF 字段不會查看產(chǎn)品的帖子 ID,因此不會顯示。


要為可變產(chǎn)品修復此問題,您必須從數(shù)組中獲取父 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 關注
  • 84 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號