在 WooCommerce 的订单编辑页面上显示自定义字段

基于“在购物车和订单项目名称下显示 woocommerce 产品自定义字段”对我之前的一个问题的回答,代码在产品编辑页面上显示自定义字段。填写这些字段后,产品页面上会显示数据。


这是代码:


// 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']));

    }

}


如何在管理面板的编辑订单页面上显示自定义字段?它应该在产品名称之后显示这些字段。


慕哥6287543
浏览 325回答 1
1回答

慕尼黑的夜晚无繁华

以下是在管理订单项目上显示您的产品自定义字段的方法:add_action( 'woocommerce_before_order_itemmeta', 'add_admin_order_item_custom_fields', 10, 2 );function add_admin_order_item_custom_fields( $item_id, $item ) {&nbsp; &nbsp; // Targeting line items type only&nbsp; &nbsp; if( $item->get_type() !== 'line_item' ) return;&nbsp; &nbsp; $product = $item-> get_product();&nbsp; &nbsp; $value1&nbsp; = $product->get_meta('_custom_weight');&nbsp; &nbsp; $value2&nbsp; = $product->get_meta('_сalories');&nbsp; &nbsp; $value3&nbsp; = $product->get_meta('_ingredients');&nbsp; &nbsp; if ( ! empty($value1) || ! empty($value2) || ! empty($value3) ) {&nbsp; &nbsp; &nbsp; &nbsp; echo '<table cellspacing="0" class="display_meta">';&nbsp; &nbsp; &nbsp; &nbsp; if ( ! empty($value1) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<tr><th>' . __("Weight", "woocommerce") . ':</th><td>' . $value1 . 'g</td></tr>';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if ( ! empty($value2) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<tr><th>' . __("Calories", "woocommerce") . ':</th><td>' . $value2 . 'kcal</td></tr>';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if ( ! empty($value3) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<tr><th>' . __("Ingredients", "woocommerce") . ':</th><td>' . $value3 . '</td></tr>';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; echo '</table>';&nbsp; &nbsp; }}代码位于活动子主题(或活动主题)的 functions.php 文件中。测试和工作。
打开App,查看更多内容
随时随地看视频慕课网APP