本文介绍了在 WooCommerce 中获取订单小计价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用该模块计算当地承运人的交货,但该模块不计算总运费.
wp-content/plugins/woocommerce/templates/order/order-details.php 中,我添加了以下代码来计算运费和小计:

I use the module for calculating delivery by local carrier, but the module don't calculate shipping costs to the total.
In the wp-content/plugins/woocommerce/templates/order/order-details.php I added the following code to take the amount of shipping and subtotal:

     <?php $a = array(
         get_post_meta($order_id, 'Order_subtotal', true),
         get_post_meta($order_id, 'Econt_Customer_Shipping_Cost', true));
     ?>
     <tr class="total-cost">
         <th><?php _e( 'Total:', 'woocommerce'); ?></th>
         <td><?php echo array_sum($a); ?> <?php echo get_woocommerce_currency_symbol(); ?></td>
     </tr>

因此,我只得到了 "Econt_Customer_Shipping_Cost" 的值,而不是从 "Order_subtotal" 中获取总数.

As a result, I get only the value of "Econt_Customer_Shipping_Cost", but not from "Order_subtotal" to get the total.

请问,谁能告诉我用什么来获得工作小计?

Please, can someone tell me what to use to get a working subtotal?

推荐答案

第一个 'Order_subtotal' 在 wp_postmeta 表中不存在 'shop_order' 帖子类型.

First 'Order_subtotal' does not exist in wp_postmeta table for 'shop_order' post-type.

最后一件非常重要的事情:不要直接在 woocommerce 插件中覆盖模板,以避免在插件更新时丢失您的更改.您可以更好地覆盖这些文件,方法是复制这个'templates'文件夹到您的活动子主题或主题并重命名它woocommerce.
请参阅此参考:通过主题覆盖 WooCommerce 模板...

Last very important thing: Don't overrinde templates directly in woocommerce plugin, to avoid loosing your changes when plugin is updated. You can better override this files by copying this 'templates' folder to your active child theme or theme and rename it woocommerce.
See this reference: Overriding WooCommerce Templates via a Theme…

更改代码(答案)

正如您在此模板的开头 $order = wc_get_order( $order_id );,您可以使用 WC_Abstract_Order 本地函数 get_subtotal( )get_total( ) 直接来自 $order,不使用 get_post_meta() Wordpress 函数.

As you have already at the beginning of this template $order = wc_get_order( $order_id );, you can use the class WC_Abstract_Order native functions get_subtotal( ) or get_total( ) directly from $order, without using get_post_meta() Wordpress function.

我在您的代码中更改了一些内容:

I have changed some things in your code:

<?php 
    $display_sum = $order->get_subtotal( ); // or $order->get_total( );
    $display_sum += get_post_meta( $order->id, 'Econt_Customer_Shipping_Cost', true );
    $display_sum .= ' '. get_woocommerce_currency_symbol( );
?>
<tr class="total-cost">
    <th><?php _e( "Total:", "woocommerce" ); ?></th>
    <td><?php echo $display_sum; ?></td>
</tr>

这应该可以正常工作,但这不会更新订单总数,它只会显示它.

This should work as you want it, but this will not update the order total, it just display it.

参考文献:

这篇关于在 WooCommerce 中获取订单小计价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 03:52