gpt4 book ai didi

php - 在 WooCommerce 中获取订单小计值(value)

转载 作者:行者123 更新时间:2023-12-02 15:19:40 25 4
gpt4 key购买 nike

我使用模块计算本地承运人的送货,但模块不计算总运费。
wp-content/plugins/woocommerce/templates/order/order-details.php 中,我添加了以下代码以获取运费和小计金额:

     <?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" 获得总计。

什么可以用来获得工作小计?

最佳答案

第一个 'Order_subtotal''shop_order' post-type 的 wp_postmeta 表中不存在。

最后一件非常重要的事情:不要直接在 woocommerce 插件中覆盖模板,以免在更新插件时丢失您的更改。您可以更好地通过复制'templates'文件夹到您的事件子主题或主题并重命名来覆盖这些文件它 woocommerce。
请参阅此引用资料:Overriding WooCommerce Templates via a Theme ……


更改代码(答案)

因为您已经在该模板的开头 $order = wc_get_order( $order_id );,您可以使用类 WC_Abstract_Order native 函数 get_subtotal( )get_total( ) 直接来自 $order,无需使用 get_post_meta() Wordpress 函数。

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

<?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>

这应该可以如您所愿地工作,但这不会更新订单总数,它只会显示它。

引用资料:

关于php - 在 WooCommerce 中获取订单小计值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38105310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com