通过结算名称的Woocommerce订单

时间:2017-10-24 16:13:39

标签: php mysql wordpress woocommerce

我需要通过结算名字来订购帮助,如果无法完成,请按customer_id分组。'

现在我正在使用wc_get_orders($product_id);哪个正常。它不允许任何参数。

enter image description here

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:0)

$orders= wc_get_orders( array(
'orderby' => 'billing_first_name',
'order' => 'ASC',
) );

答案 1 :(得分:0)

  

您可以使用WP_Query来获取,而不是使用wc_get_orders   订单然后形成你可以创建订单对象的ID然后   你喜欢什么。

像这样:

$args = array(
    'numberposts' => -1, //to get all post, you can use Pagination
    'post_type' => 'shop_order',
    'post_status' => array_keys(wc_get_order_statuses()),
    'meta_key' => '_billing_first_name', //order by first name
    'orderby' => 'meta_value',
    'order' => 'ASC',
    'fields' => 'ids'
);

$orders = new WP_Query($args);
//echo '<pre>';
//print_r($orders->posts);
if (!empty($orders->posts))
{
    foreach ($orders->posts as $order_id)
    {
        $order = wc_get_order($order_id); //getting order Object
        //Now that you have the order object make the magic happen here.
        echo $order->get_billing_first_name() . '<br>';
    }
    /* Restore original Post Data */
    wp_reset_postdata();
}
else
{
    // no posts found
}

print_r(array_keys(wc_get_order_statuses()));将输出如下内容:

Array
(
    [0] => wc-pending
    [1] => wc-processing
    [4] => wc-on-hold
    [5] => wc-completed
    [6] => wc-cancelled
    [7] => wc-refunded
    [8] => wc-failed
)

<小时/> <强>已更新

  

如果您想按产品ID获取订单,则必须使用   自定义查询,因为WP_Query不适用于非自定义MySQL   表

以下是替代方案:

/**
 * method to get the order ID(s) by product ID(s)
 * @param int|array $product_id
 * @return array
 */
function wh_getOrdersByProductId($product_id)
{
    global $wpdb;

    if(empty($product_id))
        return [];

    $operator = is_array($product_id) ? 'IN' : '=';

    $query = "SELECT p.ID AS order_id
    FROM {$wpdb->prefix}posts AS p
    INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
    INNER JOIN {$wpdb->prefix}woocommerce_order_items AS woi ON p.ID = woi.order_id
    INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
    WHERE p.post_type = 'shop_order'
      AND p.post_status IN ('wc-pending', 'wc-completed', 'wc-processing', 'wc-on-hold', 'wc-cancelled', 'wc-refunded', 'wc-failed')
      AND pm.meta_key = '_billing_first_name'
      AND woim.meta_key = '_product_id'
      AND woim.meta_value {$operator} ({$product_id})
      AND woi.order_item_type = 'line_item'
    ORDER BY pm.meta_key ASC;";

    return $wpdb->get_col($wpdb->prepare($query));
}

代码进入您的活动子主题(或主题)的functions.php文件。或者也可以在任何插件php文件中。

<强> USAGE

//$orders = wh_getOrdersByProductId([37, 45]);
$orders = wh_getOrdersByProductId(37);

if (!empty($orders))
{
    foreach ($orders as $order_id)
    {
        $order = wc_get_order($order_id); //getting order Object
        //Now that you have the order object make the magic happen here.
        echo $order->get_billing_first_name() . '<br>';
    }
}
else
{
    // no posts found
}

有用的链接:

代码经过测试并有效。

希望这有帮助!

答案 2 :(得分:0)

如果有人对这里感兴趣的是长版本...我确定WP_Query版本更加简单,我只是无法弄明白。< / p>

SELECT order_items.order_id FROM {$wpdb->prefix}woocommerce_order_items as order_items LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id LEFT JOIN {$wpdb->postmeta} AS postmeta ON order_items.order_id = postmeta.post_id AND postmeta.meta_key = '_billing_first_name' LEFT JOIN {$wpdb->posts} AS posts ON order_items.order_id = posts.ID WHERE posts.post_type = 'shop_order' AND posts.post_status IN ( '" . implode( "','", $order_status ) . "' ) AND order_items.order_item_type = 'line_item' AND order_item_meta.meta_key = '_product_id' AND order_item_meta.meta_value = '$product_id' ORDER BY postmeta.meta_value DESC