Skip to content

Instantly share code, notes, and snippets.

@seb86

seb86/index.php Secret

Last active March 25, 2025 20:46
Show Gist options
  • Select an option

  • Save seb86/e1a60b2c518041f887e528400d19b22f to your computer and use it in GitHub Desktop.

Select an option

Save seb86/e1a60b2c518041f887e528400d19b22f to your computer and use it in GitHub Desktop.
Example of creating an order for a customer using CoCart API v2 and WC API - cURL
<?php
// Get cart.
// DEV NOTE: Return the cart by default so it can return in the format WooCommerce generates the cart data in.
$curl = curl_init();
curl_setopt_array( $curl, array(
CURLOPT_URL => "https://example.com/wp-json/cocart/v2/cart",
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
'User-Agent: CoCart API/v2',
)
) );
// Returns cart.
$cart = curl_exec($curl);
curl_close($curl);
// Create order.
include 'key.php';
$curl = curl_init();
// Decodes customers cart so we can get the items they requested.
$cart = json_decode($cart);
// Prepare order data using cart information
$order_data = array(
'payment_method' => 'bacs',
'payment_method_title' => 'Direct Bank Transfer',
'set_paid' => true,
'billing' => array(
'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' => array(
'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
),
'line_items' => array(),
'shipping_lines' => array()
);
// Add shipping method if available
if (isset($cart->shipping->packages->default->chosen_method)) {
$chosen_method = $cart->shipping->packages->default->rates->{$cart->shipping->packages->default->chosen_method};
$order_data['shipping_lines'][] = array(
'method_id' => $chosen_method->method_id,
'method_title' => $chosen_method->label,
'total' => $chosen_method->cost
);
}
// Create line items for WC API order creation
foreach ($cart->items as $item) {
$line_item = array(
'product_id' => $item->id,
'quantity' => $item->quantity->value,
'name' => $item->name
);
if (!empty($item->meta->product_type) && $item->meta->product_type === 'variation') {
$line_item['variation_id'] = $item->id;
}
$order_data['line_items'][] = $line_item;
}
// Encode order data.
$args = json_encode( $order_data );
$domain = 'https://example-store.com';
// Create order.
curl_setopt_array( $curl, array(
CURLOPT_URL => $domain . "/wp-json/wc/v3/orders",
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $args,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
'Authorization: Basic ' . base64_encode($username . ':' . $password),
'Content-Type: application/json;charset=utf-8'
)
) );
// Order created.
$response = curl_exec($curl);
curl_close($curl);
var_dump( print_r( $response ) );
<?php
// Place your consumer key and secret in this file.
$username = 'ck_d15a6adb407ca*************';
$password = 'cs_b968b4b5d4b4e*************';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment