为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕哥6287543 2021-06-29 17:59:41
基于“在购物车和订单项目名称下显示 woocommerce 产品自定义字段”对我之前的一个问题的回答,代码在产品编辑页面上显示自定义字段。填写这些字段后,产品页面上会显示数据。这是代码:// Backend: Display additional product fieldsadd_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 fieldsadd_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']));    }}如何在管理面板的编辑订单页面上显示自定义字段?它应该在产品名称之后显示这些字段。
查看完整描述

1 回答

?
慕尼黑的夜晚无繁华

TA贡献1864条经验 获得超6个赞

以下是在管理订单项目上显示您的产品自定义字段的方法:


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>';

    }

}

代码位于活动子主题(或活动主题)的 functions.php 文件中。测试和工作。

//img1.sycdn.imooc.com//60dec88e00012eeb07740202.jpg

查看完整回答
反对 回复 2021-07-02
  • 1 回答
  • 0 关注
  • 324 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信