wordpress - 动态添加自定义产品数据作为订单上的项目元数据

标签 wordpress woocommerce product hook-woocommerce orders

是否可以在订单内的产品上添加元数据?

在这种情况下,将有一个元数据,但每个产品(按顺序)将具有不同的值(value)。
例子:

Order 1:
     * Product 1 
     Sample Meta: Meta1

     * Product 2
     Sample Meta: Meta2

谢谢。

更新 1:

我现在被困在如何从 woocommerce_add_cart_item_data 中获取值筛选。我能够从那里成功添加元数据。

我需要获取这些值供我在此 woocommerce_add_order_item_meta 中使用 Action 钩。

这就是我成功地将元数据添加到过滤器的方式:
function add_cart_item_data( $cart_item_data, $product_id ) {
    $cart_item_data[ "meta1" ] = $_POST["meta1"];  
    $cart_item_data[ "meta2" ] = $_POST["meta2"]; 
    return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_data', 99, 2 );

最佳答案

是的,这可以使用 Hook 在 中的自定义函数来实现。 woocommerce_add_order_item_meta Action 钩。

add_action('woocommerce_add_order_item_meta','adding_custom_data_in_order_items_meta', 1, 1 );
function adding_custom_data_in_order_items_meta( $item_id, $values, $cart_item_key ) {

    // The corresponding Product Id for the item:
    $product_id = $values[ 'product_id' ];

    $custom_meta_value = $values['my_custom_field1_key'];
    // or $custom_meta_value = $_POST['my_custom_field_key'];
    // or $custom_meta_value = get_post_meta( $values[ 'product_id' ], '_some_meta_key', true );

    if ( !empty($custom_meta_value) ) 
        wc_add_order_item_meta($item_id, 'custom_meta_key', $custom_meta_value, true);

    // And so on …
}

But as your question is not detailed and you are not showing any code related to how this custom data is set in your products or passed to the cart object, is not possible to help more that that.



与您的答案相关的更新: (在链接的答案中查看一个真实的例子):

Adding user custom field value to order items details

So, as in this linked answer, you will need to create first a product attribute because in your code, wc_add_order_item_meta($item_id, 'The Meta', $the_meta ); is not correct as the second argument has to be a meta_key slug without uppercase and space characters, so 'The Meta' is not convenient and not recommended...



此产品属性创建名称将是(关于您的回答代码): 'The Meta' 和蛞蝓 'the_meta' .

然后,您必须在每个相关产品中使用强制值(任何值,因为此值将被您在下面的自定义值替换)中设置它。

所以一旦完成,您的代码将是:
add_action('woocommerce_add_order_item_meta','adding_custom_data_in_order_items_meta', 10, 3 );
function adding_custom_data_in_order_items_meta( $item_id, $values, $cart_item_key ) {
    if ( isset($values['meta1']) && isset($values['meta2']) ) {
        $custom_value = $values['meta1'] . '.' .  $values['meta2'];
        wc_add_order_item_meta($item_id, 'pa_the-meta', $custom_value );
    }
}

然后你会在你的订单项目中得到这种显示('XXXX'是你的自定义值):
The Meta: XXXX

关于wordpress - 动态添加自定义产品数据作为订单上的项目元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41971376/

相关文章:

Woocommerce - 仅显示循环中的分组产品父级

php - 向 WooCommerce 单个产品页面添加多个选项卡

php - 通过phpMyAdmin(Wordpress)上传.sql文件后定位或建立MySQL UN和PW

php - 我需要了解 php 和 mySQL 将静态网站转换为 WordPress 主题吗

php - 如何在 WooCommerce 中添加产品排序下拉列表?

php - 将自定义列添加到 WooCommerce 管理优惠券列表

php - 如果 WooCommerce 短代码没有返回任何产品,则显示一条消息

php - 我在 wamp 中的 mysql 数据库有什么问题?

css - 向侧边栏添加左边框(Wordpress 24 主题)

css - WooCommerce 后端的超大信用卡图标