这是一个函数,它使我能够在前端(我的帐户)中为WooCommerce订单创建按日期过滤字段.它是有效的,但是当我 Select 没有订单的日期时,过滤字段消失,并且没有办法再次过滤,我必须手动从URL中删除添加的参数.
我想实现以下目标:当没有订单时,过滤器字段必须仍然可见,才能 Select 不同的日期,还必须出现重置按钮,这将重置过滤器.
以下是我当前的代码:
add_action('woocommerce_before_account_orders', 'add_form');
function add_form($has_orders)
{
if ($has_orders) {
echo
'</form>
<form action="#" method="get">
<input type="date" name="start_date">
<input type="date" name="end_date">
<input type="submit" value="' . esc_attr__( 'Filter by date', 'your-text-domain' ) . '">
</form>';
}
}
add_filter( 'woocommerce_my_account_my_orders_query', 'custom_woocommerce_my_account_my_orders_query', 10, 1);
function custom_woocommerce_my_account_my_orders_query($array) {
$start_date='';
$end_date='';
if ( isset($_GET['start_date']) && isset($_GET['end_date'])){
$start_date = $_GET['start_date'];
$end_date = $_GET['end_date'];
}
$array['date_query'] = array(
array(
'after' => $start_date, //start_date 2021-11-16
'before' => $end_date, //end_date
'inclusive' => true,
),
);
return $array;
}