我有一個隱藏的輸入,我在 Woocommerce 產品螢屏上填充了一些 JS,我試圖訪問有關發送到管理螢屏和管理訂單通知的訂單元資料的資訊,但我無法訪問。
我覺得我可以將資訊附加到購物車專案,但這不是通過最終訂單來實作的。
添加隱藏欄位
function add_polarity_custom_hidden_field() {
global $product;
ob_start(); //Side note...not sure I need Output buffering here, feel free to let me know.
//Here's the input (value is populated with a separate radio group using JS)
?>
<input type="hidden" id="polarity-info" name="polarity_information" value="">
<?php
$content = ob_get_contents();
ob_end_flush();
return $content;
}
add_action('woocommerce_after_variations_table', 'add_polarity_custom_hidden_field', 10);
我在下面使用此功能將隱藏輸入的值添加到購物車專案(不嘗試向客戶顯示此資訊)
將自定義資料添加到購物車訂單項:
/**
* Add custom data to Cart
*/
function add_polarity_info_to_order($cart_item_data, $product_id, $variation_id) {
if (isset($_REQUEST['polarity_information'])) {
$cart_item_data['polarity_information'] = sanitize_text_field($_REQUEST['polarity_information']);
}
return $cart_item_data;
}
add_filter('woocommerce_add_cart_item_data', 'add_polarity_info_to_order', 10, 3);
在管理頁面上顯示為訂單元資料:
我正在嘗試讓該資訊顯示在
- 訂單通知電子郵件作為專案元資料
- 管理行專案,也作為專案元資料
我嘗試像這樣使用此功能,但無法訪問我正在尋找的屬性:
add_action('woocommerce_admin_order_data_after_order_details', function ($order) {
$order->get_ID();
foreach ($order->get_items() as $item_id => $item) {
$allmeta = $item->get_meta_data();
var_dump($allmeta); //Not within these properties.
}
});
邊注
我可以使用以下代碼將商品顯示在購物車頁面上,但我無法將其轉換為最終訂單。
/**
* Display information as Meta on Cart page
*/
function add_polarity_data_to_cart_page($item_data, $cart_item) {
if (array_key_exists('polarity_information', $cart_item)) {
$polarity_details = $cart_item['polarity_information'];
$item_data[] = array(
'key' => 'Polarity Information',
'value' => $polarity_details
);
}
return $item_data;
}
add_filter('woocommerce_get_item_data', 'add_polarity_data_to_cart_page', 10, 2);
uj5u.com熱心網友回復:
您必須使用woocommerce_checkout_create_order_line_item掛鉤將購物車資料存盤為訂單商品元資料。
所以:
add_action('woocommerce_checkout_create_order_line_item', 'save_custom_order_item_meta_data', 10, 4 );
function save_custom_order_item_meta_data( $item, $cart_item_key, $values, $order ) {
if ( isset( $values['polarity_information'] ) ) {
$item->update_meta_data( '_polarity_information', $values['polarity_information'] );
}
}
您現在可以從woocommerce_admin_order_data_after_order_details掛鉤訪問訂單專案元資料。
該代碼已經過測驗和作業。將它添加到您的活動主題的functions.php。
相關答案
- 將 Woocommerce 購物車專案自定義資料保存為訂單專案元資料,在訂單和電子郵件中顯示它
- 如何從 WooCommerce 中的訂單專案中獲取購物車專案密鑰
- 在 WooCommerce 訂單和電子郵件中添加和顯示自定義購物車專案資料
- 在 Woocommerce 3 中使用購物車專案自定義資料更新訂單元資料
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/421554.html
標籤:
上一篇:洗掉手機和平板電腦上的div
