Skip to content

Instantly share code, notes, and snippets.

@seb86

seb86/index.php Secret

Created June 8, 2021 10:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save seb86/e1a60b2c518041f887e528400d19b22f to your computer and use it in GitHub Desktop.
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?default=true",
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();
$order_data = array(
'payment_method' => 'bacs',
'payment_method_title' => 'Direct Bank Transfer',
'set_paid' => true,
'billing' => array(
'first_name' => 'John',
'last_name' => 'Doe',
'address_1' => '969 Market',
'address_2' => '',
'city' => 'San Francisco',
'state' => 'CA',
'postcode' => '94103',
'country' => 'US',
'email' => 'john.doe@example.com',
'phone' => '(555) 555-5555'
),
'shipping' => array(
'first_name' => 'John',
'last_name' => 'Doe',
'address_1' => '969 Market',
'address_2' => '',
'city' => 'San Francisco',
'state' => 'CA',
'postcode' => '94103',
'country' => 'US'
),
'line_items' => array(), // Leave blank as we will be fetching items from cart.
'shipping_lines' => array(
[
'method_id' => 'flat_rate',
'method_title' => 'Flat Rate',
'total' => '10.00'
]
)
);
// Decodes customers cart so we can get the items they requested.
$cart = json_decode( $cart );
// Create line items for WC API order creation.
foreach( $cart as $item => $cart_item ) {
$order_data['line_items'][$item] = array(
'product_id' => $cart_item->product_id,
'quantity' => $cart_item->quantity
);
if ( ! empty( $cart_item->product_name ) ) {
$order_data['line_items'][$item]['name'] = $cart_item->product_name;
}
if ( ! empty( $cart_item->variation_id ) && $cart_item->variation_id > 0 ) {
$order_data['line_items'][$item]['variation_id'] = $cart_item->variation_id;
}
}
// Now we need to clean the line items so it is formatted correctly again.
$clean_order_data_items = array();
foreach( $order_data['line_items'] as $item ) {
$clean_order_data_items[] = $item;
}
if ( ! empty( $clean_order_data_items ) ) {
$order_data['line_items'] = $clean_order_data_items;
}
// Encode order data.
$args = json_encode( $order_data );
$domain = 'https://wp-demo.cocart.xyz';
// 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