2016-07-02 3 views
1

У меня есть петля продукта в Woocommerce. Если клиент купил продукт раньше, я хотел бы заменить кнопку продукта покупки сообщением и датой заказа. Например.Как получить дату покупки из заказа woocommerce?

«Вы купили этот продукт на 15 января 2016»

я могу получить идентификатор продукта, и я могу получить текущий идентификатор пользователя, но не могу понять, как использовать эти кусочки информации, чтобы тянуть Идентификаторы ордеров.

$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); 

Идеи?

+1

введите код. –

+0

Не могли бы вы объяснить, на какой странице вы должны отобразить эту новую кнопку? – purvik7373

ответ

1

Вы можете использовать эту функцию ниже

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

например Показаны сообщения на странице с одним товаром echo _cmk_check_ordered_product(get_the_ID());

+0

Хорошо работает. Спасибо. –

Смежные вопросы