Created
November 8, 2022 05:51
-
-
Save CameronGilroy/fcb5bdfc73d718c87ffd75fa76af57b8 to your computer and use it in GitHub Desktop.
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 | |
/* | |
Plugin Name: Woocommerce NewBook.cloud Integration | |
Plugin URI: https://camerongilroy.com | |
Description: Woocommerce NewBook.cloud Integration | |
Version: 1.0.0 | |
Author: Cameron Gilroy | |
Author URI: https://camerongilroy.com | |
License: GPL-2.0+ | |
License URI: http://www.gnu.org/licenses/gpl-2.0.txt | |
This plugin is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation, either version 2 of the License, or | |
any later version. | |
This plugin is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with This plugin. If not, see {URI to Plugin License}. | |
*/ | |
if (!defined('WPINC')) { | |
die; | |
} | |
// Add individual order actions | |
add_action('woocommerce_order_actions', 'custom_wc_order_action', 10, 1); | |
function custom_wc_order_action($actions) | |
{ | |
if (is_array($actions)) { | |
$actions['custom_action'] = __('Send Booking to NewBook'); | |
} | |
return $actions; | |
} | |
// Adding to admin order list bulk dropdown a custom action 'custom_downloads' | |
// add_filter( 'bulk_actions-edit-shop_order', 'downloads_bulk_actions_edit_product', 20, 1 ); | |
// function downloads_bulk_actions_edit_product( $actions ) { | |
// $actions['write_downloads'] = __( 'Bulk Send to NewBook', 'woocommerce' ); | |
// return $actions; | |
// } | |
/** | |
* Filter name is woocommerce_order_action_{$action_slug} | |
*/ | |
add_action('woocommerce_order_action_custom_action', 'Send_Booking_to_NewBook'); | |
add_filter( 'handle_bulk_actions-edit-shop_order', 'Send_Booking_to_NewBook', 10, 3 ); | |
function Send_Booking_to_NewBook($order_id) | |
{ | |
$order = new WC_Order($order_id); | |
// Get the data from woocommerce | |
$order_data = $order->get_data(); // The Order data | |
$order_billing_first_name = $order_data['billing']['first_name']; | |
$order_billing_last_name = $order_data['billing']['last_name']; | |
$order_billing_address_1 = $order_data['billing']['address_1']; | |
$order_billing_address_2 = $order_data['billing']['address_2']; | |
$order_billing_city = $order_data['billing']['city']; | |
$order_billing_state = $order_data['billing']['state']; | |
$order_billing_postcode = $order_data['billing']['postcode']; | |
$order_billing_country = $order_data['billing']['country']; | |
$order_billing_email = $order_data['billing']['email']; | |
$order_billing_phone = $order_data['billing']['phone']; | |
// $order->add_order_note(json_encode($order_data['meta_data']['5']->value)); | |
$car_rego = $order_data['meta_data']['5']->value; | |
$church = $order_data['meta_data']['6']->value; | |
$order_notes = $order_data['customer_note']; | |
$adults_q = 0; | |
$children_q = 0; | |
$big_camp_2020 = false; | |
// Iterating through each WC_Order_Item_Product objects | |
foreach ($order->get_items() as $item_key => $item) : | |
// Item ID is directly accessible from the $item_key in the foreach loop or | |
$item_id = $item->get_id(); | |
$product_id = $item->get_product_id(); // the Product id | |
$variation_id = $item->get_variation_id(); // the Variation id | |
$quantity = $item->get_quantity(); | |
if ($product_id == 22164) { | |
$big_camp_2020 = true; | |
} | |
if ($variation_id == 22174) { | |
$category_id = '1'; //Caravane | |
} | |
if ($variation_id == 22175) { | |
$category_id = '40'; //UnPowered Site | |
} | |
if ($variation_id == 22176) { | |
$category_id = '41'; //Conference Tent no Power | |
} | |
if ($variation_id == 22177) { | |
$category_id = '2'; //Conference Tent With Power | |
} | |
if ($product_id == 22198) { | |
$adults_q = $quantity; | |
} | |
if ($product_id == 22178) { | |
$children_q = $quantity; | |
} | |
endforeach; | |
// Passing the variables to newbook | |
$region = 'au'; //hard coded | |
$api_url = 'https://api.newbook.cloud/rest'; | |
$authorization = 'Basic api'; | |
$cookie = '__cfduid=api'; | |
$api_key = 'instances_api'; //hard coded | |
$guest_firstname = $order_billing_first_name; | |
$guest_lastname = $order_billing_last_name; | |
$address_street = $order_billing_address_1 . ' ' . $order_billing_address_2; | |
$address_state_name = $order_billing_state; | |
$address_city = $order_billing_city; | |
$address_postcode = $order_billing_postcode; | |
$address_country_name = $order_billing_country; | |
$guest_email = $order_billing_email; | |
$guest_phone = $order_billing_phone; | |
$adults = $adults_q; | |
$children = $children_q; | |
$period_from = "2019-11-17"; //hard coded | |
$period_to = "2019-11-25"; //hard coded | |
// $category_id = "1"; //hard coded - Defined up above | |
$status = 'Unconfirmed'; //hard coded | |
$flagged = 1; //hard coded | |
$notes = 'Order Notes: ' . $order_notes . ', Site Type ID: ' . $category_id . ', Car Rego: ' . $car_rego . ', Church: ' . $church; | |
if ($big_camp_2020 == true) { | |
$response = wp_remote_post($api_url, array( | |
'headers' => array( | |
'Authorization' => 'Basic api=', | |
'Content-Type' => 'text/plain; charset=utf-8', | |
'Cookie' => '__cfduid=api', | |
), | |
'body' => '{ | |
"region": "' . $region . '", | |
"api_key": "' . $api_key . '", | |
"request_action": "bookings_create", | |
"guest_firstname": "' . $guest_firstname . '", | |
"guest_lastname": "' . $guest_lastname . '", | |
"address_street": "' . $address_street . '", | |
"address_city": "' . $address_city . '", | |
"address_state_name": "' . $address_state_name . '", | |
"address_postcode": "' . $address_postcode . '", | |
"address_country_name": "' . $address_country_name . '", | |
"guest_email": "' . $guest_email . '", | |
"guest_phone": "' . $guest_phone . '", | |
"guest_car_rego": "' . $car_rego . '", | |
"adults": "' . $adults . '", | |
"children": "' . $children . '", | |
"period_from": "' . $period_from . '", | |
"period_to": "' . $period_to . '", | |
"category_id": "1", | |
"status": "' . $status . '", | |
"flagged": "' . $flagged . '", | |
"notes": "' . $notes . '" | |
}', | |
)); | |
if (!is_wp_error($response)) { | |
// The request went through successfully, check the response code against | |
// what we're expecting | |
if (200 == wp_remote_retrieve_response_code($response)) { | |
// Do something with the response | |
$body = wp_remote_retrieve_body($response); | |
// $headers = wp_remote_retrieve_headers( $response ); | |
$order->add_order_note('<a href=https://appau.newbook.cloud/bookings_view/' . json_decode($body, true)['data']['booking_id'] . '>View booking on NewBook</a> | <a href=https://appau.newbook.cloud/guests_duplicates_merge/' . json_decode($body, true)['data']['guest_id'] . '>View Guest on NewBook</a>'); | |
// $order->add_order_note($notes); | |
update_post_meta( $order_data['id'], 'new_book_status', '1' ); | |
// update_post_meta( $order_data['id'], 'new_book_site_id', json_decode($body, true)['data']['booking_id'] ); | |
// $order->add_order_note(json_encode($order_data)); | |
// $order->add_order_note(json_encode($response)); | |
// WP_Logging::add('Added booking '.json_decode($body, true)['data']['booking_id'], json_encode($response), $order_id, 'event'); | |
} elseif (412 == wp_remote_retrieve_response_code($response)) { | |
$order->add_order_note("Unable to add booking to NewBook.</br>412" . json_encode($response)); | |
$order->add_order_note(json_encode($order_data)); | |
$order->add_order_note($notes); | |
// WP_Logging::add('NewBook 412 error', json_encode($response), $order_id, 'error'); | |
} else { | |
// The response code was not what we were expecting, record the message | |
$order->add_order_note("Unable to add booking to NewBook.</br>" . json_encode($response)); | |
$error_message = wp_remote_retrieve_response_message($response); | |
$order->add_order_note($notes); | |
} | |
} else { | |
// There was an error making the request | |
$order->add_order_note("There was an error making the request.</br>" . json_encode($response)); | |
$order->add_order_note($notes); | |
$error_message = $response->get_error_message(); | |
} | |
} else { | |
$order->add_order_note("This order doesn't have a product that could be sent to NewBook" . $product_id); | |
$order->add_order_note($notes); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment