Created
October 7, 2016 06:24
-
-
Save ak5/a1e36036cf9fd51561f9ff6168dd8d01 to your computer and use it in GitHub Desktop.
WooCommerce Order Creation + Updating
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// this is the current id of an order in test env | |
$id = 44; | |
$order = wc_get_order($id); | |
/** OR **/ | |
// create a new order like so | |
$address = array( | |
'first_name' => 'Dick', | |
'last_name' => 'Fiddler', | |
'company' => 'WooThemes', | |
'email' => '[email protected]', | |
'phone' => '777-777-777-777', | |
'address_1' => '23 Wiggely Point', | |
'address_2' => '', | |
'city' => 'Mianus', | |
'state' => 'CT', | |
'postcode' => '12345', | |
'country' => 'US' | |
); | |
$order = wc_create_order(); | |
$order->set_address($address, 'billing'); | |
$order->set_address($address, 'shipping'); | |
// add fee to order | |
$name = 'my fee'; | |
$amount = 6.5; | |
$fee = (object)array( | |
'name' => $name, | |
'amount' => $amount, | |
'tax_data' => array() // gets rid of "Warning: array_map(): Argument #2 should be an array" | |
); | |
$order->add_fee($fee); | |
$order->calculate_totals(); // updates order total | |
// this is the current id of a product in test env | |
$product_id = 7; | |
$product = wc_get_product($product_id); | |
// add product to order at specified price | |
$quantity = 1; // default | |
$price = 19.99; | |
$args = array( | |
'totals' => array( | |
'subtotal' => $price, | |
'total' => $price | |
) | |
); | |
$order->add_product($product, $quantity, $args); | |
$order->calculate_totals(); // updates order total |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment