In this tutorial, we’ll walk through the process of converting a shopping cart into an order using the WooCommerce REST API. This is particularly useful when you need to programmatically create orders from cart sessions.
CoCart will provide it’s own checkout API in the future that will stream line the process. In the mean time, this is the best method.
You will need WooCommerce API credentials (consumer key and secret) for this.
Step 1: Retrieve the Cart Data
First, we’ll fetch the current cart data using the CoCart API:
In the following request examples, you would replace <cart_key>
, <username>
and <password>
before sending the request.
curl -X GET \
https://your-store.com/wp-json/cocart/v2/cart?cart_key= < cart_ke y > \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-H 'User-Agent: CoCart API/v2' // Not a requirement.
Registered Customer Authenticated
curl -X GET \
https://your-store.com/wp-json/cocart/v2/cart \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-H 'Authorization: Basic base64_encode(<username>:<password>)'
-H 'User-Agent: CoCart API/v2' // Not a requirement.
Step 2: Prepare Order Data
Next, we’ll prepare the order data using the cart information. We’ll need to:
Decode the cart JSON
Set up billing and shipping information
Configure payment details
# First decode and store the cart response from Step 1
CART_DATA = $( cat cart-response.json )
# Prepare the order data structure
ORDER_DATA = $( jq -n \
--arg payment_method "bacs" \
--arg payment_title "Direct Bank Transfer" \
--argjson paid true \
--argjson cart " $CART_DATA " \
'{
payment_method: $payment_method,
payment_method_title: $payment_title,
set_paid: $paid,
billing: {
first_name: $cart.customer.billing_address.billing_first_name,
last_name: $cart.customer.billing_address.billing_last_name,
address_1: $cart.customer.billing_address.billing_address_1,
address_2: $cart.customer.billing_address.billing_address_2,
city: $cart.customer.billing_address.billing_city,
state: $cart.customer.billing_address.billing_state,
postcode: $cart.customer.billing_address.billing_postcode,
country: $cart.customer.billing_address.billing_country,
email: $cart.customer.billing_address.billing_email,
phone: $cart.customer.billing_address.billing_phone
},
shipping: {
first_name: $cart.customer.shipping_address.shipping_first_name,
last_name: $cart.customer.shipping_address.shipping_last_name,
address_1: $cart.customer.shipping_address.shipping_address_1,
address_2: $cart.customer.shipping_address.shipping_address_2,
city: $cart.customer.shipping_address.shipping_city,
state: $cart.customer.shipping_address.shipping_state,
postcode: $cart.customer.shipping_address.shipping_postcode,
country: $cart.customer.shipping_address.shipping_country
}
}' )
Step 3: Add Shipping Method
If shipping is selected in the cart, we’ll add it to the order:
# Add shipping lines if shipping method exists
if [[ $( echo " $CART_DATA " | jq -r '.shipping.packages.default.chosen_method' ) != "null" ]]; then
CHOSEN_METHOD = $( echo " $CART_DATA " | jq -r '.shipping.packages.default.chosen_method' )
ORDER_DATA = $( echo " $ORDER_DATA " | jq \
--arg method_id "$( echo " $CART_DATA " | jq -r ".shipping.packages.default.rates. $CHOSEN_METHOD .method_id")" \
--arg title "$( echo " $CART_DATA " | jq -r ".shipping.packages.default.rates. $CHOSEN_METHOD .label")" \
--arg cost "$( echo " $CART_DATA " | jq -r ".shipping.packages.default.rates. $CHOSEN_METHOD .cost")" \
'. + {shipping_lines: [{method_id: $method_id, method_title: $title, total: $cost}]}' )
fi
Step 4: Process Line Items
We’ll convert cart items into order line items:
foreach ( $cart -> items as $item ) {
$line_item = array (
'product_id' => $item -> id ,
'quantity' => $item -> quantity -> value ,
'name' => $item -> name ,
'total' => $item -> totals -> total
);
if ( ! empty ( $item -> meta -> product_type ) && $item -> meta -> product_type === 'variation' ) {
$line_item [ 'variation_id' ] = $item -> id ;
$line_item [ 'product_id' ] = $item -> parent_id ;
}
$order_data [ 'line_items' ][] = $line_item ;
}
Step 5: Create the Order
Finally, we’ll send the prepared data to the WooCommerce API to create the order:
Conclusion
You’ve now successfully converted a CoCart cart into a WooCommerce order! The response will contain the newly created order details. Remember to:
Replace example-store.com
with your actual domain
Insert your WooCommerce API credentials
Handle any potential errors in the response
Consider adding error checking and validation
For more information about the available order parameters, consult the WooCommerce REST API documentation .