WooCommerce create an order programmatically and redirect to payment. Use following code in your theme’s functions.php file OR in site specific plugin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
if (isset($_POST['isOrder']) && $_POST['isOrder'] == 1) { $address = array( 'first_name' => $_POST['notes']['domain'], 'last_name' => '', 'company' => $_POST['customer']['company'], 'email' => $_POST['customer']['email'], 'phone' => $_POST['customer']['phone'], 'address_1' => $_POST['customer']['address'], 'address_2' => '', 'city' => $_POST['customer']['city'], 'state' => '', 'postcode' => $_POST['customer']['postalcode'], 'country' => 'NL' ); $order = wc_create_order(); foreach ($_POST['product_order'] as $productId => $productOrdered) : $order->add_product( get_product( $productId ), 1 ); endforeach; $order->set_address( $address, 'billing' ); $order->set_address( $address, 'shipping' ); $order->calculate_totals(); update_post_meta( $order->id, '_payment_method', 'ideal' ); update_post_meta( $order->id, '_payment_method_title', 'iDeal' ); // Store Order ID in session so it can be re-used after payment failure WC()->session->order_awaiting_payment = $order->id; // Process Payment $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); $result = $available_gateways[ 'ideal' ]->process_payment( $order->id ); // Redirect to success/confirmation/payment page if ( $result['result'] == 'success' ) { $result = apply_filters( 'woocommerce_payment_successful_result', $result, $order->id ); wp_redirect( $result['redirect'] ); exit; } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.