Last active
June 3, 2019 09:55
-
-
Save damiencarbery/f6ffe754ba8aae970003b072c6a2c370 to your computer and use it in GitHub Desktop.
Duplicate a ThriveCart order in WooCommerce: Convert ThriveCart order data into a WooCommerce order. https://www.damiencarbery.com/2019/06/duplicate-a-thrivecart-order-in-woocommerce/
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 | |
/* | |
// Restrict who can access this script. | |
$permitted_ips = array('12.34.56.78', '87.65.43.21'); | |
if (in_array($_SERVER['REMOTE_ADDR'], $permitted_ips) == false) { | |
header('HTTP/1.0 403 Forbidden'); | |
die(); | |
} | |
*/ | |
define('WP_USE_THEMES', false); | |
/** Loads the WordPress Environment */ | |
require( dirname( __FILE__ ) . '/wp-blog-header.php' ); | |
$product_id = 28; | |
$address = array( | |
'first_name' => 'Damien', | |
'last_name' => 'Carbery', | |
'email' => 'me@example.com', | |
'address_1' => '1 Main Street', | |
'city' => 'Blanchardstown', | |
'state' => 'Dublin', | |
'postcode' => 'D15', | |
'country' => 'IE', | |
); | |
function create_test_order() { | |
global $address, $tc_to_wc_products, $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( 'email', $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', | |
) ); | |
if ( is_wp_error( $customer_id ) ) { | |
echo $customer_id->get_error_message(); | |
die(); | |
} | |
} | |
// Now we create the order | |
$order = wc_create_order( array('customer_id' => $customer_id) ); | |
$order->add_product( wc_get_product( $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>"; | |
} | |
create_test_order(); |
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 | |
/* | |
// Restrict who can access this script. | |
$permitted_ips = array('12.34.56.78', '87.65.43.21'); | |
if (in_array($_SERVER['REMOTE_ADDR'], $permitted_ips) == false) { | |
header('HTTP/1.0 403 Forbidden'); | |
die(); | |
} | |
*/ | |
define('WP_USE_THEMES', false); | |
/** Loads the WordPress Environment */ | |
require( dirname( __FILE__ ) . '/wp-blog-header.php' ); | |
// 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; | |
} | |
} | |
// Basic prerequisites. | |
if ( ( 'order.success' != get_post_parameter( 'event', '' ) ) || | |
( 'generic' != get_post_parameter( 'thrivecart_account', '' ) ) || | |
( 'JLZE3Y54FEQ1' != get_post_parameter( 'thrivecart_secret', '' ) ) ) { | |
header( 'HTTP/1.1 400 Bad Request' ); | |
} | |
header("Content-Type: text/plain"); | |
// 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() ); | |
$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( 2 => 28 ); | |
create_test_order(); // The same function as in mk-local-order.php |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment