php 在 Woocommerce 中为订单添加额外的元数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25626058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:55:19  来源:igfitidea点击:

Add extra meta for orders in Woocommerce

phpwordpresswoocommercemetadataorders

提问by Mo Saeedi

I'm creating a custom plugin for my website.

我正在为我的网站创建一个自定义插件。

In some part of this plugin I need to store extra meta in wp_postmetafor each orders.

在这个插件的某些部分,我需要wp_postmeta为每个订单存储额外的元数据。

I added this in my plugin's class:

我在我的插件类中添加了这个:

add_action ('woocommerce_before_checkout_process', array( &$this, 'add_item_meta', 10, 2) );

And this is add_item_meta()function:

这是add_item_meta()功能:

function add_item_meta( $item_id, $values ) {
  wc_add_order_item_meta($item_id, '_has_event', 'yes' );
}

This function is not complete, but nothing happens with this codes; I think I need to use another hook but I can't find a proper one.

此功能不完整,但此代码没有任何反应;我想我需要使用另一个钩子,但我找不到合适的钩子。

Does anyone know anything about this?

有人对这个有了解吗?

I also have another problem with $item_id: this is woocommerce global variable but I can't see it in my plugin!

我还有另一个问题$item_id:这是 woocommerce 全局变量,但我在插件中看不到它!

I mean I don't have access to this variable from my plugin or something like this!

我的意思是我无法从我的插件或类似的东西访问这个变量!

回答by LoicTheAztec

The 2018 way:

2018年的方式:

Built on Guido W.P.answer you can use instead woocommerce_checkout_create_orderaction hook in a more lighter and effective version code (using WC 3+ CRUD methods):

基于Guido WP答案,您可以woocommerce_checkout_create_order在更轻量和更有效的版本代码中使用动作钩子(使用WC 3+ CRUD 方法

add_action('woocommerce_checkout_create_order', 'before_checkout_create_order', 20, 2);
function before_checkout_create_order( $order, $data ) {
    $order->update_meta_data( '_custom_meta_key', 'value' );
}

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

代码位于活动子主题(或活动主题)的 function.php 文件中。

Tested and works in WooCommerce 3+ (only).

在 WooCommerce 3+ (仅)中测试和工作。



SOME EXPLANATIONS:

一些解释:

The woocommerce_checkout_create_orderaction hook is just one step before saving the order data. See below in an extractof the WC_Checkoutcreate_order()method (with both hooks):

woocommerce_checkout_create_order行动钩保存订单数据前,只需一个步骤。请参阅下面的方法摘录WC_Checkoutcreate_order()(带有两个钩子):

/**
 * Action hook to adjust order before save.
 * @since 3.0.0
 */
do_action( 'woocommerce_checkout_create_order', $order, $data );

// Save the order.
$order_id = $order->save();

do_action( 'woocommerce_checkout_update_order_meta', $order_id, $data );

return $order_id;

Why using woocommerce_checkout_create_orderinstead?:

  • Because You don't need to use $order = wc_get_order( $order_id );as you already got $orderas an argument in the hooked function.
  • You don't need to use $order->save();as this will be done just after anyway (see the source code)
  • Also woocommerce_checkout_create_orderhas been released in WooCommerce version 3 and it's maid for that too.

So this just works with a single line of codeinside the function.

为什么用woocommerce_checkout_create_order呢?

  • 因为您不需要使用,$order = wc_get_order( $order_id );因为您已经$order在挂钩函数中作为参数使用。
  • 您不需要使用,$order->save();因为无论如何这都会完成(请参阅源代码)
  • woocommerce_checkout_create_order已在 WooCommerce 版本 3 中发布,它也是如此。

所以这只适用于函数内的一行代码

回答by Guido Walter Pettinari

Building on Mo Saeedi answer, I believe this snippet is more in line with the new CRUD approachintroduced by WooCommerce 3.0:

基于 Mo Saeedi 的回答,我相信这个片段更符合WooCommerce 3.0 引入的新 CRUD 方法

add_action('woocommerce_checkout_update_order_meta',function( $order_id, $posted ) {
    $order = wc_get_order( $order_id );
    $order->update_meta_data( 'my_custom_meta_key', 'my data' );
    $order->save();
} , 10, 2);

See also this threads on the WordPress forums:

另请参阅 WordPress 论坛上的此主题:

回答by Mo Saeedi

answer is: I should use woocommerce_checkout_update_order_metafor add_action and also i should simply use update_post_meta()to add extra meta to my order

答案是:我应该woocommerce_checkout_update_order_meta用于 add_action 并且我应该简单地用于update_post_meta()向我的订单添加额外的元数据

    function add_item_meta( $order_id ) {
            //global $woocommerce;
            update_post_meta( $order_id, '_has_event', 'yes' );
        } 

回答by Sorin C

The 2016 way:

2016年的方式:

add_action('woocommerce_checkout_update_order_meta',function( $order_id, $posted ) {  
    update_post_meta( $order_id, 'my_custom_meta_key', 'my data' );  
} , 10, 2);
  • $order_idis the id of the order, which is stored as a custom post type
  • $postedis all the data from $_POST
  • $order_id是订单的id,存储为自定义帖子类型
  • $posted是所有数据来自 $_POST