我已經詳細說明了所附的代碼,以便召回用戶購買的所有產品。但是,此時我想插入一個條件,例如:僅顯示最近 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(); ?>
}
uj5u.com熱心網友回復:
您可以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();
}
或者你可以date_query在get_posts.
<?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(); ?>
}
?>
uj5u.com熱心網友回復:
我曾經使用這些條件選擇訂單
'date_query' => array(
'after' => date('Y-m-d', strtotime('-3 days'))
)
'date_query' => array(
array(
'after' => $date_start,
'before' => $date_end,
'inclusive' => true,
),
),
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/377580.html
標籤:php WordPress的 求购
