duanaozhong0696 2018-02-13 05:21
浏览 81
已采纳

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

I have some code which check the purchased date of a product in woocommerce.

Here is the code:

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';
    }
}

So it just check the purchase date. Now I need to check if the current date is less than 15 days from the purchase date and if it is I echo: "this product is expired".

I am thinking about using PHP Date for this, something like if ($date < strtotime('-15 days')). But I am stuck. What is the right direction? How can I solve it?

Any help is very appreciated.

  • 写回答

1条回答 默认 最新

  • douan5151 2018-02-13 09:42
    关注

    Below is a light and effective SQL query where you will get what you are expecting (and more):

    • It will check that the product has been already bought
    • It will check that the purchase date has been maid in the defined number of remaining days.

    As a customer can purchase multiple times a same iteme (product), the function will take in account the last purchase item for a defined product ID.

    The code:

    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>';
    }
    

    Usage (example) for a logged in user on front end.

    For product ID 37 on past 15 days you will use:

    check_ordered_product( 37, 15 );
    

    It will display:

    • If product has been purchased after the 15 past days: This product has expired.
    • If product has been purchased before the 15 past days: This product is still valid.
    • If product has not been purchased yet: You have not purchased this product yet.

    Related similar answer: WooCommerce - Check if user purchased a product in the past 60 days

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于OPENCV的人脸识别
  • ¥20 51单片机学习中的问题
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!