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 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划