Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce create an order programmatically and redirect to payment

For WooCommerce i'm lookin' for a solution to create an order programmaticly (my site just has 1 homepage with some fields) to order.

After products are added with a checkbox i'd like to create an order and redirect to the payment method.

Creating an order is almost done with this answer, but how do i start a payment? Wordpress (Woocommerce extension) - Create new order programatically

like image 344
Ronn0 Avatar asked Aug 03 '15 12:08

Ronn0


People also ask

How do I create a custom order in WooCommerce programmatically?

function create_vip_order() { global $woocommerce; $address = array( 'first_name' => '111Joe', 'last_name' => 'Conlin', 'company' => 'Speed Society', 'email' => '[email protected]', 'phone' => '760-555-1212', 'address_1' => '123 Main st.

How do I create a custom payment method in WooCommerce?

Step 1: Go to WooCommerce >> Payments and enable Check payments. Step 2: Click on Set up and configure the payment method. You will get the check payment settings to be set. Here, enable the cheque payments and then enter the instructions and description for your customers.


1 Answers

This did it for me:

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;
    }
}
like image 92
Ronn0 Avatar answered Sep 28 '22 12:09

Ronn0