当前位置: 首页 > 知识库问答 >
问题:

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

竺和洽
2023-03-14

基于“在购物车和订单项名称下显示伍兹商业产品自定义字段”回答我以前的一个问题,代码在产品编辑页面上显示自定义字段。当填写这些字段时,数据将显示在产品页面上。

下面是代码:

// 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;
}

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

更新

据我所知,woocommerce\u checkout\u create\u order\u line\u item对此负责。但我无法添加正确的代码。我请求你的帮助!

// 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;
}

// 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 );
    }
}

共有1个答案

汪阿苏
2023-03-14

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

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

代码进入函数。活动子主题(或活动主题)的php文件。测试和工作。

相关:
也为可变产品在WooCommerce管理订单项目上显示产品自定义字段

 类似资料:
  • 在Woocommerce中,下面的代码检索特定的自定义订单项目元数据,并在“管理编辑订单”页面的“常规”区域下添加一个可编辑的自定义字段: 现在,我试图用下面的代码保存自定义的可编辑字段值,但一旦我点击update,它就不会保存任何内容。 我做错了什么?如何保存此自定义字段值? 任何帮助都将不胜感激。

  • 是否有一种方法可以在WooCommerce订单页面的每个产品下显示我的自定义SKU? 当我编辑产品时,自定义sku显示良好,但它不会显示在产品的订单页面中。我需要在订单上显示此信息,以便Zapier可以将其与产品的Visma帐户软件ArticleID匹配。 此尝试基于如何向WooCommerce产品添加(第二个)自定义sku字段的解决方案?

  • 我正在尝试从一个简单的wooCommerce商店的订单详细信息中删除“postcustom”元框。div#postcustom按顺序显示--- 我(目前)已将其连接到: 我也尝试过将“dashboard”和“post”作为$context,但都没有用。 我也尝试过用钩子删除meta框、admin init和其他一些。 我工作在一个子主题functions.php并使用默认的ooCommerce主题

  • 我创建了一些代码来显示woocommerce上的库存可用性 问题是,如果我更改了自定义字段文本,那么所有以前的订单都会用新的自定义字段文本更新。 例如:一个客户在2021年1月确认了一个产品的订单,其中自定义字段文本是“产品将在2021年3月提供”。第二客户在2021年3月确认了相同的产品订单,其中新的自定义字段文本是“产品将在2021年5月提供”。 两份订单都显示了最后一个自定义字段文本“产品将

  • 我正在搜索和尝试它2天没有成功,请帮助。 我想筛选woocommerce订单,以便根据产品属性将其他详细信息从db添加到订单详细信息页面,但我找不到适合此任务的woocommerce操作/筛选器挂钩。这里假设变量; 如果,那么我需要将自定义数据从数据库添加到订单详细信息页面。 注意:我不想添加额外的元框,而是想更改订单明细表: 将默认产品映像替换为存储在数据库和中的映像, 在产品名称下面添加包含自

  • 我有一家woocommerce商店,想定制我的产品。用户直接访问类别页面,无店铺存档页面。 你有没有什么好的方法来定制订单?我试着使用插件,并给那些产品一个编号。。。但一切都没有奏效。 问候