【问题标题】:How to check if current user has a pending order for specific product in WooCommerce?如何检查当前用户是否在 WooCommerce 中有特定产品的挂单?
【发布时间】:2021-02-09 13:41:31
【问题描述】:

我正在为客户开发插件 如何检索用户在产品页面订购的订单状态? 如果该客户对该产品的该订单的订单状态等于wc-pending,我应该条件在页面中显示某些内容 所以我在产品页面,想根据登录用户购买产品的订单状态显示元素,我该如何实现这个目标?

    $orders = wc_get_orders($user_id);
    $product_id = $product->get_id();
    <?php if(//status of purchased of current user for this product is equall wc-pending){ ?>
        <a href="<?php echo home_url('redirect-to-room/' . $product->get_id()) ?>" class="button alt">
            <?php _e('Enter room', 'skyroom') ?>
        </a>
    <?php } ?>

当前获取当前用户的订单列表,但不知道如何获取该产品页面的订单状态。 提前致谢

【问题讨论】:

  • 对我来说似乎更简单、更清晰的解决方案是创建一个自定义用户元数据,该元数据将包含一个值(用作检查,因此很可能是“真”或“假”),当订单状态更改为wc-pending。然后您需要从订单中获取客户 ID 并添加自定义元数据。您可以使用 WooCommerce 的 woocommerce_order_status_changed 函数来执行此操作。 woocommerce.github.io/code-reference/files/…

标签: php wordpress woocommerce


【解决方案1】:

wc_get_orders 接收一个数组作为输入参数:

$args = array(
    'status'    => 'wc-pending',  
    'customer'  => get_current_user_id(),  
);

$pending_orders = wc_get_orders( $args );

if( ! empty( $pending_orders ) ) {
    .............
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-17
    • 2016-12-10
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 2019-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多