在 Woocommerce 中为订单添加额外的元数据 [英] Add extra meta for orders in Woocommerce

查看:28
本文介绍了在 Woocommerce 中为订单添加额外的元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的网站创建自定义插件.

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

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

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

这是add_item_meta()函数:

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

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

有人知道这件事吗?

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

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

解决方案

2018之路:

基于 Guido W.P. 答案,您可以改用 woocommerce_checkout_create_order 动作挂钩更轻量、更有效的版本代码(使用WC 3+ CRUD方法):

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

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

在 WooCommerce 3+ 中测试并运行 (仅).

<小时>

一些解释:

woocommerce_checkout_create_order 操作挂钩只是保存订单数据之前的一个步骤.请参阅下面 WC_Checkout create_order() 方法的摘录(带有两个钩子):

/*** 动作挂钩可在保存前调整顺序.* @自 3.0.0*/do_action('woocommerce_checkout_create_order', $order, $data);//保存订单.$order_id = $order->save();do_action('woocommerce_checkout_update_order_meta', $order_id, $data );返回 $order_id;

<块引用>

为什么使用 woocommerce_checkout_create_order 代替?:

  • 因为您不需要使用 $order = wc_get_order( $order_id ); 因为您已经将 $order 作为参数在挂钩函数中.
  • 你不需要使用 $order->save(); 因为这会在之后完成 (查看源代码)
  • 此钩子自 WooCommerce 版本 3 以来已发布,其目的相同,允许使用所有可用的 WC_Order 方法.

所以这仅适用于在函数内部使用一行代码.

I'm creating a custom plugin for my website.

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

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:

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!

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

解决方案

The 2018 way:

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

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).

Tested and works in WooCommerce 3+ (only).


SOME EXPLANATIONS:

The woocommerce_checkout_create_order action hook is just one step before saving the order data. See below in an extract of the WC_Checkout create_order() method (with both hooks):

/**
 * 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_order instead?:

  • Because You don't need to use $order = wc_get_order( $order_id ); as you already got $order as 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)
  • This hook has been released since WooCommerce version 3 and it's made for the same purpose, allowing to use all available WC_Order methods.

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

这篇关于在 Woocommerce 中为订单添加额外的元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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