【问题标题】:How to subtract subtotal from total in WooCommerce?如何从 WooCommerce 中的总计中减去小计?
【发布时间】:2022-01-03 19:51:38
【问题描述】:

在 WooCommerce 中,我想知道是否有一种简单的解决方案可以在不增加费用的情况下从总数中减去小计?并让它出现在所有地方,例如结帐、订单 (Woocommerce)、我的订单 (my-account/view-order/) 等。

这样做的原因是我在产品中添加了自定义费用/附加费,这是小计的 10% 预订费,我只想先支付附加费,然后离线(如收款)。

例如

  • 小计 = 购买价格 = 100 英镑
  • 预订费 = 10 英镑
  • Ontop 管理费 = 5 英镑
  • 现在支付总额 = 15 英镑

那么,有没有一种简单的方法可以从完整订单表以及购物车/结帐中的总计中删除小计?谢谢

add_filter( 'woocommerce_calculated_total', 'custom_cart_total', 20, 2 );
function custom_cart_total( $total, $cart ) {
    return $total - $subtotal;
}

【问题讨论】:

    标签: php wordpress woocommerce cart subtotal


    【解决方案1】:

    woocommerce_calculated_total 过滤器钩子确实可以使用,只有$subtotal 在您当前的代码中未定义。

    所以你得到:

    // Allow plugins to filter the grand total, and sum the cart totals in case of modifications.
    function filter_woocommerce_calculated_total( $total, $cart ) { 
        // Get subtotal
        $subtotal = $cart->get_subtotal();
        
        return $total - $subtotal;
    }
    add_filter( 'woocommerce_calculated_total', 'filter_woocommerce_calculated_total', 10, 2 );
    

    更新:

    首先,此挂钩有效,新的总数显示在订单详细信息表、WooCommerce 电子邮件通知等中。但是,当对订单进行更改时,所有内容都会重新计算。

    为了解决这个问题,我们可以在重新计算时操纵总数。

    第 1 步: 添加特定元数据(如果适用于 WooCommerce 结帐后的当前订单)

    /**
     * Action hook fired after an order is created used to add custom meta to the order.
     *
     * @since 3.0.0
     */
    function action_woocommerce_checkout_update_order_meta( $order_id, $data ) {    
        // Get an instance of the WC_Order object
        $order = wc_get_order( $order_id );
        
        // Is a WC_Order
        if ( is_a( $order, 'WC_Order' ) ) {     
            // Get subtotal
            $subtotal = $order->get_subtotal();
            
            // Get total
            $total = $order->get_total();
            
            // Total is less than subtotal
            if ( $total < $subtotal ) {
                // Save the order data and meta data
                $order->update_meta_data( '_is_recalculated_order_id', $order_id );
                $order->save();
            }
        }
    }   
    add_action( 'woocommerce_checkout_update_order_meta', 'action_woocommerce_checkout_update_order_meta', 10, 2 );
    

    第 2 步:在针对此特定订单进行更改时操纵总数

    function filter_woocommerce_order_get_total( $total, $order ) {
        global $pagenow;
        
        // Only on order edit page
        if ( $pagenow != 'post.php' || get_post_type( $_GET['post'] ) != 'shop_order' ) return $total;
        
        // Get meta
        $is_recalculated_order_id = $order->get_meta( '_is_recalculated_order_id' );
        
        // NOT empty and meta value is equal to current order ID
        if ( ! empty ( $is_recalculated_order_id ) && $is_recalculated_order_id == $order->get_id() ) {     
            // Get subtotal
            $subtotal = $order->get_subtotal();
            
            // Subtotal is less than total
            if ( $subtotal < $total ) {         
                // Manipulate
                $total = $total - $subtotal;
            }
        }
        
        return $total;
    }
    add_filter( 'woocommerce_order_get_total', 'filter_woocommerce_order_get_total', 10, 2 );
    

    这样做的缺点是它仅适用于通过订单编辑页面进行的更改。要从 WooCommerce 管理订单列表中也应用此功能,您可以删除 if 条件,但更改不仅会应用于当前订单,还会应用于所有先前的订单(如果适用)。

    简而言之:远非理想的解决方案

    【讨论】:

    • 谢谢@7uc1f3r - 答案似乎在购物车和结帐时完美运行,但不会出现在 WooCommerce 订单的后端或我的帐户 -> 订单的用户端。那么请问是否有另一个过滤器可用于在 Orders 表中创建相同的输出(总计 - 小计)?谢谢
    • 嗨@7uc1f3r - 我发现了这个问题。正如您提到的,过滤器适用于所有订单表。但是,当我选择“将发票/订单详细信息发送给客户”并进行更新时。然后“订单更新并发送”。以某种方式重新计算完整订单并且不减去小计。
    • @Ross 我明白了,如果您进行调整,一切都会重新计算。我相信您可以通过 woocommerce_order_get_total 过滤器挂钩来防止这种情况。只有此挂钩适用于所有订单,因此您需要额外检查,以便仅将其应用于当前订单。简而言之,有点棘手。我相信如果我重新阅读您的问题,您最好在订单详细信息表中添加额外的行。总额(因为它是原始的)和额外的行显示“Total to Pay Now”和另一行“离线支付”,这样您在(重新)计算/更改订单时不会遇到不愉快的意外
    • 谢谢。请注意,目前系统上没有过去或当前的订单,并且在某个特定时间只能购买一种产品。因此,考虑到使用“woocommerce_order_get_total”可能行得通吗?
    • @Ross 答案已更新,但正如我已经说过的,这远非理想
    猜你喜欢
    • 1970-01-01
    • 2018-05-12
    • 1970-01-01
    • 2014-12-22
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多