douquanhui5735 2016-06-29 16:29
浏览 108
已采纳

WooCommerce从购物车获得小计值

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>

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?

  • 写回答

3条回答 默认 最新

  • drz49609 2016-06-30 01:19
    关注

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

    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


    Changing your code (the answer)

    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.

    References:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于作物生长模型下,有限水资源的最大化粮食产量的资源优化模型建立
  • ¥20 关于变压器的具体案例分析
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo
  • ¥15 科来模拟ARP欺骗困惑求解
  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式