prefix}posts WHE-6ren">
gpt4 book ai didi

php - Woocommerce:从最新订单获取数据

转载 作者:行者123 更新时间:2023-11-29 16:24:47 25 4
gpt4 key购买 nike

到目前为止,我已经能够获取使用此命令创建的最新完成订单的 ID:

<?php

function get_last_order_id(){
global $wpdb;
$statuses = "wc-completed";

// Getting last Order ID (max value)
$results = $wpdb->get_col( "
SELECT MAX(ID) FROM {$wpdb->prefix}posts
WHERE post_type LIKE 'shop_order'
AND post_status IN ('$statuses')
" );
return reset($results);
}

$latest_order_id = get_last_order_id();
echo ($latest_order_id);

但是,我想要获取 billing_first_name,而不是获取 ID。有谁知道如何获得吗?

最佳答案

您也可以使用联接查询,请检查下面的查询是否相同。

    SELECT MAX(P.ID) ,PM.meta_value as firstname
FROM {$wpdb->prefix}posts AS P
INNER JOIN {$wpdb->prefix}postmeta AS PM
ON P.ID = PM.post_id

WHERE P.post_type LIKE 'shop_order'
AND P.post_status IN ('$statuses')
AND PM.meta_key='_billing_first_name'

只需将您的查询替换为给定的查询即可。

完整的代码如下。

        <?php
function get_last_order_id()
{
global $wpdb;
$statuses = "wc-completed";

// Getting last Order ID (max value)
$results = $wpdb->get_col( "
SELECT MAX(P.ID) ,PM.meta_value
FROM {$wpdb->prefix}posts AS P
INNER JOIN {$wpdb->prefix}postmeta AS PM
ON P.ID = PM.post_id

WHERE P.post_type LIKE 'shop_order'
AND P.post_status IN ('$statuses')
AND PM.meta_key='_billing_first_name'
" );
return reset($results);
}

$latest_order_id = get_last_order_id(); // Last order ID
echo ($latest_order_id);

关于php - Woocommerce:从最新订单获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54288528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com