当前位置: 首页 > 知识库问答 >
问题:

Wordpress date_query行为异常

柴晔
2023-03-14

这里有点小麻烦。我正在尝试编写一些代码来确定当前用户是否有:一个超过30天的帐户和0个woocommerce订单。我有这个顶部部分提取所有的订单属于用户在过去90天。

然而,它返回0个订单,即使我的测试用户有1个已完成的订单。我预感可能是这行:“before”=>date('y-m-d',strtotime('now'))所以我去掉了它,然后它返回“19个订单”,这简直是不可能的。print_r部分似乎只是显示了1个顺序的乱码元数据,所以它从哪里得到19个,我只是不能理解!

function woo_reg_matured( $days_old = 30 )
{
    $cu = wp_get_current_user();
    return ( isset( $cu->data->user_registered ) && strtotime( $cu->data->user_registered ) < strtotime( sprintf( '-%d days', $days_old ) ) ) ? TRUE : FALSE;    
}
function woo_inactive_shortcode( $atts = array(), $content = '' ) {
    if ( is_user_logged_in() ):
        $customer_orders = get_posts( array(
        'numberposts' => - 1,
        'meta_key'    => '_customer_user',
        'meta_value'  => get_current_user_id(),
        'post_type'   => array( 'shop_order' ),
        'post_status' => array( 'wc-completed' ),
'date_query' => array(
    'after' => date('Y-m-d', strtotime('-90 days'))
) ) );
    $total = 0;
        print_r($customer_orders);
    foreach ( $customer_orders as $customer_order ) {
        $order = wc_get_order( $customer_order );
        $total += $order->get_total();
        }
        echo 'Orders: ' . $total . '<br>';
        if ($total <= 5) {
                if( woo_reg_matured(  $days_old = 30 ) ) {
            echo 'You are inactive with ' . $total . ' orders and account age over 30 days';
            return;         
            }
        echo 'You are inactive with ' . $total . ' orders and account age under 30 days';
        return;
        }
        echo 'You are active with over 30 days age and ' . $total . ' orders';
        return;
    endif;
}
add_shortcode( 'woo_inactive',   'woo_inactive_shortcode' );

数组([0]=>WP_Post对象([ID]=>75675[post_author]=>1[post_date]=>2018-01-17 12:16:28[post_contite]=>[post_title]=>订单-2018年1月17日@12:16 PM[post_excerpt]=>[post_status]=>wc-已完成[comment_status]=>打开[ping_status]=>关闭[post_password]=>order_5a5f3e9c442d6[post_name]=>订单-1月17日-2018-1216-PM

但是$Total显示:订单:19

19从何而来!!!!LOL

共有1个答案

梁丘书
2023-03-14

像这样试试。

    if ( is_user_logged_in() ):
        $customer_orders = get_posts( array(
        'numberposts' => - 1,
        'meta_key'    => '_customer_user',
        'meta_value'  => get_current_user_id(),
        'post_type'   => array( 'shop_order' ),
        'post_status' => array( 'wc-completed' ),
        'date_query' => 
        array('after' =>
                array(
                  'year'  => 2017,
                  'month' => 03,
                  'day'   => 12,
                ),
            'before' =>
                array(
                  'year'  => 2018,
                  'month' => 01,
                  'day'   => 17,
                ),  
        ) ) );
    $total = 0;
    foreach ( $customer_orders as $customer_order ) {
        $order = wc_get_order( $customer_order );
        print_r($customer_order);
        //$total++;
          $total += $order->get_total();
        }
        echo 'Orders: ' . $total . '<br>';
        if ($total <= 5) {
              /*  if( woo_reg_matured(  $days_old = 30 ) ) {
            echo 'You are inactive with ' . $total . ' orders and account age over 30 days';
            return;         
            }*/
        echo 'You are inactive with ' . $total . ' orders and account age under 30 days';
        return;
        }
        echo 'You are active with over 30 days age and ' . $total . ' orders';
        return;
    endif;
 类似资料:
  • 问题内容: 当运行上述代码片段时,在控制台中,我得到的输出为: 但我期望输出为 但是当在main()内部更改如下所述的逻辑时 我正在获得所需的输出 根据我的理解,TreeMap的entrySet()方法返回地图中包含的映射的集合视图。集合的迭代器以升序键顺序返回映射。那么,为什么在第一种情况下会发生这种情况? 任何建议都将受到高度赞赏。 问题答案: 因为在字典上小于。 这里有一个提示: 这是另一个

  • 假设我们的最大内存为256M,为什么这段代码可以工作: 但这一个会发出声音吗?

  • 我正在设计一个网站,有一些h3标题和段落,包装在一个名为“featured-info”的div类中。此外,我还有一个footer元素,它位于主体中的主包装器中。各段用斜体字写成: 并且页脚有边框: 页脚文本也是一个h4向上感知: 主要的问题是我有一个设置:@media screen和(最小宽度:750px),它使一些导航按钮内联,并调整一些文本的大小,但是...当页面大小小于750px时,页脚样式

  • 我在ios7和iOS8中都遇到了一些奇怪的行为。。 有时全屏显示X(POTRAIT全屏广告-我的应用程序仅为横向)。你点击X,就可以返回我的菜单了。 但有时会显示不带X的广告(LANDSCAPE FUllSCREEN AD)。如果您等待委托永远不会被调用。所以我尝试单击它以离开。然后它会显示另一个带有X的广告(LANDSCAPE FUllSCREEN AD)。所以我单击X。然后它会转到另一个广告(

  • 我的tmp目录无法通过php访问。因此,会话不能真正工作(他们没有),但在这种情况下,他们做了一些未知的原因。 这是一个场景, 我有一个页面index.php验证用户的凭据和设置会话变量,并将用户重定向到panel.php panel.php我有密码, 当我登录通过index.php,虽然会话数据无法读取,panel.php打开像它通常与会话 刷新页面时,会话消失,页面重定向! 对这种行为有什么解

  • 我有一个基于PHP的应用程序,它使用Azure队列。我的应用程序有一个cron,每20分钟在我的Web服务器上运行一次,它将多达2000条消息添加到Azure队列存储。它过去把这些消息放在一个队列中,但现在我使用两个不同的队列,或者把消息放在每个队列中。在每条消息添加到队列后,还有十分之一秒的Hibernate期,以避免节流。 我在Azure上有大约40个VM从这个队列中读取,处理每个消息,然后在