处理后按顺序显示产品自定义字段值 [英] Displaying product custom fields values in the order once processed

查看:93
本文介绍了处理后按顺序显示产品自定义字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在woocommerce上,我正在使用以下代码在购物车和结帐时呈现一些产品自定义字段:

On woocommerce, I am using the code below to render some product custom fields, on cart and on checkout:

add_filter( 'woocommerce_get_item_data', 'rendering_meta_field_on_cart_and_checkout', 10, 2 );

function rendering_meta_field_on_cart_and_checkout( $cart_data, $cart_item ) {
    $custom_items = array();

    if( !empty( $cart_data ) ) {
        $custom_items = $cart_data;
    }
    if( isset( $cart_item['wccpf_enter_product_id'] ) ) {

        $diamond = $cart_item['wccpf_enter_product_id'];

        $pacolor = get_the_terms($diamond, 'pa_color');
        foreach ( $pacolor  as $pacolor  ) {
           $color= $pacolor ->name;
        }


        $custom_items[] = array( "name" => "color", "value" => $color);
    }
    return $custom_items;
}

如何在订单中显示自定义产品字段wccpf_enter_product_id'的值?

How can I display that custom product fields wccpf_enter_product_id' value in orders?

谢谢。

推荐答案

您可以使用挂在woocommerce_add_order_item_meta操作中的自定义函数钩实现这一目标。

You can use a custom function hooked in woocommerce_add_order_item_meta action hook to achieve this.


您需要首先在产品中添加一个属性,以便为您的自定义字段值获得一个可读的干净标签

为此,您必须先创建一个属性,然后在产品中为其设置任何值(如您将在下面的代码中替换此值)。

For that you have to create an attribute first and then set it in your product with any value (as you will replace this value in the code below).

请参见 此处 有关该过程的更多解释……

See HERE some more explanations about that process…

您必须在我的代码中替换<通过您在 wp_woocommerce_order_itemmeta <上找到的特定自定义键来实现strong> 'custom_field_key' / strong> MySQL表,其中包含特定订单ID的对应项目ID。

要为订单找到相应的 商品ID ,可以在 中搜索wp_woocommerce_order_items 带有订单ID的MySQL表…

You will have to replace in my code the 'custom_field_key' by your specific custom key that you will find on wp_woocommerce_order_itemmeta MySQL table for the corresponding item ID for your specific Order ID.
To find the corresponding item ID for the order, you can search in wp_woocommerce_order_items MySQL table with the Order ID…

您还必须设置正确的属性slug而不是 'pa_your_attribute' ,以在订单中显示此自定义字段值的正确标签文本。

(请参见下面的其他类似答案参考)。

You will have also to set your correct attribute slug instead of 'pa_your_attribute' to display in your orders the correct label text for this custom field value.
(see below other similar answers references).

所以您的代码将像这样:

So your code will be something like this:

// ADD THE INFORMATION AS META DATA SO THAT IT CAN BE SEEN AS PART OF THE ORDER
add_action('woocommerce_add_order_item_meta','add_and_update_values_to_order_item_meta', 1, 3 );
function add_and_update_values_to_order_item_meta( $item_id, $item_values, $item_key ) {

    // Getting your custom product ID value from order item meta
    $custom_value = wc_get_order_item_meta( $item_id, 'custom_field_key', true );

    // Here you update the attribute value set in your simple product
    wc_update_order_item_meta( $item_id, 'pa_your_attribute', $custom_value );
    }
}

代码会出现在您活跃孩子的function.php文件中主题(或主题)或任何插件文件中。

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

这应该有效

相关答案:

动态地将自定义产品数据添加为订单上的商品元数据

在订单商品视图中显示自定义产品数据

这篇关于处理后按顺序显示产品自定义字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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