[英]woocommerce, how to create a condition for purchase date?
我已經詳細說明了隨附的代碼,以便召回用戶購買的所有產品。 但是,此時我想插入一個條件,例如:僅顯示過去 3 天購買的產品。 但是,到目前為止,我從未冒險過ifs。 有人知道如何創造這樣的條件嗎?
<?php
$order_status = array_keys( wc_get_order_statuses());
$customer_orders = get_posts( apply_filters( 'woocommerce_my_account_my_orders_query', array(
'numberposts' => - 1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => wc_get_order_types( 'view-orders' ),
'post_status' => $order_status,
) ) );
foreach ( $customer_orders as $customer_order ) {
$order = new WC_Order( $customer_order );
foreach ( $order->get_items() as $item_id => $item ) :
endforeach;
$date = esc_html( wc_format_datetime( $order->get_date_created() ) );
?>
<?php $date_comp = $order->get_date_completed(); ?>
<?php $product = wc_get_product( $item->get_product_id() ); ?>
<?php $product_id = $item->get_product_id(); ?>
}
我曾經使用這些條件的 select 訂單
'date_query' => array(
'after' => date('Y-m-d', strtotime('-3 days'))
)
'date_query' => array(
array(
'after' => $date_start,
'before' => $date_end,
'inclusive' => true,
),
),
您可以使用wc_get_orders
來傳遞date_created
參數。 有關更多信息,請在此處查看 - WC_Order_Query 。 試試下面的代碼。
// Get last 3 days orders.
$args = array(
'numberposts' => -1,
'orderby' => 'date',
'order' => 'DESC',
'customer_id' => get_current_user_id(),
'date_created' => '>' . date( 'Y-m-d', strtotime('- 3day' ) ),
'customer_id' => get_current_user_id(),
);
$customer_orders = wc_get_orders( $args );
//* Loop through each WC_Order object
foreach( $customer_orders as $customer_order ){
$order = new WC_Order( $customer_order );
foreach ( $order->get_items() as $item_id => $item ) :
endforeach;
$date = esc_html( wc_format_datetime( $order->get_date_created() ) );
$date_comp = $order->get_date_completed();
$product = wc_get_product( $item->get_product_id() );
$product_id = $item->get_product_id();
}
或者您可以在get_posts
中使用date_query
。
<?php
$order_status = array_keys( wc_get_order_statuses());
$customer_orders = get_posts( apply_filters( 'woocommerce_my_account_my_orders_query', array(
'numberposts' => - 1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => wc_get_order_types( 'view-orders' ),
'post_status' => $order_status,
'date_query' => array(
'after' => date( 'Y-m-d', strtotime('-3 days') )
)
) ) );
foreach ( $customer_orders as $customer_order ) {
$order = new WC_Order( $customer_order );
foreach ( $order->get_items() as $item_id => $item ) :
endforeach;
$date = esc_html( wc_format_datetime( $order->get_date_created() ) ); ?>
<?php $date_comp = $order->get_date_completed(); ?>
<?php $product = wc_get_product( $item->get_product_id() ); ?>
<?php $product_id = $item->get_product_id(); ?>
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.