用于下订单的自定义行为的 Woocommerce 挂钩

Woocommerce Hook for Custom Behaviour for Place Order

我想知道是否有一个钩子可以在单击时更改下订单按钮的行为。我正在尝试 replace/change 下订单时购买产品,因为产品选择(所有可用产品)也在结帐页面中。

到目前为止,我一直在尝试操纵 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
     }
}

更多解释,见