在WooCommerce中结账后的变更单合计 [英] Change order total after checkout in Woocommerce
本文介绍了在WooCommerce中结账后的变更单合计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我似乎找不到在用户单击结账后使用哪个钩子来更改总数(或购物车的任何变量)。例如,用户提交了结账表单,然后我需要进行一些检查并相应地更改总数。
我应该如何做,我应该使用哪个挂钩?
推荐答案
可以在woocommerce_checkout_create_order
操作挂钩中完成,在该钩子中,您必须为WC_Abstract_Order
和WC_Order
类使用CRUD getters和setters方法...
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 );
}
代码放在活动子主题(或主题)的函数.php文件中。
已测试并正常工作。
这里有一些解释:Add extra meta for orders in Woocommerce
这篇关于在WooCommerce中结账后的变更单合计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文