检查Woocommerce中购买的产品到期日期

时间:2018-02-13 05:21:16

标签: php wordpress date woocommerce product

我有一些代码可以检查woocommerce中产品的购买日期。

以下是代码:

function _cmk_check_ordered_product( $id ) {
    // Get All order of current user
    $orders = get_posts( array(
        'numberposts' => -1,
        'meta_key'    => '_customer_user',
        'meta_value'  => get_current_user_id(),
        'post_type'   => wc_get_order_types( 'view-orders' ),
        'post_status' => array_keys( wc_get_order_statuses() )
    ) );

    if ( !$orders ) return false; // return if no order found

    $all_ordered_product = array(); // store products ordered in an array

    foreach ( $orders as $order => $data ) { // Loop through each order
        $order_data = new WC_Order( $data->ID ); // create new object for each order
        foreach ( $order_data->get_items() as $key => $item ) {  // loop through each order item
            // store in array with product ID as key and order date a value
            $all_ordered_product[ $item['product_id'] ] = $data->post_date; 
        }
    }
    if ( isset( $all_ordered_product[ $id ] ) ) { // check if defined ID is found in array
        return 'You purchased this product on '. date('M. d, Y', strtotime( $all_ordered_product[ $id ] ) );
    } else {
        return 'Product Never Purchased';
    }
}

所以它只是检查购买日期。现在我需要检查当前日期是否距离购买日期不到15天,如果是我回应:“此产品已过期”

我正在考虑使用PHP Date,例如if ($date < strtotime('-15 days'))。但我被卡住了。什么是正确的方向?我该如何解决?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

下面是一个轻松有效的SQL查询,您将获得您期望的内容(以及更多内容):

  • 将检查产品是否已经购买
  • 它将检查购买日期是否已定义剩余天数的佣人。

由于客户可以多次购买相同的产品(产品),因此该功能将考虑已定义产品ID的最后一个购买项目。

代码:

function check_ordered_product( $product_id = 0, $days = 15, $user_id = 0 ) {
    global $wpdb;
    $customer_id = $user_id == 0 || $user_id == '' ? get_current_user_id() : $user_id;
    $statuses    = array_map( 'esc_sql', wc_get_is_paid_statuses() );

    $time_n_days_ago = strtotime("-$days days");
    $found = $valid = false;

    // The query
    $results = $wpdb->get_col( "
        SELECT p.post_date 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_status IN ( 'wc-" . implode( "','wc-", $statuses ) . "' )
        AND pm.meta_key = '_customer_user'
        AND pm.meta_value = $customer_id
        AND woim.meta_key IN ( '_product_id', '_variation_id' )
        AND woim.meta_value = $product_id
    " );

    if( count($results) > 0 ){
        $found = true;
        foreach( $results as $post_date ){
            if( strtotime($post_date) > $time_n_days_ago ){
                $valid = true;
                break;
            }
        }
    }
    if ( $found && ! $valid ) {
        $text = __( "This product has expired.", "woocommerce" );
    } elseif ( $found && $valid ) {
        $text = __( "This product is still valid.", "woocommerce" );
    } else {
        $text = __( "You have not purchased this product yet.", "woocommerce" );
    }
    echo '<p>'.$text.'</p>';
}
前端登录用户的

用法 (示例)

对于过去15天的产品ID 37,您将使用:

check_ordered_product( 37, 15 );

将显示:

  • 如果产品在过去15天后购买:此产品已过期。
  • 如果产品在过去15天之前购买:此产品仍然有效。
  • 如果尚未购买产品:您尚未购买此产品。

相关类似的答案:WooCommerce - Check if user purchased a product in the past 60 days