【问题标题】:Include Custom Order Status in Woocommerce Orders sales reports在 Woocommerce 订单销售报告中包含自定义订单状态
【发布时间】:2018-09-10 14:32:17
【问题描述】:

我有一个自定义订单状态 - 进行中。我的代码如下。它工作得很好 - 但具有此自定义订单状态的订单未包含在标准 Woo 销售报告或 Woocommerce 状态仪表板小部件中。

有人可以帮我看看,看看我可以如何添加到下面的这个 sn-p 中,以便这个自定义订单状态“进行中”的 $ 反映在 Woo 销售报告 $ 中。

// 1 New order status AFTER woo 2.2 IN PROGRESS
add_action( 'init', 'register_my_new_order_statuses' );
function register_my_new_order_statuses() {
    register_post_status( 'wc-in-progress', array(
        'label'                     => _x( 'In Progress', 'Order status', 'woocommerce' ),
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'In Progress <span class="count">(%s)</span>', 'In Progress<span class="count">(%s)</span>', 'woocommerce' )
    ) );
}
add_filter( 'wc_order_statuses', 'my_new_wc_order_statuses' );
// Register in wc_order_statuses.
function my_new_wc_order_statuses( $order_statuses ) {
    $order_statuses['wc-in-progress'] = _x( 'In Progress', 'Order status', 'woocommerce' );
    return $order_statuses;
}

/*
 * 2 CHANGE STATUSES ORDER IN DROPDOWN LIST
 * @param array $wc_statuses_arr Array of all order statuses on the website
 */
function change_statuses_order( $wc_statuses_arr ){

    $new_statuses_arr = array(
        'wc-processing' => $wc_statuses_arr['wc-processing'], // 1
        'wc-in-progress' => $wc_statuses_arr['wc-in-progress'], // 2      
        'wc-completed' => $wc_statuses_arr['wc-completed'], // 3
        'wc-cancelled' => $wc_statuses_arr['wc-cancelled'], // 4
        'wc-refunded' => $wc_statuses_arr['wc-refunded'], // 5
        'wc-failed' => $wc_statuses_arr['wc-failed'], // 6
        'wc-pending' => $wc_statuses_arr['wc-pending'], // 7
        'wc-on-hold' => $wc_statuses_arr['wc-on-hold'] // 8
    );

    return $new_statuses_arr;
}

add_filter( 'wc_order_statuses', 'change_statuses_order' );

/** 3 ADD COLOR TO IN PROGRESS BUTTON **/

add_action('admin_head', 'styling_admin_order_list' );
function styling_admin_order_list() {
    global $pagenow;
    if( $_GET['post_type'] == 'shop_order' && $pagenow == 'edit.php'):

    // HERE below set your custom status
    $order_status = 'In Progress'; // <==== HERE
    ?>
    <style>
        .order-status.status-<?php echo sanitize_title( $order_status ); ?> {
            background: #cc0099;
            color: #ffffff;
        }
    </style>
    <?php
    endif;
}

【问题讨论】:

    标签: php wordpress woocommerce report orders


    【解决方案1】:

    您可以使用以下挂钩函数,将您的“自定义状态”添加到订单报告中:

    add_filter( 'woocommerce_reports_order_statuses', 'include_custom_order_status_to_reports', 20, 1 );
    function include_custom_order_status_to_reports( $statuses ){
        // Adding the custom order status to the 3 default woocommerce order statuses
        return array( 'processing', 'in-progress', 'completed', 'on-hold' );
    }
    

    Code goes in function.php file of your active child theme (or active theme). 已测试并且可以工作。


    在与$_GET['post_type'] == 'shop_order' 相关的第三个函数中生成了一个错误...相反,您可以通过这种方式对其进行更改:

    // 3. ADD COLOR TO IN PROGRESS BUTTON
    add_action('admin_head', 'styling_admin_order_list' );
    function styling_admin_order_list() {
        global $pagenow, $post;
    
        if( $pagenow != 'edit.php') return; // Exit
        if( get_post_type($post->ID) != 'shop_order' ) return; // Exit
    
        // HERE below set your custom status
        $order_status = 'In Progress'; // <==== HERE
        ?>
        <style>
            .order-status.status-<?php echo sanitize_title( $order_status ); ?> {
                background: #cc0099;
                color: #ffffff;
            }
        </style>
        <?php
    }
    

    它将避免这个小错误,因为post_type 不在订单编辑页面的 URL 中(我知道这是我的错,因为这是我的答案之一的代码)...

    【讨论】:

    • 太棒了!它完美运行 - 对 #3 代码的修订也很棒 - 非常感谢 - 你真的帮助我改进了我的商店、我的流程和我的报告与这 2 个 sn-ps - 我非常感激!
    • @yatgirl 欢迎...您甚至可以在后端订单列表中启用您的自定义状态in the bulk action select field
    • 对您给我的第三个函数的更正导致我的 php 错误日志中出现 php 错误。特别是这一行: if( get_post_type($post->ID) != 'shop_order' ) return; // 出口。我的日志中的错误是 PHP Notice: Trying to get property of non-object in /home/xxx/public_html/wp-content/plugins/code-sn-ps/php/sn-p-ops.php(469) : eval()'d 代码在第 48 行。我想知道是否可以对代码进行一些更正以阻止错误。据说是因为 -> @LoicTheAztec
    猜你喜欢
    • 2021-09-04
    • 1970-01-01
    • 2019-11-29
    • 2019-08-27
    • 2018-12-30
    • 2018-08-30
    • 2020-02-01
    • 2019-11-13
    • 2016-04-17
    相关资源
    最近更新 更多