我的订单格式为[domain]/checkout/order-received/[order_number]/key=[wc-order-key]
- 如何获得[wc-order-key]
?
到目前为止我已经完成了:
add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1);
function custom_process_order($order_id)
{
$order = new WC_Order( $order_id );
$myuser_id = (int)$order->user_id;
$user_info = get_userdata($myuser_id);
$items = $order->get_items();
foreach ($items as $item)
{
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
$product_description = get_post_meta($item['product_id'])->post_content
}
return $order_id;
}
答案 0 :(得分:8)
如果我理解正确,你需要通过order_id获取order_key,这是正确的吗? 如果是这样,您可以使用WC_Order属性:
$test_order = new WC_Order($order_id);
$test_order_key = $test_order->order_key;
答案 1 :(得分:1)
随着WooCommerce 3更改了调用属性的方式,获取相同信息的适当方法是:
$order = wc_get_order($order_id);
// Added a check to make sure it's a real order
if ($order && !is_wp_error($order)) {
$order_key = $order->get_order_key();
}
请注意,您可以轻松地反向执行此操作:从订单密钥获取订单ID:
$order_id = wc_get_order_id_by_order_key($order_key);