在WooCommerce的订单编辑页面上显示自定义字段 [英] Show custom fields on the order editing page in WooCommerce

查看:70
本文介绍了在WooCommerce的订单编辑页面上显示自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于


Based on "Show woocommerce product custom fields under the cart and order item name" answer to one of my previous questions, The code shows custom fields on the product edit page. When these fields are filled in, data is displayed on the product page.

Here is the code:

// Backend: Display additional product fields
add_action('woocommerce_product_options_general_product_data', 'add_fields_to_options_general_product_data');

function add_fields_to_options_general_product_data() {
    // Custom Weight Field
    woocommerce_wp_text_input(array(
            'id' => '_custom_weight',
            'label' => __('Weight dishes', 'woocommerce'),
    ));

    // Calories Field
    woocommerce_wp_text_input(array(
            'id' => '_сalories',
            'label' => __('Calories', 'woocommerce'),
    ));

    // Ingredients Field
    woocommerce_wp_textarea_input(array(
            'id' => '_ingredients',
            'label' => __('Ingredients', 'woocommerce'),
    ));
}

// Backend: Save the data value from the custom fields
add_action('woocommerce_admin_process_product_object', 'save_admin_product_custom_fields_values');

function save_admin_product_custom_fields_values($product) {
    // Save Custom Weight Field
    if (isset($_POST['_custom_weight'])) {
            $product->update_meta_data('_custom_weight', sanitize_text_field($_POST['_custom_weight']));
    }

    // Save Calories Field
    if (isset($_POST['_сalories'])) {
            $product->update_meta_data('_сalories', sanitize_text_field($_POST['_сalories']));
    }

    // Save Ingredients Field
    if (isset($_POST['_ingredients'])) {
            $product->update_meta_data('_ingredients', sanitize_textarea_field($_POST['_ingredients']));
    }
}

// Display product custom fields values on single product pages under short description and on archive pages
add_action('woocommerce_before_add_to_cart_form', 'display_custom_meta_field_value', 25);
add_action('woocommerce_after_shop_loop_item', 'display_custom_meta_field_value', 25);

function display_custom_meta_field_value() {
    global $product;

    if ($custom_weight = $product->get_meta('_custom_weight'))
            echo '<p id="value-on-single-product">'.__("Weight:", "woocommerce").
    ' '.$custom_weight.
    'g'.
    '</p>';

    if ($сalories = $product->get_meta('_сalories'))
            echo '<p id="value-on-single-product">'.__("Calories:", "woocommerce").
    ' '.$сalories.
    ' kcal.'.
    '</p>';

    if ($ingredients = $product->get_meta('_ingredients'))
            echo '<p id="value-on-single-product">'.__("Ingredients:", "woocommerce").
    ' '.$ingredients.
    '</p>';
}

// Add custom fields values under cart item name in cart
add_filter('woocommerce_cart_item_name', 'custom_cart_item_name', 10, 3);

function custom_cart_item_name($item_name, $cart_item, $cart_item_key) {
    if (!is_cart())
            return $item_name;

    if ($value1 = $cart_item['data']->get_meta('_custom_weight')) {
            $item_name. = '<br><span class="custom-field"><strong>'.__("Weight", "woocommerce").
            ':</strong> '.$value1.
            'g</span>';
    }

    if ($value2 = $cart_item['data']->get_meta('_сalories')) {
            $item_name. = '<br><span class="custom-field"><strong>'.__("Calories", "woocommerce").
            ':</strong> '.$value2.
            'kcal</span>';
    }

    if ($value3 = $cart_item['data']->get_meta('_ingredients')) {
            $item_name. = '<br><span class="custom-field"><strong>'.__("Ingredients", "woocommerce").
            ':</strong> <br>'.$value3.
            '</span>';
    }

    return $item_name;
}

// Display custom fields values on orders and email notifications
add_filter('woocommerce_order_item_name', 'custom_order_item_name', 10, 2);

function custom_order_item_name($item_name, $item) {
    $product = $item - > get_product();

    if ($value1 = $product->get_meta('_custom_weight')) {
            $item_name. = '<br><span class="custom-field"><strong>'.__("Weight", "woocommerce").
            ':</strong> '.$value1.
            'g</span>';
    }

    if ($value2 = $product->get_meta('_сalories')) {
            $item_name. = '<br><span class="custom-field"><strong>'.__("Calories", "woocommerce").
            ':</strong> '.$value2.
            'kcal</span>';
    }

    if ($value3 = $product->get_meta('_ingredients')) {
            $item_name. = '<br><span class="custom-field"><strong>'.__("Ingredients", "woocommerce").
            ':</strong> <br>'.$value3.
            '</span>';
    }

    return $item_name;
}

How to display custom fields on the edit order page in the admin panel? It is supposed to show these fields after the product name.


UPDATE

As I understand it, woocommerce_checkout_create_order_line_item is responsible for this. But I can't add the correct code. I ask for your help!

// Save chosen slelect field value to each order item as custom meta data and display it everywhere
add_action('woocommerce_checkout_create_order_line_item', 'save_order_item_product', 10, 4 );
function save_order_item_product( $item, $cart_item_key, $values, $order ) {
    $product = $item->get_product();

    if( $value1 = $product->get_meta('_custom_weight') ) {
        $item_name .= '<br /><span class="custom-field"><strong>' . __("Weight dishes", "woocommerce") . ':</strong> ' . $value1 . ' g</span>';
    }
    return $item_name;
}

or

// Save chosen slelect field value to each order item as custom meta data and display it everywhere
add_action('woocommerce_checkout_create_order_line_item', 'save_order_item_product', 10, 4 );
function save_order_item_product( $item, $cart_item_key, $values, $order ) {
    if( isset($values['_custom_weight']) ) {
        $key = __('Weight dishes', 'woocommerce');
        $value = $values['_custom_weight'];
        $item->update_meta_data( $key, $value );
    }
}

解决方案

Here is the way to display your product custom fields on admin order items:

add_action( 'woocommerce_before_order_itemmeta', 'add_admin_order_item_custom_fields', 10, 2 );
function add_admin_order_item_custom_fields( $item_id, $item ) {
    // Targeting line items type only
    if( $item->get_type() !== 'line_item' ) return;

    $product = $item-> get_product();
    $value1  = $product->get_meta('_custom_weight');
    $value2  = $product->get_meta('_сalories');
    $value3  = $product->get_meta('_ingredients');

    if ( ! empty($value1) || ! empty($value2) || ! empty($value3) ) {
        echo '<table cellspacing="0" class="display_meta">';

        if ( ! empty($value1) ) {
            echo '<tr><th>' . __("Weight", "woocommerce") . ':</th><td>' . $value1 . 'g</td></tr>';
        }

        if ( ! empty($value2) ) {
            echo '<tr><th>' . __("Calories", "woocommerce") . ':</th><td>' . $value2 . 'kcal</td></tr>';
        }

        if ( ! empty($value3) ) {
            echo '<tr><th>' . __("Ingredients", "woocommerce") . ':</th><td>' . $value3 . '</td></tr>';
        }
        echo '</table>';
    }
}

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

这篇关于在WooCommerce的订单编辑页面上显示自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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