Kaydarin
Kaydarin

Reputation: 391

Woocommerce Hook for Custom Behaviour for Place Order

I wonder if there's a hook for changing Place Order Button behaviour upon click. I am trying to replace/change a product upon placing order as the product selection (all available products) are also in the Checkout Page.

So far I have been trying to manipulate woocommerce_checkout_order_review hook & failed.

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
    }
}

The hook above is not triggered upon Place Order. Maybe there's something wrong with my code or maybe what I suspected is wrong hook applied. Any ideas?

Upvotes: 0

Views: 2603

Answers (1)

Kaydarin
Kaydarin

Reputation: 391

Nevermind... found the answer...

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
     }
}

For more explanation, see Woocommerce Replace Product in Cart Upon Place Order in Checkout Page

Upvotes: 1

Related Questions