【发布时间】:2018-02-28 18:46:46
【问题描述】:
在用户单击结帐后,我似乎无法找到要使用哪个挂钩来更改总数(或购物车的任何变量)。例如,用户提交结帐表格,然后我需要做一些检查并相应地更改总数。
我应该怎么做,我用哪个钩子?
【问题讨论】:
标签: php wordpress woocommerce hook-woocommerce orders
在用户单击结帐后,我似乎无法找到要使用哪个挂钩来更改总数(或购物车的任何变量)。例如,用户提交结帐表格,然后我需要做一些检查并相应地更改总数。
我应该怎么做,我用哪个钩子?
【问题讨论】:
标签: php wordpress woocommerce hook-woocommerce orders
这可以在 woocommerce_checkout_create_order 动作钩子中完成,您必须在 WC_Abstract_Order 和 WC_Order 类中使用 CRUD getter 和 setter 方法...
由于购物车对象和购物车会话尚未销毁,您仍然可以使用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 文件中。
经过测试并且有效。
【讨论】:
WC_Checkout create_order_line_items() method source code