Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Created June 7, 2019 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damiencarbery/56e96ad4ddfc7a794285b4a3f3697b53 to your computer and use it in GitHub Desktop.
Save damiencarbery/56e96ad4ddfc7a794285b4a3f3697b53 to your computer and use it in GitHub Desktop.
Trying WooCommerce REST API and then reverting to regular API
<?php
// Convert ThriveCart order to WooCommerce order.
// ThriveCart order data is passed via ThriveCart Webhook into $_POST.
// By Damien Carbery, damien@damiencarbery.com, https://www.damiencarbery.com
//
// $Id: tc-to-wc-order.php 4726 2019-06-07 11:10:43Z damien $
// Avoid warnings about element not being set.
function get_post_parameter( $key, $default ) {
if ( array_key_exists( $key, $_POST ) ) {
return $_POST[ $key ];
}
else {
return $default;
}
}
function create_vip_order() {
global $address, $tc_to_wc_products, $tc_product_id;
$payment_method = 'bacs';
// User check/creation code from: https://gist.github.com/stormwild/7f914183fc18458f6ab78e055538dcf0 (and https://stackoverflow.com/a/35304031/8605943)
$default_password = wp_generate_password();
$customer_id = null;
if ( $user = get_user_by( 'login', $address[ 'email' ] ) ) {
$customer_id = $user->ID;
}
else {
$customer_id = wp_insert_user( array(
'user_login' => $address[ 'email' ],
'user_pass' => $default_password,
'user_email' => $address[ 'email' ],
'role' => 'customer',
) );
}
// Now we create the order
$order = wc_create_order( array('customer_id' => $customer_id) );
// The add_product() function below is located in /plugins/woocommerce/includes/abstracts/abstract_wc_order.php
$order->add_product( wc_get_product( $tc_to_wc_products[ $tc_product_id ] ), 1); // This is an existing SIMPLE product
$order->set_address( $address, 'billing' );
// This block copied from: https://stackoverflow.com/a/55437588/8605943
$order->set_created_via( 'programatically' );
$order->set_customer_id( $customer_id );
$order->set_currency( get_woocommerce_currency() );
$order->set_prices_include_tax( 'yes' === get_option( 'woocommerce_prices_include_tax' ) );
$gateways = WC()->payment_gateways->get_available_payment_gateways();
$order->set_payment_method( isset( $gateways[ $payment_method ] ) ? $gateways[ $payment_method ] : $payment_method );
$order->calculate_totals();
$order_id = $order->save();
$status_change = $order->update_status('wc-completed', 'Imported order', TRUE);
echo "<p>Order: <strong>$order_id</strong></p>";
echo "<p>Status: <strong>", $order->get_status(), "</strong></p>";
}
header('HTTP/1.1 200 OK');
header('Content-Type: text/plain');
// Return 2xx status for HEAD request to satisfy ThriveCart webhook check.
if ( 'HEAD' == $_SERVER['REQUEST_METHOD'] ) {
// Don't do anything.
}
else {
define('WP_USE_THEMES', false);
/** Loads the WordPress Environment */
require( dirname( __FILE__ ) . '/wp-blog-header.php' );
// Basic prerequisites.
if ( ( 'order.success' != get_post_parameter( 'event', '' ) ) ||
( '-redacted-' != get_post_parameter( 'thrivecart_account', '' ) ) || // In
( '-redacted-' != get_post_parameter( 'thrivecart_secret', '' ) ) ) {
//header( 'HTTP/1.1 400 Bad Request' );
echo( '<p>Invalid "event", or "thrivecart_account" or "thrivecart_secret".</p>' );
}
else {
// Extract the required data from $_POST so that an order can be created.
$tc_product_id = get_post_parameter( 'base_product', 0 );
$customer = get_post_parameter( 'customer', array() );
//var_export( $customer );
$address = array(
'first_name' => $customer[ 'first_name' ],
'last_name' => $customer[ 'last_name' ],
'email' => $customer[ 'email' ],
'address_1' => $customer[ 'address' ][ 'line1' ],
'city' => $customer[ 'address' ][ 'city' ],
'state' => 'NA',
'postcode' => $customer[ 'address' ][ 'zip' ],
'country' => $customer[ 'address' ][ 'country' ],
);
// Map ThriveCart products to WooCommerce Sensi products.
$tc_to_wc_products = array(
5 => 5962, // ULTIMATE Online Screenwriting Course
16 => 75576, // How to Write a Great Novel – Online Course
17 => 194105, // How to Make Money Blogging – Online Course
15 => 5960, // Write The Great American TV Script – Online
18 => 208689, // Write the Hit Romance & Erotic Novel – Online
);
// Ensure that the product has an equivalent in WooCommerce.
if ( array_key_exists( $tc_product_id, $tc_to_wc_products ) ) {
create_vip_order();
}
}
}
<?php
$request_url = 'http://localhost/storefront/wp-json/wc/v3/orders/';
$consumer_key = 'ck_-redacted-';
$consumer_secret = 'cs_-redacted-';
$credentials = array();
$credentials = array( 'username: admin', 'password: -redacted-', 'consumer_key' );
$curl_handle = curl_init( );
curl_setopt( $curl_handle, CURLOPT_URL, $request_url );
curl_setopt( $curl_handle, CURLOPT_CONNECTTIMEOUT, 0 );
curl_setopt( $curl_handle, CURLOPT_TIMEOUT, 15 );
curl_setopt( $curl_handle, CURLOPT_HTTPHEADER, $credentials );
curl_setopt( $curl_handle, CURLOPT_USERPWD, $consumer_key . ':' . $consumer_secret);
curl_setopt( $curl_handle, CURLOPT_RETURNTRANSFER, TRUE );
$JsonResponse = curl_exec( $curl_handle );
$http_code = curl_getinfo( $curl_handle );
if ( 200 == $http_code[ 'http_code' ] ) {
echo $JsonResponse, "\n";
}
else {
echo 'ERROR: <pre>', var_export( $JsonResponse, true ), "</pre>\n";
}
/*
Returns:
ERROR: <pre>'{"code":"woocommerce_rest_cannot_view","message":"Sorry, you cannot list resources.","data":{"status":401}}'</pre>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment