【问题标题】:Show message if no orders in woocommerce如果 woocommerce 中没有订单,则显示消息
【发布时间】:2016-02-24 12:58:42
【问题描述】:

如果客户没有订单,我想在我的帐户中显示一条消息我想显示“当前没有订单”。 我猜有一个函数可以用来在某个地方挂钩? 进行了很好的搜索,但找不到任何可以让我开始的东西。 谢谢。

【问题讨论】:

    标签: function woocommerce hook message orders


    【解决方案1】:

    this tutorial修改我认为这会起作用:

    function wc_get_customer_orders() {
    
        // Get all customer orders
        $customer_orders = get_posts( array(
            'numberposts' => 1,
            'meta_key'    => '_customer_user',
            'meta_value'  => get_current_user_id(),
            'post_type'   => wc_get_order_types(),
            'post_status' => array_keys( wc_get_order_statuses() ),
        ) );
    
        $customer = wp_get_current_user();
    
        // Text for our message
        $notice_text = sprintf( 'Hey %1$s 😀 We noticed you haven\'t placed any orders with us.', $customer->display_name );
    
        // Display our notice if the customer has no orders
        if ( count( $customer_orders ) == 0 ) {
            wc_print_notice( $notice_text, 'notice' );
        }
    }
    add_action( 'woocommerce_before_my_account', 'wc_get_customer_orders' );
    

    基本上,在帐户页面上,我们查询当前登录用户的单个订单。如果我们没有收到订单,我们会出示通知。

    【讨论】:

    • 感谢您的回复。我看过那个例子并尝试过。它确实适用于我的帐户,但是我正在为我的帐户设计一个选项卡式仪表板,如果没有订单,我希望在订单页面上显示无订单消息。
    • 那么只需使用与woocommerce_before_my_account 不同的钩子即可。或者将该钩子添加到您的其他标签页。
    • 谢谢。我正在处理 my-orders.php,它可以通过。我已经尝试过以下钩子:woocommerce_my_account_my_orders_actions、woocommerce_my_account_my_orders_query、woocommerce_my_account_my_orders_title,但未显示功能/消息。我认为问题可能出在这是一个外部/自定义页面而不是 wp 页面。有没有办法可以将此函数作为 if / else 语句的一部分或包含在包含之前?谢谢,:)
    • 你可以在任何地方运行wc_get_customer_orders(),我认为甚至可以直接调用它。但是为什么要使用外部页面而不是 WP 页面呢?
    • 如果您看到我的网站,可能会更容易解释。这是thecookerytutor dot co .uk。如果您注册帐户或通过联系页面直接与我联系,我可以向您展示我想要实现的目标。谢谢。
    【解决方案2】:

    将 my-orders.php 复制到本地文件夹并添加 else 子句:

    else{
        echo "No orders";
        }
    

    这可以使用 WC css 设置样式

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-24
      • 1970-01-01
      • 2019-08-20
      • 2017-09-28
      • 2016-12-19
      相关资源
      最近更新 更多