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;
}
- 1 回答
- 0 關注
- 84 瀏覽
添加回答
舉報