在 Woocommerce 中结帐后更改订单总数
Change order total after checkout in Woocommerce
我似乎找不到在用户点击结帐后使用哪个挂钩来更改总数(或购物车的任何变量)。因此,例如,用户提交结帐表单,然后我需要进行一些检查并相应地更改总数。
我应该怎么做,我应该使用哪个钩子?
这可以在 woocommerce_checkout_create_order
action hook, where you will have to use CRUD getters and setters methods for WC_Abstract_Order
and WC_Order
类...
中完成
由于购物车对象和购物车会话还没有被销毁,你仍然可以使用WC()->cart
对象和WC_Cart
方法来获取数据…
这个挂钩在订单数据用 $order->save();
保存到数据库之前被触发。可以看到in the source code HERE.
下面是一个假的工作示例:
add_action( 'woocommerce_checkout_create_order', 'change_total_on_checking', 20, 1 );
function change_total_on_checking( $order ) {
// Get order total
$total = $order->get_total();
## -- Make your checking and calculations -- ##
$new_total = $total * 1.12; // <== Fake calculation
// Set the new calculated total
$order->set_total( $new_total );
}
代码进入您的活动子主题(或主题)的 function.php 文件。
已测试并有效。
这里有一些解释:Add extra meta for orders in Woocommerce
我似乎找不到在用户点击结帐后使用哪个挂钩来更改总数(或购物车的任何变量)。因此,例如,用户提交结帐表单,然后我需要进行一些检查并相应地更改总数。
我应该怎么做,我应该使用哪个钩子?
这可以在 woocommerce_checkout_create_order
action hook, where you will have to use CRUD getters and setters methods for WC_Abstract_Order
and WC_Order
类...
由于购物车对象和购物车会话还没有被销毁,你仍然可以使用WC()->cart
对象和WC_Cart
方法来获取数据…
这个挂钩在订单数据用 $order->save();
保存到数据库之前被触发。可以看到in the source code HERE.
下面是一个假的工作示例:
add_action( 'woocommerce_checkout_create_order', 'change_total_on_checking', 20, 1 );
function change_total_on_checking( $order ) {
// Get order total
$total = $order->get_total();
## -- Make your checking and calculations -- ##
$new_total = $total * 1.12; // <== Fake calculation
// Set the new calculated total
$order->set_total( $new_total );
}
代码进入您的活动子主题(或主题)的 function.php 文件。
已测试并有效。
这里有一些解释:Add extra meta for orders in Woocommerce