针对自定义行为的Woocommerce钩子对于下订单
问题描述:
我不知道是否有一个钩子用于在点击时更改“下单按钮”行为。我试图在下单时更换/更换产品,因为产品选择(所有可用产品)也都位于结帐页面中。针对自定义行为的Woocommerce钩子对于下订单
到目前为止,我一直试图操纵woocommerce_checkout_order_review
挂钩&失败。
add_action('woocommerce_checkout_order_review', 'remove_woocommerce_product');
function remove_woocommerce_product(){
if (isset($_POST['woocommerce_checkout_place_order'])){
global $woocommerce;
$woocommerce->cart->empty_cart(); // Empty the cart
$selectedproduct = $_POST['selectedproductid']; // Get the selected product
WC()->cart->add_to_cart($selectedproduct); // Insert the selected product in the the cart
return esc_url(wc_get_checkout_url()); // Redirect to Payment Gateway Page
}
}
上面的挂钩不会在下订单时触发。也许我的代码有问题,或者我怀疑是错误的钩子应用。有任何想法吗?
答
没关系......找到了答案......
add_action('woocommerce_checkout_process', 'change_product_upon_submission');
function change_product_upon_submission() {
if (!empty($_POST['_wpnonce']) && !empty($_POST['selectedproductid'])) {
$selectedproduct = $_POST['selectedproductid']; // Get the selected product
WC()->cart->empty_cart(); //Empty the cart
WC()->cart->add_to_cart($selectedproduct); // Insert the selected product in the cart
}
}
有关详细说明,请参见Woocommerce Replace Product in Cart Upon Place Order in Checkout Page