| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Mstore API |
| 4 |
* Plugin URI: https://github.com/inspireui/mstore-api |
| 5 |
* Description: The MStore API Plugin which is used for the Mstore and FluxStore App |
| 6 |
* Version: 3.0.4 |
| 7 |
* Author: InspireUI |
| 8 |
* Author URI: http://inspireui.com |
| 9 |
* |
| 10 |
* Text Domain: MStore-Api |
| 11 |
*/ |
| 12 |
|
| 13 |
defined('ABSPATH') or wp_die( 'No script kiddies please!' ); |
| 14 |
|
| 15 |
|
| 16 |
// use MStoreCheckout\Templates\MDetect; |
| 17 |
|
| 18 |
include plugin_dir_path(__FILE__)."templates/class-mobile-detect.php"; |
| 19 |
include plugin_dir_path(__FILE__)."templates/class-rename-generate.php"; |
| 20 |
include_once plugin_dir_path(__FILE__)."controllers/FlutterUser.php"; |
| 21 |
include_once plugin_dir_path(__FILE__)."controllers/FlutterHome.php"; |
| 22 |
include_once plugin_dir_path(__FILE__)."controllers/FlutterVendor.php"; |
| 23 |
include_once plugin_dir_path(__FILE__)."controllers/FlutterBooking.php"; |
| 24 |
include_once plugin_dir_path(__FILE__)."controllers/FlutterWoo.php"; |
| 25 |
include_once plugin_dir_path(__FILE__)."functions/index.php"; |
| 26 |
|
| 27 |
class MstoreCheckOut |
| 28 |
{ |
| 29 |
public $version = '3.0.4'; |
| 30 |
|
| 31 |
public function __construct() |
| 32 |
{ |
| 33 |
define('MSTORE_CHECKOUT_VERSION', $this->version); |
| 34 |
define('MSTORE_PLUGIN_FILE', __FILE__); |
| 35 |
include_once (ABSPATH . 'wp-admin/includes/plugin.php'); |
| 36 |
if (is_plugin_active('woocommerce/woocommerce.php') == false) { |
| 37 |
return 0; |
| 38 |
} |
| 39 |
|
| 40 |
$order = filter_has_var(INPUT_GET, 'code') && strlen(filter_input(INPUT_GET, 'code')) > 0 ? true : false; |
| 41 |
if ($order) { |
| 42 |
add_filter('woocommerce_is_checkout', '__return_true'); |
| 43 |
} |
| 44 |
|
| 45 |
include_once plugin_dir_path(__FILE__)."controllers/MStoreHome.php"; |
| 46 |
|
| 47 |
add_action('wp_print_scripts', array($this, 'handle_received_order_page')); |
| 48 |
|
| 49 |
//add meta box shipping location in order detail |
| 50 |
add_action( 'add_meta_boxes', 'mv_add_meta_boxes' ); |
| 51 |
if ( ! function_exists( 'mv_add_meta_boxes' ) ) |
| 52 |
{ |
| 53 |
function mv_add_meta_boxes() |
| 54 |
{ |
| 55 |
add_meta_box( 'mv_other_fields', __('Shipping Location','woocommerce'), 'mv_add_other_fields_for_packaging', 'shop_order', 'side', 'core' ); |
| 56 |
} |
| 57 |
} |
| 58 |
// Adding Meta field in the meta container admin shop_order pages |
| 59 |
if ( ! function_exists( 'mv_add_other_fields_for_packaging' ) ) |
| 60 |
{ |
| 61 |
function mv_add_other_fields_for_packaging() |
| 62 |
{ |
| 63 |
global $post; |
| 64 |
$note = $post->post_excerpt; |
| 65 |
$items = explode("\n", $note); |
| 66 |
if (strpos($items[0],"URL:") !== false) { |
| 67 |
$url = str_replace("URL:","",$items[0]); |
| 68 |
echo '<iframe width="600" height="500" src="'.$url.'"></iframe>'; |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
register_activation_hook( __FILE__, array($this,'create_custom_mstore_table') ); |
| 74 |
|
| 75 |
/** |
| 76 |
* Prepare data before checkout by webview |
| 77 |
*/ |
| 78 |
add_action( 'template_redirect', 'prepare_checkout' ); |
| 79 |
|
| 80 |
/** |
| 81 |
* Register js file to theme |
| 82 |
*/ |
| 83 |
function mstore_frontend_script() { |
| 84 |
wp_enqueue_script( 'my_script', plugins_url('assets/js/mstore-inspireui.js', MSTORE_PLUGIN_FILE), array( 'jquery' ), '1.0.0', true ); |
| 85 |
wp_localize_script( 'my_script', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); |
| 86 |
} |
| 87 |
add_action( 'wp_enqueue_scripts', 'mstore_frontend_script' ); |
| 88 |
// Setup Ajax action hook |
| 89 |
add_action( 'wp_ajax_mstore_delete_json_file', array( $this, 'mstore_delete_json_file' ) ); |
| 90 |
add_action( 'wp_ajax_mstore_update_limit_product', array( $this, 'mstore_update_limit_product' ) ); |
| 91 |
add_action( 'wp_ajax_mstore_update_firebase_server_key', array( $this, 'mstore_update_firebase_server_key' ) ); |
| 92 |
|
| 93 |
//listen changed order status to notify |
| 94 |
add_action( 'woocommerce_order_status_changed', array( $this, 'track_order_status_changed'), 9, 4 ); |
| 95 |
add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'track_new_order')); |
| 96 |
add_action( 'woocommerce_rest_insert_shop_order_object', array( $this, 'track_api_new_order'), 10, 4); |
| 97 |
|
| 98 |
$path = get_template_directory()."/templates"; |
| 99 |
if (!file_exists($path)) { |
| 100 |
mkdir($path, 0777, true); |
| 101 |
} |
| 102 |
if (file_exists($path)) { |
| 103 |
$templatePath = plugin_dir_path(__FILE__)."templates/mstore-api-template.php"; |
| 104 |
if (!copy($templatePath,$path."/mstore-api-template.php")) { |
| 105 |
return 0; |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
function mstore_delete_json_file(){ |
| 111 |
$id = $_REQUEST['id']; |
| 112 |
$uploads_dir = wp_upload_dir(); |
| 113 |
$filePath = trailingslashit( $uploads_dir["basedir"] )."/2000/01/".$id; |
| 114 |
unlink($filePath); |
| 115 |
echo "success"; |
| 116 |
die(); |
| 117 |
} |
| 118 |
|
| 119 |
function mstore_update_limit_product(){ |
| 120 |
$limit = $_REQUEST['limit']; |
| 121 |
if (is_numeric($limit)) { |
| 122 |
update_option("mstore_limit_product", intval($limit)); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
function mstore_update_firebase_server_key(){ |
| 127 |
$serverKey = $_REQUEST['serverKey']; |
| 128 |
update_option("mstore_firebase_server_key", $serverKey); |
| 129 |
} |
| 130 |
|
| 131 |
function track_order_status_changed($id, $previous_status, $next_status){ |
| 132 |
trackOrderStatusChanged( $id, $previous_status, $next_status ); |
| 133 |
} |
| 134 |
|
| 135 |
function track_new_order($order_id){ |
| 136 |
trackNewOrder($order_id); |
| 137 |
} |
| 138 |
|
| 139 |
function track_api_new_order($object){ |
| 140 |
trackNewOrder($object->id); |
| 141 |
} |
| 142 |
|
| 143 |
public function handle_received_order_page() |
| 144 |
{ |
| 145 |
// default return true for getting checkout library working |
| 146 |
if (is_order_received_page()) { |
| 147 |
$detect = new MDetect; |
| 148 |
if ($detect->isMobile()) { |
| 149 |
wp_register_style('mstore-order-custom-style', plugins_url('assets/css/mstore-order-style.css', MSTORE_PLUGIN_FILE)); |
| 150 |
wp_enqueue_style('mstore-order-custom-style'); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
function create_custom_mstore_table(){ |
| 157 |
global $wpdb; |
| 158 |
// include upgrade-functions for maybe_create_table; |
| 159 |
if (!function_exists('maybe_create_table')) { |
| 160 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 161 |
} |
| 162 |
$charset_collate = $wpdb->get_charset_collate(); |
| 163 |
$table_name = $wpdb->prefix . 'mstore_checkout'; |
| 164 |
$sql = "CREATE TABLE $table_name ( |
| 165 |
id mediumint(9) NOT NULL AUTO_INCREMENT, |
| 166 |
`code` tinytext NOT NULL, |
| 167 |
`order` text NOT NULL, |
| 168 |
PRIMARY KEY (id) |
| 169 |
) $charset_collate;"; |
| 170 |
$success = maybe_create_table($table_name, $sql); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
$mstoreCheckOut = new MstoreCheckOut(); |
| 175 |
|
| 176 |
// use JO\Module\Templater\Templater; |
| 177 |
include plugin_dir_path(__FILE__)."wp-templater/src/Templater.php"; |
| 178 |
|
| 179 |
add_action('plugins_loaded', 'load_mstore_templater'); |
| 180 |
function load_mstore_templater() |
| 181 |
{ |
| 182 |
|
| 183 |
// add our new custom templates |
| 184 |
$my_templater = new Templater( |
| 185 |
array( |
| 186 |
// YOUR_PLUGIN_DIR or plugin_dir_path(__FILE__) |
| 187 |
'plugin_directory' => plugin_dir_path(__FILE__), |
| 188 |
// should end with _ > prefix_ |
| 189 |
'plugin_prefix' => 'plugin_prefix_', |
| 190 |
// templates directory inside your plugin |
| 191 |
'plugin_template_directory' => 'templates', |
| 192 |
) |
| 193 |
); |
| 194 |
$my_templater->add( |
| 195 |
array( |
| 196 |
'page' => array( |
| 197 |
'mstore-api-template.php' => 'Page Custom Template', |
| 198 |
), |
| 199 |
) |
| 200 |
)->register(); |
| 201 |
} |
| 202 |
|
| 203 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 204 |
// Define for the API User wrapper which is based on json api user plugin |
| 205 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 206 |
|
| 207 |
// if (!is_plugin_active('json-api/json-api.php') && !is_plugin_active('json-api-master/json-api.php')) { |
| 208 |
// // add_action('admin_notices', 'pim_draw_notice_json_api'); |
| 209 |
// return; |
| 210 |
// } |
| 211 |
|
| 212 |
add_filter('json_api_controllers', 'registerJsonApiController'); |
| 213 |
add_filter('json_api_mstore_user_controller_path', 'setMstoreUserControllerPath'); |
| 214 |
add_action('init', 'json_apiCheckAuthCookie', 100); |
| 215 |
|
| 216 |
//custom rest api |
| 217 |
function mstore_users_routes() { |
| 218 |
$controller = new FlutterUserController(); |
| 219 |
$controller->register_routes(); |
| 220 |
} |
| 221 |
add_action( 'rest_api_init', 'mstore_users_routes' ); |
| 222 |
add_action( 'rest_api_init', 'mstore_check_payment_routes' ); |
| 223 |
function mstore_check_payment_routes() { |
| 224 |
register_rest_route( 'order', '/verify', array( |
| 225 |
'methods' => 'GET', |
| 226 |
'callback' => 'mstore_check_payment', |
| 227 |
'permission_callback' => function (){ |
| 228 |
return true; |
| 229 |
}, |
| 230 |
) |
| 231 |
); |
| 232 |
} |
| 233 |
function mstore_check_payment() { |
| 234 |
return true; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
// Add menu Setting |
| 240 |
add_action('admin_menu', 'mstore_plugin_setup_menu'); |
| 241 |
|
| 242 |
function mstore_plugin_setup_menu(){ |
| 243 |
add_menu_page( 'MStore Api', 'MStore Api', 'manage_options', 'mstore-plugin', 'mstore_init' ); |
| 244 |
} |
| 245 |
|
| 246 |
function mstore_init(){ |
| 247 |
load_template( dirname( __FILE__ ) . '/templates/mstore-api-admin-page.php' ); |
| 248 |
} |
| 249 |
|
| 250 |
function registerJsonApiController($aControllers) |
| 251 |
{ |
| 252 |
$aControllers[] = 'Mstore_User'; |
| 253 |
return $aControllers; |
| 254 |
} |
| 255 |
|
| 256 |
function setMstoreUserControllerPath() |
| 257 |
{ |
| 258 |
return plugin_dir_path(__FILE__) . '/controllers/MStoreUser.php'; |
| 259 |
} |
| 260 |
|
| 261 |
function json_apiCheckAuthCookie() |
| 262 |
{ |
| 263 |
global $json_api; |
| 264 |
|
| 265 |
if (isset($json_api->query) && $json_api->query->cookie) { |
| 266 |
$user_id = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in'); |
| 267 |
if ($user_id) { |
| 268 |
$user = get_userdata($user_id); |
| 269 |
wp_set_current_user($user->ID, $user->user_login); |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
// function add_checkout_page() { |
| 275 |
// deleteDuplicateCheckoutPages(); |
| 276 |
// $my_post = array( |
| 277 |
// 'post_type' => 'page', |
| 278 |
// 'post_name' => 'mstore-checkout', |
| 279 |
// 'post_title' => 'Mstore Checkout', |
| 280 |
// 'post_status' => 'publish', |
| 281 |
// 'post_author' => 1, |
| 282 |
// 'post_type' => 'page' |
| 283 |
// ); |
| 284 |
|
| 285 |
// // Insert the post into the database |
| 286 |
// $page_id = wp_insert_post( $my_post ); |
| 287 |
// update_post_meta( $page_id, '_wp_page_template', 'templates/mstore-api-template.php' ); |
| 288 |
// } |
| 289 |
|
| 290 |
|
| 291 |
/** |
| 292 |
* Register the mstore caching endpoints so they will be cached. |
| 293 |
*/ |
| 294 |
function wprc_add_mstore_endpoints( $allowed_endpoints ) { |
| 295 |
if ( ! isset( $allowed_endpoints[ 'mstore/v1' ] ) || ! in_array( 'cache', $allowed_endpoints[ 'mstore/v1' ] ) ) { |
| 296 |
$allowed_endpoints[ 'mstore/v1' ][] = 'cache'; |
| 297 |
} |
| 298 |
return $allowed_endpoints; |
| 299 |
} |
| 300 |
add_filter( 'wp_rest_cache/allowed_endpoints', 'wprc_add_mstore_endpoints', 10, 1); |
| 301 |
|
| 302 |
add_filter( 'woocommerce_rest_prepare_product_variation_object','custom_woocommerce_rest_prepare_product_variation_object' ); |
| 303 |
add_filter( 'woocommerce_rest_prepare_product_object','custom_change_product_response', 20, 3 ); |
| 304 |
function custom_change_product_response( $response ) { |
| 305 |
global $woocommerce_wpml; |
| 306 |
|
| 307 |
if ( ! empty( $woocommerce_wpml->multi_currency ) && ! empty( $woocommerce_wpml->settings['currencies_order'] ) ) { |
| 308 |
|
| 309 |
$type = $response->data['type']; |
| 310 |
$price = $response->data['price']; |
| 311 |
|
| 312 |
foreach ( $woocommerce_wpml->settings['currency_options'] as $key => $currency ) { |
| 313 |
$rate = (float)$currency["rate"]; |
| 314 |
$response->data['multi-currency-prices'][ $key ]['price'] = $rate == 0 ? $price : sprintf("%.2f", $price*$rate); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
$product = wc_get_product($response->data['id']); |
| 319 |
$attributes = $product->get_attributes(); |
| 320 |
$attributesData = []; |
| 321 |
foreach ( $attributes as $attr ) { |
| 322 |
if ( $attr->is_taxonomy() ) { |
| 323 |
$taxonomy = $attr->get_taxonomy_object(); |
| 324 |
$label = $taxonomy->attribute_label; |
| 325 |
}else{ |
| 326 |
$label = $attr->get_name(); |
| 327 |
} |
| 328 |
$attr["options"] = wc_get_product_terms($response->data['id'],$attr["name"]); |
| 329 |
$attributesData[] = array_merge($attr->get_data(), ["label"=>$label]); |
| 330 |
} |
| 331 |
$response->data['attributesData'] = $attributesData; |
| 332 |
|
| 333 |
return $response; |
| 334 |
} |
| 335 |
|
| 336 |
function custom_woocommerce_rest_prepare_product_variation_object( $response ) { |
| 337 |
|
| 338 |
global $woocommerce_wpml; |
| 339 |
|
| 340 |
if ( ! empty( $woocommerce_wpml->multi_currency ) && ! empty( $woocommerce_wpml->settings['currencies_order'] ) ) { |
| 341 |
|
| 342 |
$type = $response->data['type']; |
| 343 |
$price = $response->data['price']; |
| 344 |
|
| 345 |
foreach ( $woocommerce_wpml->settings['currency_options'] as $key => $currency ) { |
| 346 |
$rate = (float)$currency["rate"]; |
| 347 |
$response->data['multi-currency-prices'][ $key ]['price'] = $rate == 0 ? $price : sprintf("%.2f", $price*$rate); |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
return $response; |
| 352 |
} |
| 353 |
|
| 354 |
//Prepare data before checkout by webview |
| 355 |
function prepare_checkout() { |
| 356 |
|
| 357 |
if ( isset( $_GET['mobile'] ) && isset( $_GET['code'] ) ) { |
| 358 |
|
| 359 |
$code = $_GET['code']; |
| 360 |
global $wpdb; |
| 361 |
$table_name = $wpdb->prefix . "mstore_checkout"; |
| 362 |
$item = $wpdb->get_row( "SELECT * FROM $table_name WHERE code = '$code'" ); |
| 363 |
if ($item) { |
| 364 |
$data = json_decode(urldecode(base64_decode($item->order)), true); |
| 365 |
}else{ |
| 366 |
return var_dump("Can't not get the order"); |
| 367 |
} |
| 368 |
|
| 369 |
$shipping = isset($data['shipping']) ? $data['shipping'] : $data['billing']; |
| 370 |
$billing = isset($data['billing']) ? $data['billing'] : $shipping; |
| 371 |
|
| 372 |
if (isset($data['token'])) { |
| 373 |
// Validate the cookie token |
| 374 |
$userId = wp_validate_auth_cookie($data['token'], 'logged_in'); |
| 375 |
if (isset($billing)) { |
| 376 |
update_user_meta($userId, 'billing_first_name', $billing["first_name"]); |
| 377 |
update_user_meta($userId, 'billing_last_name', $billing["last_name"]); |
| 378 |
update_user_meta($userId, 'billing_company', $billing["company"]); |
| 379 |
update_user_meta($userId, 'billing_address_1', $billing["address_1"]); |
| 380 |
update_user_meta($userId, 'billing_address_2', $billing["address_2"]); |
| 381 |
update_user_meta($userId, 'billing_city', $billing["city"]); |
| 382 |
update_user_meta($userId, 'billing_state', $billing["state"]); |
| 383 |
update_user_meta($userId, 'billing_postcode', $billing["postcode"]); |
| 384 |
update_user_meta($userId, 'billing_country', $billing["country"]); |
| 385 |
update_user_meta($userId, 'billing_email', $billing["email"]); |
| 386 |
update_user_meta($userId, 'billing_phone', $billing["phone"]); |
| 387 |
|
| 388 |
update_user_meta($userId, 'shipping_first_name', $billing["first_name"]); |
| 389 |
update_user_meta($userId, 'shipping_last_name', $billing["last_name"]); |
| 390 |
update_user_meta($userId, 'shipping_company', $billing["company"]); |
| 391 |
update_user_meta($userId, 'shipping_address_1', $billing["address_1"]); |
| 392 |
update_user_meta($userId, 'shipping_address_2', $billing["address_2"]); |
| 393 |
update_user_meta($userId, 'shipping_city', $billing["city"]); |
| 394 |
update_user_meta($userId, 'shipping_state', $billing["state"]); |
| 395 |
update_user_meta($userId, 'shipping_postcode', $billing["postcode"]); |
| 396 |
update_user_meta($userId, 'shipping_country', $billing["country"]); |
| 397 |
update_user_meta($userId, 'shipping_email', $billing["email"]); |
| 398 |
update_user_meta($userId, 'shipping_phone', $billing["phone"]); |
| 399 |
}else{ |
| 400 |
$billing = []; |
| 401 |
$shipping = []; |
| 402 |
|
| 403 |
$billing["first_name"] = get_user_meta($userId, 'billing_first_name', true ); |
| 404 |
$billing["last_name"] = get_user_meta($userId, 'billing_last_name', true ); |
| 405 |
$billing["company"] = get_user_meta($userId, 'billing_company', true ); |
| 406 |
$billing["address_1"] = get_user_meta($userId, 'billing_address_1', true ); |
| 407 |
$billing["address_2"] = get_user_meta($userId, 'billing_address_2', true ); |
| 408 |
$billing["city"] = get_user_meta($userId, 'billing_city', true ); |
| 409 |
$billing["state"] = get_user_meta($userId, 'billing_state', true ); |
| 410 |
$billing["postcode"] = get_user_meta($userId, 'billing_postcode', true ); |
| 411 |
$billing["country"] = get_user_meta($userId, 'billing_country', true ); |
| 412 |
$billing["email"] = get_user_meta($userId, 'billing_email', true ); |
| 413 |
$billing["phone"] = get_user_meta($userId, 'billing_phone', true ); |
| 414 |
|
| 415 |
$shipping["first_name"] = get_user_meta($userId, 'shipping_first_name', true ); |
| 416 |
$shipping["last_name"] = get_user_meta($userId, 'shipping_last_name', true ); |
| 417 |
$shipping["company"] = get_user_meta($userId, 'shipping_company', true ); |
| 418 |
$shipping["address_1"] = get_user_meta($userId, 'shipping_address_1', true ); |
| 419 |
$shipping["address_2"] = get_user_meta($userId, 'shipping_address_2', true ); |
| 420 |
$shipping["city"] = get_user_meta($userId, 'shipping_city', true ); |
| 421 |
$shipping["state"] = get_user_meta($userId, 'shipping_state', true ); |
| 422 |
$shipping["postcode"] = get_user_meta($userId, 'shipping_postcode', true ); |
| 423 |
$shipping["country"] = get_user_meta($userId, 'shipping_country', true ); |
| 424 |
$shipping["email"] = get_user_meta($userId, 'shipping_email', true ); |
| 425 |
$shipping["phone"] = get_user_meta($userId, 'shipping_phone', true ); |
| 426 |
|
| 427 |
if (isset($billing["first_name"]) && !isset($shipping["first_name"])) { |
| 428 |
$shipping = $billing; |
| 429 |
} |
| 430 |
if (!isset($billing["first_name"]) && isset($shipping["first_name"])) { |
| 431 |
$billing = $shipping; |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
// Check user and authentication |
| 436 |
$user = get_userdata($userId); |
| 437 |
if ($user && (!is_user_logged_in() || get_current_user_id() != $userId)) { |
| 438 |
wp_set_current_user( $userId, $user->user_login ); |
| 439 |
wp_set_auth_cookie( $userId ); |
| 440 |
|
| 441 |
header( "Refresh:0" ); |
| 442 |
} |
| 443 |
} else { |
| 444 |
if ( is_user_logged_in()) { |
| 445 |
wp_logout(); |
| 446 |
wp_set_current_user( 0 ); |
| 447 |
header( "Refresh:0" ); |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
global $woocommerce; |
| 452 |
WC()->session->set( 'refresh_totals', true ); |
| 453 |
WC()->cart->empty_cart(); |
| 454 |
|
| 455 |
$products = $data['line_items']; |
| 456 |
foreach ($products as $product) { |
| 457 |
$productId = absint($product['product_id']); |
| 458 |
|
| 459 |
$quantity = $product['quantity']; |
| 460 |
$variationId = isset($product['variation_id']) ? $product['variation_id'] : ""; |
| 461 |
|
| 462 |
$attributes = []; |
| 463 |
if(isset($product["meta_data"])){ |
| 464 |
foreach ($product["meta_data"] as $item) { |
| 465 |
$attributes[$item["key"]] = $item["value"]; |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
// Check the product variation |
| 470 |
if (!empty($variationId)) { |
| 471 |
$productVariable = new WC_Product_Variable($productId); |
| 472 |
$listVariations = $productVariable->get_available_variations(); |
| 473 |
foreach ($listVariations as $vartiation => $value) { |
| 474 |
if ($variationId == $value['variation_id']) { |
| 475 |
$attributes = array_merge($value['attributes'], $attributes); |
| 476 |
$woocommerce->cart->add_to_cart($productId, $quantity, $variationId, $attributes); |
| 477 |
} |
| 478 |
} |
| 479 |
} else { |
| 480 |
$woocommerce->cart->add_to_cart($productId, $quantity, 0, $attributes); |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
if(isset($shipping)){ |
| 485 |
$woocommerce->customer->set_shipping_first_name($shipping["first_name"]); |
| 486 |
$woocommerce->customer->set_shipping_last_name($shipping["last_name"]); |
| 487 |
$woocommerce->customer->set_shipping_company($shipping["company"]); |
| 488 |
$woocommerce->customer->set_shipping_address_1($shipping["address_1"]); |
| 489 |
$woocommerce->customer->set_shipping_address_2($shipping["address_2"]); |
| 490 |
$woocommerce->customer->set_shipping_city($shipping["city"]); |
| 491 |
$woocommerce->customer->set_shipping_state($shipping["state"]); |
| 492 |
$woocommerce->customer->set_shipping_postcode($shipping["postcode"]); |
| 493 |
$woocommerce->customer->set_shipping_country($shipping["country"]); |
| 494 |
} |
| 495 |
|
| 496 |
if(isset($billing)){ |
| 497 |
$woocommerce->customer->set_billing_first_name($billing["first_name"]); |
| 498 |
$woocommerce->customer->set_billing_last_name($billing["last_name"]); |
| 499 |
$woocommerce->customer->set_billing_company($billing["company"]); |
| 500 |
$woocommerce->customer->set_billing_address_1($billing["address_1"]); |
| 501 |
$woocommerce->customer->set_billing_address_2($billing["address_2"]); |
| 502 |
$woocommerce->customer->set_billing_city($billing["city"]); |
| 503 |
$woocommerce->customer->set_billing_state($billing["state"]); |
| 504 |
$woocommerce->customer->set_billing_postcode($billing["postcode"]); |
| 505 |
$woocommerce->customer->set_billing_country($billing["country"]); |
| 506 |
$woocommerce->customer->set_billing_email($billing["email"]); |
| 507 |
$woocommerce->customer->set_billing_phone($billing["phone"]); |
| 508 |
} |
| 509 |
|
| 510 |
if (!empty($data['coupon_lines'])) { |
| 511 |
$coupons = $data['coupon_lines']; |
| 512 |
foreach ($coupons as $coupon) { |
| 513 |
$woocommerce->cart->add_discount($coupon['code']); |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
if (!empty($data['shipping_lines'])) { |
| 518 |
$shippingLines = $data['shipping_lines']; |
| 519 |
$shippingMethod = $shippingLines[0]['method_id']; |
| 520 |
WC()->session->set( 'chosen_shipping_methods', array($shippingMethod) ); |
| 521 |
} |
| 522 |
if (!empty($data['payment_method'])) { |
| 523 |
WC()->session->set('chosen_payment_method', $data['payment_method']); |
| 524 |
} |
| 525 |
if(isset($data['customer_note']) && !empty($data['customer_note'])){ |
| 526 |
$_POST["order_comments"] = $data['customer_note']; |
| 527 |
$checkout_fields = WC()->checkout->__get("checkout_fields"); |
| 528 |
$checkout_fields["order"] = ["order_comments"=>["type"=>"textarea", "class"=>[], "label"=>"Order notes", "placeholder"=>"Notes about your order, e.g. special notes for delivery."]]; |
| 529 |
WC()->checkout->__set("checkout_fields", $checkout_fields); |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
if(isset( $_GET['cookie'] )){ |
| 534 |
$cookie = urldecode(base64_decode($_GET['cookie'])); |
| 535 |
$userId = wp_validate_auth_cookie($cookie, 'logged_in'); |
| 536 |
if ($userId !== false) { |
| 537 |
$user = get_userdata($userId); |
| 538 |
if ($user !== false) { |
| 539 |
wp_set_current_user( $userId, $user->user_login ); |
| 540 |
wp_set_auth_cookie( $userId ); |
| 541 |
if (isset( $_GET['vendor_admin'] )) { |
| 542 |
global $wp; |
| 543 |
$request = $wp->request; |
| 544 |
wp_redirect( home_url("/".$request) ); |
| 545 |
die; |
| 546 |
} |
| 547 |
} |
| 548 |
} |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
// Add product image to order |
| 553 |
add_filter('woocommerce_rest_prepare_shop_order_object', 'custom_woocommerce_rest_prepare_shop_order_object', 10, 1); |
| 554 |
function custom_woocommerce_rest_prepare_shop_order_object( $response ) { |
| 555 |
if( empty( $response->data ) ){ |
| 556 |
return $response; |
| 557 |
} |
| 558 |
$api = new WC_REST_Products_Controller(); |
| 559 |
$line_items = []; |
| 560 |
foreach($response->data['line_items'] as $item){ |
| 561 |
$product_id= $item['product_id']; |
| 562 |
$res = $api->get_item(["id"=>$product_id]); |
| 563 |
if(is_wp_error( $res )){ |
| 564 |
$item["product_data"] = null; |
| 565 |
}else{ |
| 566 |
$item["product_data"] = $res->get_data(); |
| 567 |
} |
| 568 |
$line_items[] = $item; |
| 569 |
|
| 570 |
} |
| 571 |
$response->data['line_items'] = $line_items; |
| 572 |
return $response; |
| 573 |
} |