Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从商业订单中获取购买日期?_Php_Wordpress_Woocommerce - Fatal编程技术网

Php 如何从商业订单中获取购买日期?

Php 如何从商业订单中获取购买日期?,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我在商业中有一个产品循环。如果客户以前购买过产品,我想用消息和订单日期替换“购买产品”按钮。比如说 “您于2016年1月15日购买了此产品” 我可以获得产品id和当前用户id,但不知道如何使用这些信息获取订单id $postid = get_the_ID(); $current_user = wp_get_current_user(); $has_product = wc_customer_bought_product( $current_user->user_email, $curren

我在商业中有一个产品循环。如果客户以前购买过产品,我想用消息和订单日期替换“购买产品”按钮。比如说

“您于2016年1月15日购买了此产品”

我可以获得产品id和当前用户id,但不知道如何使用这些信息获取订单id

$postid = get_the_ID();
$current_user = wp_get_current_user();
$has_product = wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product->id);

想法?

您可以使用下面的功能

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';
    }
}
e、 g。
在单个产品页面上显示消息
echo\u cmk\u check\u ordered\u product(获取\u ID())

请粘贴您的代码。请您解释一下您必须在哪一页显示这个新按钮?效果很好。非常感谢。