保存自定义结帐字段并将其显示在admin Woocommerce Orders中 [英] Save custom checkout fields and display them in admin Woocommerce Orders

查看:51
本文介绍了保存自定义结帐字段并将其显示在admin Woocommerce Orders中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根据所拥有的订单数量创建了一些其他表单字段.因此,如果我在购物车中订购了2件物品,则该字段应该出现两次.

I created some additional form fields in accordance to the quantity of orders that I have. Therefore if I order 2 quantities of items in my Cart, the field should come twice.

一切正常,除了,我只在订购单中得到一个字段的输入.我希望每个相似的表单字段都有单独的值.

Everything works fine, except that, I only get the input for one field in my order form. I want to have separate values for each similar form fields.

foreach(WC()->cart->get_cart() as $cart_item){
    //2nd Loop go through each unit related to item quantity
    for($i = 1; $i <= $cart_item['quantity']; $i++){
        $index++;

        woocommerce_form_field('myname', array(
            'type' =>'text',
            'class'=>array('my-field-class form-row-wide'),
            'label'=>__('My Name'),
            'placeholder'=>__('Please enter your name'),
        ), $checkout->get_value('myname'));

我对此进行更新:

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 (! empty( $_POST['myname'])){
        update_post_meta($order_id,'Aspirant Name', sanitize_text_field($_POST['myname']));
    }
}

并显示以下内容:

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){
    echo '<p><strong>'.__('My Name').':</strong> ' . get_post_meta($order->get_id(),'My Name', true).'</p>';
}

感谢您的帮助.

推荐答案

这是在订单中保存所有相关的自定义结帐值并在帐单明细下方的订单编辑页面中显示它们的正确方法:

Here is the correct way to save all related custom checkout values in the order and display them in order edit pages below billing details:

// Add checkout custom text fields
add_action( 'woocommerce_before_order_notes', 'add_checkout_custom_text_fields', 20, 1 );
function add_checkout_custom_text_fields( $checkout) {
    $index = 0;

    // 1st Loop through cart items
    foreach(WC()->cart->get_cart() as $cart_item){
        $index++;
        // 2nd Loop through each unit related to item quantity
        for($i = 1; $i <= $cart_item['quantity']; $i++){

            woocommerce_form_field("my_field[$index][$i]", array(
                'type' =>'text',
                'class'=>array('my-field-class form-row-wide'),
                'label'=>__('My Field')." (item $index - $i)",
                'placeholder'=>__('Please enter your data'),
            ), $checkout->get_value("my_field[$index][$i]"));
        }
    }
}

// Save fields in order meta data
add_action('woocommerce_checkout_create_order', 'save_custom_fields_to_order_meta_data', 20, 2 );
function save_custom_fields_to_order_meta_data( $order, $data ) {
    $index = 0;

    // 1st Loop through order items
    foreach( $order->get_items() as $item ){
        $index++;
        // 2nd Loop through each unit related to item quantity
        for($i = 1; $i <= $item->get_quantity(); $i++){
            if (isset( $_POST['my_field'][$index][$i]) && ! empty($_POST['my_field'][$index][$i]) ){
                $order->update_meta_data( '_my_field_'.$index.'_'.$i, esc_attr( $_POST['my_field'][$index][$i] ) );
            }
        }
    }
}

// Display fields in order edit pages
add_action('woocommerce_admin_order_data_after_billing_address','display_custom_fields_in_admin_order', 20, 1);
function display_custom_fields_in_admin_order( $order ){
    $index = 0;

    // 1st Loop through order items
    foreach( $order->get_items() as $item ){
        $index++;
        // 2nd Loop through each unit related to item quantity
        for($i = 1; $i <= $item->get_quantity(); $i++){
            $my_field = get_post_meta($order->get_id(),'_my_field_'.$index.'_'.$i, true );
            if (! empty($my_field) ){
                echo '<p><strong>'.__('My Field')." <em>(item $index - $i)</em>".':</strong> ' . $my_field . '</p>';
            }
        }
    }
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试并有效

Code goes in function.php file of your active child theme (or active theme). Tested and works

这篇关于保存自定义结帐字段并将其显示在admin Woocommerce Orders中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆