Skip to content

Instantly share code, notes, and snippets.

@robindevitt
Last active June 24, 2020 13:23
Show Gist options
  • Save robindevitt/8a092598137aac6c138c4d7a6d553e08 to your computer and use it in GitHub Desktop.
Save robindevitt/8a092598137aac6c138c4d7a6d553e08 to your computer and use it in GitHub Desktop.
<?php
/*
* Create order dynamically
*/
add_action( 'woocommerce_before_checkout_form', 'create_order' );
function create_order() {
global $woocommerce;
$address = array(
'first_name' => 'Firstname',
'last_name' => 'Lastname',
'company' => 'Company',
'email' => 'emailaddress@nope.com',
'phone' => '012-345-6789',
'address_1' => 'Address line 1',
'address_2' => 'Address line 2',
'city' => 'City',
'state' => 'State',
'postcode' => '00000',
'country' => 'Country'
);
// Now we create the order
$order = wc_create_order();
// Use the product IDs to add
$order->add_product( get_product( 123 ), 1);
// Set addresses
$order->set_address( $address, 'billing' );
$order->set_address( $address, 'shipping' );
// Set payment gateway
$payment_gateways = WC()->payment_gateways->payment_gateways();
$order->set_payment_method( $payment_gateways['bacs'] );
// Calculate totals
$order->calculate_totals();
$order->update_status( 'Processing', 'Order created via Pick-A-Display - ', TRUE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment