如何获得不含税的

如何获得不含税的

本文介绍了如何获得不含税的 WooCommerce 订单总销售额?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

所以我想计算我网站的总销售额(不含税).但是,我在网站上有大量订单.使它崩溃页面,因为它无法处理计算.有没有更好的方法可以从 WooCommerce 计算/检索它?

So i want to calculate the total sale amount, excluding tax, for my website. However, i have a enormous load of orders on the website. Making it crash the page because it can't handle the calculation. Is there a better way to calculate / retrieve this from WooCommerce?

function calculateTotalSales(){

    $orders = get_posts( array(
        'numberposts' => - 1,
        'post_type'   => array( 'shop_order' ),
        'post_status' => array( 'wc-completed', 'wc-processing', 'wc-pending' )
    ) );

    $total = 0;
    foreach ( $orders as $customer_order ) {
        $order = wc_get_order( $customer_order );
        $total += $order->get_total() - $order->get_total_tax();
    }

    update_option('totalSales', $totalSales);
    return $totalSales;

}

推荐答案

您可以使用这个自定义函数,该函数使用 WordPress WPDB 获取订单总销售额(不含税).

You can use this custom function that uses a very lightweight SQL query using WordPress WPDB Class to get orders total sales (excluding taxes).

它将从具有已完成"、正在处理"、暂停"状态的订单中获取总销售额.和待定"状态.

It will get total sales from orders with "completed", "processing", "on-hold" and "pending" status.

主要功能代码:

function get_orders_total_sales( $type = 'excluding' ) {
    global $wpdb;

    // Excluding taxes (by default)
    if ( 'excluding' === $type ) {
        $column = 'net_total';
    }
    // Including taxes
    elseif ( 'including' === $type ) {
        $column = 'total_sales';
    }
    // only taxes
    elseif ( 'taxes' === $type ) {
        $column = 'tax_total';
    }
    // only shipping
    elseif ( 'shipping' === $type ) {
        $column = 'shipping_total';
    }

    return (float) $wpdb->get_var("
        SELECT SUM($column)
        FROM {$wpdb->prefix}wc_order_stats
        WHERE status IN ('wc-completed','wc-processing','wc-on-hold','wc-pending')
    ");
}

然后你可以在你自己的函数中使用它,比如:

Then you can use it in your own function like:

function calculateTotalSales(){
    total_sales = get_orders_total_sales(); // get orders total sales (excluding taxes)
    update_option( 'totalSales', total_sales ); // Save it as a setting option
    return total_sales;
}

代码位于活动子主题(或活动主题)的functions.php 文件中.在 WooCommerce 4+ 中测试并运行.

Code goes in functions.php file of the active child theme (or active theme). Tested and works in WooCommerce 4+.

该函数还允许获取:

  • 订单总销售额(含税):get_orders_total_sales('including')
  • 订单总销售额(仅税):get_orders_total_sales('taxes')
  • 订单总销售额(仅发货):get_orders_total_sales('shipping')

这篇关于如何获得不含税的 WooCommerce 订单总销售额?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 16:35