【问题标题】:Can't find customers order and product ID找不到客户订单和产品 ID
【发布时间】:2015-11-24 09:18:17
【问题描述】:

我正在使用具有会员资格和订阅扩展的 woo-commerce。我正在尝试显示当前用户订阅的到期日期,我需要订阅密钥才能访问它。

要找到这个键,我需要 product_id 和 order_id 和(因为这两个值组成了键,例如“254_119”。)。但是当我尝试使用

访问当前用户订单时
$order = new WC_Order(ID);
print_r($order);

订单数据为空,我也尝试访问数据库中的user_meta,才意识到它只包含运输信息等。

是否可以访问当前用户最近的订单?我读过我可能需要使用

get_posts() 

但我不确定访问所需数据的参数。

这似乎也不起作用。

$customer_orders = get_posts($order_count);

【问题讨论】:

  • 全球 $woocommerce, $post; $order = new WC_Order($post->ID);

标签: php wordpress woocommerce


【解决方案1】:

如果我正确理解您的问题,那么方法应该是:

获取当前用户的最近最近订单

/*Get most recent order of customer*/
$recent_orders = get_posts( array(
    'numberposts' => 1,
    'meta_key'    => '_customer_user',
    'meta_value'  => get_current_user_id(), //Current user's id
    'post_type'   => wc_get_order_types(),
    'post_status' => array_keys( wc_get_order_statuses() ),
) );

现在我们有最新的订单,我们可以从该订单中获得order_id

$order_id = $recent_orders[0]->ID; //Order Id

现在要获取该订单的产品,我们可以执行以下操作:

$order = new WC_Order($order_id);
$cart_item = $order->get_items();//Get Cart Items of that order

现在我们同时拥有 product_idorder_id,所以我们可以制作密钥:

foreach($cart_item as $item){
    echo "Product Name : ".$item['name'];
    echo "<br>";
    echo "Product Id : ".$item['product_id'];
    echo "<br>";
    echo "Your Key ".$order_id."_".$item['product_id'];
    echo "<br>";
}

所有代码放在一起:

<?php 
    /*Get most recent order of customer*/
    $recent_orders = get_posts( array(
        'numberposts' => 1,
        'meta_key'    => '_customer_user',
        'meta_value'  => get_current_user_id(), //Current user's id
        'post_type'   => wc_get_order_types(),
        'post_status' => array_keys( wc_get_order_statuses() ),
    ) );

    $order_id = $recent_orders[0]->ID; //Order Id
    $order = new WC_Order($order_id);

    $cart_item = $order->get_items();//Get Cart Items of that order
    foreach($cart_item as $item){
        echo "Product Name : ".$item['name'];
        echo "<br>";
        echo "Product Id : ".$item['product_id'];
        echo "<br>";
        echo "Your Key ".$order_id."_".$item['product_id'];
        echo "<br>";
    }
?>

如果您有任何疑问,请告诉我。

【讨论】:

  • 绝对完美,就像我需要的一样,非常感谢!我注意到在收到的订单页面上,我可以回显订单号和产品 ID,因此我打算将这两个值保存到单独的会话中,然后在我正在尝试的页面上使用它们。这个解决方案更好!
  • 这个答案不再起作用了。 $item['product_id'] 已经不存在了
猜你喜欢
  • 1970-01-01
  • 2018-08-26
  • 1970-01-01
  • 1970-01-01
  • 2012-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多