【问题标题】:Add custom column with customer total spent on WooCommerce admin orders list添加自定义列,其中包含在 WooCommerce 管理订单列表上花费的客户总数
【发布时间】:2022-07-11 02:46:00
【问题描述】:

我们有很多回头客,所以我试图在 WooCommerce 管理订单列表上显示用户生命周期价值(LTV - 所有过去订单的总和),以快速识别最佳用户。

但我什至不知道这是否可能。我有一个自定义列来查看它是否是回头客,但不知道从哪里开始 LTV。

我用于标记退货买家的代码:

add_filter( 'manage_shop_order_posts_columns', 
'shalior_wc_set_custom_edit_post_columns',99,1 );
function shalior_wc_set_custom_edit_post_columns($columns) {
    $columns['is-returning'] = __( 'Is returning?', 'your_text_domain' );
    return $columns;
}

add_action( 'manage_shop_order_posts_custom_column' , 'shalior_wc_is_returning', 99, 2 );
function shalior_wc_is_returning( $column, $post_id ) {
switch ( $column ) {

    case 'is-returning':
        $order = new WC_Order( $post_id );
        $user_id = $order->get_user_id();
        $orders_count = wc_get_customer_order_count( $user_id );
        echo $orders_count > 1 ? '<span style="color: #040404; background: #9ae288; 
  padding: 3px; padding-left: 12px; padding-right: 12px; border-radius: 3px;">Yes</span>' : "No" ;
        break;
}
} 

add_action('admin_head', 'my_custom_fonts');

function my_custom_fonts() {
    echo '<style>
    td.is-returning.column-is-returning {
    text-align: center;
}
th#is-returning {
    text-align: center;
}
th.manage-column.column-is-returning {
    text-align: center;
}
td.order_coupons.column-order_coupons {
    text-align: center;
}
th#order_coupons {
    text-align: center;
}
th.manage-column.column-order_coupons {
    text-align: center;
}
</style>';
}

【问题讨论】:

    标签: wordpress woocommerce orders


    【解决方案1】:

    您可以使用get_total_spent() 函数,返回客户花费的金额

    所以你得到:

    /**
     * Add column
     */
    function filter_manage_edit_shop_order_columns( $columns ) {
        $columns['total-spent'] = __( 'LTV', 'woocommerce' );
        
        return $columns;
    }
    add_filter( 'manage_edit-shop_order_columns', 'filter_manage_edit_shop_order_columns', 10, 1 );
    
    /**
     * Populate column
     */
    function filter_manage_shop_order_posts_custom_column( $column, $post_id ) {
        // Compare
        if ( $column == 'total-spent' ) {
            // Get order
            $order = wc_get_order( $post_id );
    
            // Is a WC_Order
            if ( is_a( $order, 'WC_Order' ) ) {
                // Get user id
                $user_id = $order->get_user_id();
                
                // NOT empty
                if ( ! empty ( $user_id ) ) {
                    // Get customer
                    $customer = new WC_Customer( $user_id );
                    
                    // Output
                    echo $customer->get_total_spent();
                } else {
                    // Output
                    echo __( 'N/A', 'woocommerce' );
                }
            }
        }
    }
    add_filter( 'manage_shop_order_posts_custom_column', 'filter_manage_shop_order_posts_custom_column', 10, 2 );
    

    【讨论】:

      猜你喜欢
      • 2021-07-16
      • 1970-01-01
      • 2019-04-02
      • 1970-01-01
      • 2018-01-12
      • 2019-04-25
      • 2020-10-12
      • 2018-09-09
      • 1970-01-01
      相关资源
      最近更新 更多