[英]Add cart metadata to admin order screen in Woocommerce?
我有一個隱藏的輸入,我在 Woocommerce 產品屏幕上填充了一些 JS,我試圖訪問有關發送到管理屏幕和管理訂單通知的訂單元數據的信息,但我無法訪問。
我覺得我可以將 append 信息添加到購物車項目中,但這並沒有通過最終訂單。
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);
我正在使用下面的 function 將隱藏輸入的值添加到購物車項目(不嘗試向客戶顯示此信息)
/**
* 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);
我正在嘗試讓該信息顯示在
我嘗試像這樣使用這個 function ,但無法訪問我正在尋找的屬性:
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);
您必須使用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
掛鈎訪問訂單商品元數據。
該代碼已經過測試和工作。 將其添加到您的活動主題的功能中。php。
相關答案
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.