17

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

Community
  • 1
  • 1
Ronn0
  • 2,249
  • 2
  • 21
  • 36

2 Answers2

27

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;
    }
}
Ronn0
  • 2,249
  • 2
  • 21
  • 36
0

To skip the checkout page you can filter the add to cart url.

function so_31787244_redirect_to_checkout( $url ) {

    // Remove default cart message
    WC()->clear_messages();

    // Redirect to checkout
    $url = WC()->cart->get_checkout_url();

    return $url;
}
add_filter( 'add_to_cart_redirect', 'so_31787244_redirect_to_checkout' );

You could also pursue a plugin such as One Page Checkout

helgatheviking
  • 25,596
  • 11
  • 95
  • 152
  • I want to skip the cart and checkout page. For example, on my productpage people can fill their contact info and push button "Pay now". After that an order needs to be created and redirected to payment page (for example paypal.com). – Ronn0 Aug 04 '15 at 06:03
  • I'm not sure why you are even using WooCommerce then. But, that said, this sounds pretty custom, maybe you can write a function that will create the order when PayPal validates the order. But it seems like a lot of work for little benefit. I think you'd be better off investing in an SSL cert and a premium PayPal plugin and accepting payment right on your site. – helgatheviking Aug 04 '15 at 13:54