我在 WooCommerce 產品上為帖子型別設定了高級自定義欄位。所以每個產品都有 1 個獨特的自定義欄位。
我試圖在購物車上的產品名稱和結帳頁面以及訂單表資訊之后顯示自定義欄位。
但是,由于我的代碼不顯示任何輸出而遇到問題。
任何關于如何實作這一目標的建議將不勝感激。謝謝
// Display the ACF custom field 'location' after the product title on cart / checkout page.
function cart_item_custom_feild( $cart_item ) {
$address = get_field( 'location', $cart_item['product_id'] );
echo "<div>Address: $address.</div>";
}
add_action( 'woocommerce_after_cart_item_name', 'cart_item_custom_feild', 10, 1 );
我也試過the_field代替get_field
uj5u.com熱心網友回復:
1- 在購物車頁面和結帳頁面上的訂單審查表上
如果您需要在購物車頁面和結帳頁面上的訂單審查表上運行它,您可以使用woocommerce_cart_item_name過濾器鉤子,如下所示:
add_filter('woocommerce_cart_item_name', 'order_review_custom_field', 999, 3);
function order_review_custom_field($product_name, $cart_item, $cart_item_key)
{
$address = get_field('location', $cart_item['product_id']);
return ($address) ?
$product_name . '<div>Address: ' . $address . '</div>'
:
$product_name . '<div>Address: No address found!</div>';
}
這是購物車頁面上的結果:

在結賬頁面的訂單審查表上:

2- 在電子郵件和感謝頁面上的訂單詳細資訊表中:
我們可以使用woocommerce_order_item_meta_endaction hook 將自定義欄位值附加到電子郵件模板上的產品名稱末尾:
add_action("woocommerce_order_item_meta_end", "email_order_custom_field", 999, 4);
function email_order_custom_field($item_id, $item, $order, $plain_text)
{
$address = get_field('location', $item->get_product_id());
echo ($address) ?
'<div>Address: ' . $address . '</div>'
:
'<div>Address: No address found!</div>';
};
這是電子郵件中的結果:

在感謝頁面上的訂單詳細資訊表中:

這個答案已經在 woocommerce 上進行了全面測驗5.7.1并且有效。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/403881.html
標籤:
下一篇:使用類方法時未創建表
