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 条件,但更改不仅会应用于当前订单,还会应用于所有先前的订单(如果适用)。
简而言之:远非理想的解决方案