久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

將自定義字段數據添加到 WooCommerce 訂單

Add custom field data to WooCommerce order(將自定義字段數據添加到 WooCommerce 訂單)
本文介紹了將自定義字段數據添加到 WooCommerce 訂單的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我的 WooCommerce 單一產品上有一個自定義字段.它發送到購物車罰款,它在結帳時顯示,它在儀表板中按順序顯示.

I have a custom field on my WooCommerce single product. It sends to the cart fine, it displays on checkout fine, it shows in the order in the dashboard fine.

我現在要做的是將值設置為訂單頁面中的自定義字段,這樣我就可以在需要時修改文本.出于某種原因,當我提交表單時,這一步不起作用.

What I am now trying to do is set the value as a custom field in the order page so I am able to amend the text when I need to. For some reason when I submit the form this step isn't working.

我在functions.php文件中使用的代碼:

The code that i use in my functions.phpfile:

// Add the field to the product
add_action('woocommerce_before_add_to_cart_button', 'my_custom_checkout_field');

function my_custom_checkout_field() {
    echo '<div id="my_custom_checkout_field"><h3>'.__('My Field').'</h3>';
    echo  '<label>fill in this field</label> <input type="text" name="my_field_name">';
    echo '</div>';
}

// Store custom field
function save_my_custom_checkout_field( $cart_item_data, $product_id ) {
    if( isset( $_REQUEST['my_field_name'] ) ) {
        $cart_item_data[ 'my_field_name' ] = $_REQUEST['my_field_name'];
        /* below statement make sure every add to cart action as unique line item */
        $cart_item_data['unique_key'] = md5( microtime().rand() );
    }
    return $cart_item_data;
}
add_action( 'woocommerce_add_cart_item_data', 'save_my_custom_checkout_field', 10, 2 );

// Render meta on cart and checkout
function render_meta_on_cart_and_checkout( $cart_data, $cart_item = null ) {
    $custom_items = array();
    /* Woo 2.4.2 updates */
    if( !empty( $cart_data ) ) {
        $custom_items = $cart_data;
    }
    if( isset( $cart_item['my_field_name'] ) ) {
        $custom_items[] = array( "name" => 'My Field', "value" => $cart_item['my_field_name'] );
    }
    return $custom_items;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );

// Display as order meta
function my_field_order_meta_handler( $item_id, $values, $cart_item_key ) {
    if( isset( $values['my_field_name'] ) ) {
        wc_add_order_item_meta( $item_id, "my_field_name", $values['my_field_name'] );
    }
}
add_action( 'woocommerce_add_order_item_meta', 'my_field_order_meta_handler', 1, 3 );

/** THIS IS WHERE I'M STUCK **/

add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
    global $woocommerce;

    // Check if set, if its not set add an error. This one is only requite for companies
    if ($_POST['billing_company'])
        if (!$_POST['my_field_name']) 
            $woocommerce->add_error( __('Please enter your XXX.') );
}
// Update the user meta with field value
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta');
function my_custom_checkout_field_update_user_meta( $user_id ) {
    if ($user_id && $_POST['my_field_name']) update_user_meta( $user_id, 'my_field_name', esc_attr($_POST['my_field_name']) );
}

// Update the order meta with field value

add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ($_POST['my_field_name']) update_post_meta( $order_id, 'My Field', esc_attr($_POST['my_field_name']));
}

當前發生的屏幕截圖:

我希望發生的事情:

任何幫助將不勝感激.

推薦答案

更新:與 Woocommerce 版本 3+ 的兼容性

Updated: compatibility with Woocommerce version 3+

您缺少在訂單編輯頁面上顯示此自定義字段值的功能:

You have missing the function to display this custom field value on the order edit page:

/**
 * Display field value on the order edit page
 */
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta( $order ){
    $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
    echo '<p><strong>'.__('My Field Name').':</strong> ' . get_post_meta( $order_id, 'my_field_name', true ) . '</p>';
}

在下面的參考鏈接中,您擁有所有原始的 wooThemes 功能工作代碼片段.這是一個出色的全功能教程.

On the reference link below, you have all original wooThemes functional working code snippets. It's an excellent fully functional tutorial.

參考:[使用操作和過濾器自定義結帳字段][1]

Reference: [Customizing checkout fields using actions and filters][1]

在 Order item meta 中使用自定義字段值顯示自定義標簽

獲得一個像我的字段名稱"這樣的自定義標簽和你的自定義字段值(按項目元排序)而不是像my_field_name這樣的slug強>,參考這個踩:

To get a custom label like "MY field name" with your custom field value (in order items meta) instead of a slug like my_field_name, refer to this treads:

  • 保存產品自定義字段并在購物車頁面中顯示
  • 顯示產品自定義字段值按處理后的順序
  • 將用戶自定義字段值添加到訂單商品詳情

這篇關于將自定義字段數據添加到 WooCommerce 訂單的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產品的總訂單數)
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 免费同性女女aaa免费网站 | 99精品免费在线观看 | 成人不卡视频 | 一本一道久久a久久精品蜜桃 | 天天躁日日躁狠狠躁白人 | 国产亚洲www| 91在线看| 国产一区91精品张津瑜 | 久久精品国产一区二区三区 | 福利二区| www国产亚洲精品 | 国产精品一区二区在线 | 国产香蕉视频 | 中文字幕成人在线 | 欧美bondage紧缚视频 | 亚州影院 | 午夜不卡一区二区 | 精品www| 羞羞免费网站 | 亚洲国内精品 | 在线免费观看视频你懂的 | 久久久成人免费视频 | 午夜电影网 | 九九在线视频 | 一区二区三区四区免费观看 | 91精品国产综合久久久动漫日韩 | 久久www免费人成看片高清 | 日韩和的一区二区 | 青青草一区二区 | 国产精品日韩在线观看 | 福利片一区二区 | 国产精品美女一区二区三区 | 日韩高清一区 | 亚洲欧美视频一区二区 | 中文久久| 日韩欧美网 | 国产91久久精品一区二区 | 精品国产欧美一区二区三区不卡 | 麻豆久久久9性大片 | 日韩不卡在线观看 | 中文字幕一区二区三区四区五区 |