function logs_detail() { $total_post = get_option('post_product'); $LAIDKey = get_option('linksync_laid'); update_option('linksync_sycning_status', 'completed'); linksync_class::add('Product Sync Woo to Vend', 'success', $total_post . ' Product synced', $LAIDKey); }
if (isset($accounts_expense) && !empty($accounts_expense)) { $product['expense_account_id'] = $accounts_expense; } else { $product['expense_account_id'] = null; } $accounts_revenue = get_option('ps_account_revenue'); if (isset($accounts_revenue) && !empty($accounts_revenue)) { $product['income_account_id'] = $accounts_revenue; } else { $product['income_account_id'] = null; } //include_tax $product['includes_tax'] = isset($_POST['_tax_status']) && $_POST['_tax_status'] == 'taxable' ? true : false; $data = json_encode($product); $response = $apicall->linksync_postProduct($data); linksync_class::add('Product Sync Woo to QBO', 'success', 'Product synced SKU:' . $product['sku'], $LAIDKey); } } } } } else { die('Error in Configuration ' . $apicall['lastresponse']['message']); } } } } // Helper functions function linksyn_tax_QBO($taxname) { $result = array(); $taxDb = get_option('tax_class');
update_option('ps_delete', 'off'); } update_option('prod_update_suc', NULL); update_option('prod_last_page', NULL); update_option('product_detail', NULL); if ($_POST['product_sync_type_QBO'] == 'two_way' || $_POST['product_sync_type_QBO'] == 'QB_to_wc-way') { // Set Import To Yes on the base of point 31 update_option('product_import', 'yes'); $result = $apicall->testConnection(); $plugin_file = dirname(__FILE__) . '/linksync.php'; $plugin_data = get_plugin_data($plugin_file, $markup = true, $translate = true); $linksync_version = $plugin_data['Version']; $webhook = $apicall->webhookConnection(plugins_url() . '/linksync/update.php?c=' . get_option('webhook_url_code'), $linksync_version, get_option('order_import'), 'yes'); if (isset($webhook) && !empty($webhook)) { if (isset($webhook['result']) && $webhook['result'] == 'success') { $apicall->add('WebHookConnection', 'success', 'Connected to a file ' . plugins_url() . '/linksync/update.php?c=' . get_option('webhook_url_code'), $LAIDKey); update_option('linksync_addedfile', '<a href="' . plugins_url() . '/linksync/update.php?c=' . get_option('webhook_url_code') . '">' . plugins_url() . '/linksync/update.php?c=' . get_option('webhook_url_code') . '</a>'); } } else { $apicall->add('WebHookConnection', 'fail', 'Product-Config File: Connected to a file ' . plugins_url() . '/linksync/update.php?c=' . get_option('webhook_url_code'), $LAIDKey); } } else { update_option('product_import', 'no'); $result = $apicall->testConnection(); $plugin_file = dirname(__FILE__) . '/linksync.php'; $plugin_data = get_plugin_data($plugin_file, $markup = true, $translate = true); $linksync_version = $plugin_data['Version']; $webhook = $apicall->webhookConnection(plugins_url() . '/linksync/update.php?c=' . get_option('webhook_url_code'), $linksync_version, get_option('order_import'), 'no'); if (isset($webhook) && !empty($webhook)) { if (isset($webhook['result']) && $webhook['result'] == 'success') { $apicall->add('WebHookConnection', 'success', 'Connected to a file ' . plugins_url() . '/linksync/update.php?c=' . get_option('webhook_url_code'), $LAIDKey);
} } if (isset($_POST['in_woo']) && !empty($_POST['in_woo'])) { $laids = get_option('linksync_laid'); foreach ($_POST['product_sku'] as $product_id => $product_sku) { if (!empty($product_sku)) { $count = wp_delete_post($product_id); //use the product Id and delete the product if ($count) { $method = "Success"; $message = 'Product Sku:' . $product_sku . ', Product Id in Woo:' . $product_id; } else { $method = "Error"; $message = "Unable to Delete Product"; } linksync_class::add('Product Deleted(In Woo Store):Clean Up', $method, $message, $laids); } } } $class1 = 'error'; $class2 = 'updated'; $message_resp = "Clean up Run Successfully!!"; } else { $class1 = 'updated'; $class2 = 'error'; $message_resp = "No Product(s) Selected !!"; } ?> <script> linksync_jQuery1('#response').removeClass("<?php echo $class1;
}); </script><?php } if ($_POST['product_sync_type'] != 'disabled_sync') { if ($_POST['product_sync_type'] == 'vend_to_wc-way') { $enable = 'Vend to Woo'; } elseif ($_POST['product_sync_type'] == 'two_way') { $enable = 'Two way'; } else { $enable = 'Woo to Vend'; } $setting_message = $enable . ' enable'; } else { $setting_message = 'Sync Setting Disabled'; } linksync_class::add('Product Sync Setting', 'success', $setting_message, $LAIDKey); } update_option('image_process', 'complete'); } elseif (isset($_POST['sync_reset_btn'])) { update_option('prod_update_suc', NULL); update_option('prod_last_page', NULL); update_option('product_detail', NULL); update_option('image_process', 'complete'); ?> <div id="pop_up" class="clientssummarybox" style=" width:600px !important; top: 24% !important; display: none; z-index: 999999999; position: fixed !important; padding: 10px !important; line-height: 30px !important; left: 25%; position: absolute; top: 100%; float: left; background-color: #ffffff; border: 1px solid #ccc; border: 1px solid rgba(0, 0, 0, 0.2); -webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px; -webkit-box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2); -moz-box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2); box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2); -webkit-background-clip: padding-box; -moz-background-clip: padding; background-clip: padding-box; "> <center><div id="showMessage"><center><h4>Your changes will require a full re-sync of product data from Vend.</h4></center> <center><h4>Do you want to re-sync now?</h4></center></div> </center> <center><h4 style="display:none;" id="syncing_loader1"><img src="../wp-content/plugins/linksync/img/ajax-loader.gif"></h4></center> <center><h4 id="sync_start"></h4></center> <center><div id="total_product"></div></center>
update_option('prod_last_page', NULL); update_option('product_detail', NULL); } } if (isset($products['errorCode']) || !isset($products) || empty($products)) { update_option('prod_update_suc', get_option('prod_update_suc')); } if (isset($products['pagination']['results']) && $products['pagination']['results'] != 0) { linksync_class::add('Product Sync Vend to Woo', 'success', $products['pagination']['results'] . ' Product(s) synced.', $LAIDKey); } $message['message'] .= 'Product Sync:Complete Successfully!!'; } else { $message['message'] .= '<span style="color:#d54e21;">Product Sync has been Disabled Or Not Selected</span>'; } } else { linksync_class::add('Webhook Triggered', 'error', 'Invalid Request', ''); # Error to be loggged } fflush($fp); // flush output before releasing the lock flock($fp, LOCK_UN); // release the lock } else { echo "Couldn't get the lock!"; } if (isset($message['message']) && !empty($message['message'])) { update_option('product_detail', NULL); echo json_encode($message); exit; } exit;
function order_product_post() { global $wpdb; $testMode = get_option('linksync_test'); $LAIDKey = get_option('linksync_laid'); $apicall = new linksync_class($LAIDKey, $testMode); if (isset($_REQUEST['key']) && !empty($_REQUEST['key'])) { $orderId = wc_get_orderid_by_order_key($_REQUEST['key']); } else { $orderId = $_POST['ID']; } //Checking for already sent Order $sentOrderIds = get_option('linksync_sent_order_id'); if (isset($sentOrderIds)) { if (!empty($sentOrderIds)) { $order_id_array = unserialize($sentOrderIds); } else { $order_id_array = array(); } if (!in_array($orderId, $order_id_array)) { update_option('linksync_sent_order_id', serialize(array_merge($order_id_array, array($orderId)))); $taxsetup = false; $product = array(); $order = new WC_Order($orderId); //Ordered product(s) $items = $order->get_items(); foreach ($items as $item) { $product = array(); $product_id = $item['product_id']; $query = mysql_query("SELECT post_status,post_content FROM `" . $wpdb->prefix . "posts` WHERE ID='" . $product_id . "'"); if (mysql_num_rows($query) != 0) { $result = mysql_fetch_assoc($query); if (isset($result) && !empty($result)) { if ($result['post_status'] != 'trash') { $post_detail = get_post_meta($product_id); if (@empty($post_detail['_sku'][0])) { $post_detail['_sku'][0] = 'sku_' . $product_id; } $post_detail['_sku'][0] = linksync_removespaces_sku_orderProduct($post_detail['_sku'][0]); update_post_meta($product_id, '_sku', $post_detail['_sku'][0]); $product['sku'] = html_entity_decode($post_detail['_sku'][0]); //SKU(unique Key) //product status ->publish $product['active'] = isset($result['post_status']) && $result['post_status'] == 'publish' ? 1 : 0; // Price with Tax if (get_option('woocommerce_calc_taxes') == 'yes') { if (get_option('linksync_woocommerce_tax_option') == 'on') { if (get_option('woocommerce_prices_include_tax') == 'yes') { $excluding_tax = 'off'; } else { $excluding_tax = 'on'; } } else { $excluding_tax = get_option('excluding_tax'); } } else { $excluding_tax = get_option('excluding_tax'); } $display_retail_price_tax_inclusive = get_option('linksync_tax_inclusive'); if (get_option('ps_price') == 'on') { if (isset($post_detail['_tax_status'][0]) && $post_detail['_tax_status'][0] == 'taxable') { # Product with TAX $taxname = empty($post_detail['_tax_class'][0]) ? 'standard-tax' : $post_detail['_tax_class'][0]; $response_taxes = linksyn_get_tax_details($taxname); // echo"<pre>"; print_r($response_taxes); if ($response_taxes['result'] == 'success') { $product['tax_name'] = html_entity_decode($response_taxes['data']['tax_name']); $product['tax_rate'] = $response_taxes['data']['tax_rate']; $taxsetup = true; } } if ($excluding_tax == 'on') { # https://www.evernote.com/shard/s144/sh/e63f527b-903f-4002-8f00-313ff0652290/d9c1e0ce5a95800a if ($taxsetup) { if (isset($post_detail['_regular_price'][0]) && !empty($post_detail['_regular_price'][0])) { //cost price:_regular_price $regular_price = (double) $post_detail['_regular_price'][0]; // Get Tax_value $tax_rate = (double) $product['tax_rate']; $tax_value = (double) ($regular_price * $tax_rate); /* For excluding tax (both Woo Tax Excluding and Vend Tax Excluding) * display_retail_price_tax_inclusive 1, sell_price = Woo Final price + tax * For display_retail_price_tax_inclusive 0, sell_price = Woo Final price */ if ($display_retail_price_tax_inclusive == '1') { $price = $post_detail['_regular_price'][0] + $tax_value; } elseif ($display_retail_price_tax_inclusive == '0') { $price = $post_detail['_regular_price'][0]; } //sell price:_regular_price $product['sell_price'] = str_replace(',', '', $price); $product['list_price'] = str_replace(',', '', $price); $product['tax_value'] = $tax_value; } } else { // excluding tax off and tax not enabled in woocomerce if (isset($post_detail['_regular_price'][0]) && !empty($post_detail['_regular_price'][0])) { //cost price:_regular_price //sell price:_regular_price $product['sell_price'] = str_replace(',', '', $post_detail['_regular_price'][0]); $product['list_price'] = str_replace(',', '', $post_detail['_regular_price'][0]); } } } else { // No effect on price if (isset($post_detail['_regular_price'][0]) && !empty($post_detail['_regular_price'][0])) { $regular_price = (double) $post_detail['_regular_price'][0]; // Get Tax_value $tax_rate = (double) $product['tax_rate']; $tax_value = $regular_price - $regular_price / (1 + $tax_rate); if ($display_retail_price_tax_inclusive == '1') { } elseif ($display_retail_price_tax_inclusive == '0') { $post_detail['_regular_price'][0] = $post_detail['_regular_price'][0] - $tax_value; } //sell price:_regular_price $product['sell_price'] = str_replace(',', '', $post_detail['_regular_price'][0]); $product['list_price'] = str_replace(',', '', $post_detail['_regular_price'][0]); } } } if (isset($post_detail['_stock_status'][0]) && $post_detail['_stock_status'][0] == 'instock') { $product['quantity'] = isset($post_detail['_stock'][0]) ? $post_detail['_stock'][0] : 0; } #Name/Title Check if (get_option('ps_name_title') == 'on') { $product['name'] = html_entity_decode($item['name']); } #Description if (get_option('ps_description') == 'on') { $product['description'] = html_entity_decode($result['post_content']); } $product['includes_tax'] = isset($post_detail['_tax_status'][0]) && $post_detail['_tax_status'][0] == 'taxable' ? true : false; #---Outlet---Product----# if (get_option('ps_quantity') == 'on') { if (get_option('ps_wc_to_vend_outlet') == 'on') { $getoutlets = get_option('wc_to_vend_outlet_detail'); if (isset($getoutlets) && !empty($getoutlets)) { $outlet = explode('|', $getoutlets); if (isset($post_detail['_stock'][0]) && !empty($post_detail['_stock'][0])) { $product['outlets'] = array(array('name' => html_entity_decode($outlet[0]), 'quantity' => $post_detail['_stock'][0])); } } } } else { $product['outlets'] = array(array('quantity' => NULL)); } #qunantity // #Tags if (get_option('ps_tags') == 'on') { //To get the Detail of the Tags and Category of the product using product id(Post ID) $tags_query = "SELECT " . $wpdb->prefix . "terms.name FROM `" . $wpdb->prefix . "term_taxonomy` JOIN " . $wpdb->prefix . "terms ON(" . $wpdb->prefix . "terms.term_id=" . $wpdb->prefix . "term_taxonomy.term_id) JOIN " . $wpdb->prefix . "term_relationships ON(" . $wpdb->prefix . "term_relationships.term_taxonomy_id=" . $wpdb->prefix . "term_taxonomy.term_taxonomy_id) WHERE " . $wpdb->prefix . "term_taxonomy.`taxonomy`='product_tag' AND " . $wpdb->prefix . "term_relationships.object_id='" . $product_id . "'"; $result_tags = mysql_query($tags_query) or die(mysql_error()); // if (!$result_tags) // die("Error In Connection : " . mysql_error() . " Line No. " . __LINE__); if (mysql_num_rows($result_tags) != 0) { $tags_product_type = array(); while ($row_tags = mysql_fetch_assoc($result_tags)) { $tags_product_type[] = array('name' => html_entity_decode($row_tags['name'])); } } if (isset($tags_product_type) && !empty($tags_product_type)) { $product['tags'] = $tags_product_type; } //To free an array to use futher unset($tags_product_type); } #brands if (get_option('ps_brand') == 'on') { //To get the Detail of the Tags and Category of the product using product id(Post ID) $brands_query = "SELECT " . $wpdb->prefix . "terms.name FROM `" . $wpdb->prefix . "term_taxonomy` JOIN " . $wpdb->prefix . "terms ON(" . $wpdb->prefix . "terms.term_id=" . $wpdb->prefix . "term_taxonomy.term_id) JOIN " . $wpdb->prefix . "term_relationships ON(" . $wpdb->prefix . "term_relationships.term_taxonomy_id=" . $wpdb->prefix . "term_taxonomy.term_taxonomy_id) WHERE " . $wpdb->prefix . "term_taxonomy.`taxonomy`='product_brand' AND " . $wpdb->prefix . "term_relationships.object_id='" . $product_id . "'"; $result_brands = mysql_query($brands_query) or die(mysql_error()); if (mysql_num_rows($result_brands) != 0) { while ($row_brands = mysql_fetch_assoc($result_brands)) { $brands[] = array('name' => html_entity_decode($row_brands['name'])); } } if (!empty($brands)) { $product['brands'] = $brands; } //To free an array to use futher unset($brands); } #Variants product $variants_data = get_posts(array('post_type' => 'product_variation', 'post_parent' => $product_id)); if (isset($variants_data) && !empty($variants_data)) { $total_var_product = 0; foreach ($variants_data as $variant_data) { $option = array(1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', 6 => 'six', 7 => 'seven', 8 => 'eight', 9 => 'nine', 10 => 'ten'); $variants_detail = get_post_meta($variant_data->ID); if (@empty($variants_detail['_sku'][0])) { $variants_detail['_sku'][0] = 'sku_' . $variant_data->ID; } $variants_detail['_sku'][0] = linksync_removespaces_sku_orderProduct($variants_detail['_sku'][0]); update_post_meta($variant_data->ID, '_sku', $variants_detail['_sku'][0]); $variant['sku'] = html_entity_decode($variants_detail['_sku'][0]); //SKU(unique Key) #Name/Title Check if (get_option('ps_name_title') == 'on') { $variant['name'] = html_entity_decode($variant_data->post_title); } #quantity if (@$variants_detail['_stock_status'][0] == 'instock') { $variant['quantity'] = @$variants_detail['_stock'][0]; } // Price with Tax if (get_option('ps_price') == 'on') { if (isset($variants_detail['_tax_status'][0]) && $variants_detail['_tax_status'][0] == 'taxable') { # Product with TAX $taxname = empty($variants_detail['_tax_class'][0]) ? 'standard-tax' : $variants_detail['_tax_class'][0]; $response_taxes = linksyn_get_tax_details($taxname); if ($response_taxes['result'] == 'success') { $variant['tax_name'] = html_entity_decode($response_taxes['data']['tax_name']); $variant['tax_rate'] = $response_taxes['data']['tax_rate']; $taxsetup = true; } } if ($excluding_tax == 'on') { # https://www.evernote.com/shard/s144/sh/e63f527b-903f-4002-8f00-313ff0652290/d9c1e0ce5a95800a if ($taxsetup) { if (isset($variants_detail['_regular_price'][0]) && !empty($variants_detail['_regular_price'][0])) { //cost price:_regular_price $regular_price = (double) $variants_detail['_regular_price'][0]; // Get Tax_value $tax_rate = (double) $variant['tax_rate']; $tax_value = (double) ($regular_price * $tax_rate); //sell price:_regular_price if ($display_retail_price_tax_inclusive == '1') { $price_variant = $variants_detail['_regular_price'][0] + $tax_value; } elseif ($display_retail_price_tax_inclusive == '0') { $price_variant = $variants_detail['_regular_price'][0]; } $variant['sell_price'] = str_replace(',', '', $price_variant); $variant['list_price'] = str_replace(',', '', $price_variant); $variant['tax_value'] = $tax_value; } } else { // excluding tax off and tax not enabled in woocomerce if (isset($variants_detail['_regular_price'][0]) && !empty($variants_detail['_regular_price'][0])) { //sell price:_regular_price $variant['sell_price'] = str_replace(',', '', $variants_detail['_regular_price'][0]); $variant['list_price'] = str_replace(',', '', $variants_detail['_regular_price'][0]); } } } else { // No effect on price if (isset($variants_detail['_regular_price'][0]) && !empty($variants_detail['_regular_price'][0])) { $regular_price = (double) $variants_detail['_regular_price'][0]; // Get Tax_value $tax_rate = (double) $variant['tax_rate']; $tax_value = $regular_price - $regular_price / (1 + $tax_rate); if ($display_retail_price_tax_inclusive == '1') { } elseif ($display_retail_price_tax_inclusive == '0') { $variants_detail['_regular_price'][0] = $variants_detail['_regular_price'][0] - $tax_value; } //sell price:_regular_price $variant['sell_price'] = str_replace(',', '', $variants_detail['_regular_price'][0]); $variant['list_price'] = str_replace(',', '', $variants_detail['_regular_price'][0]); $variant['tax_value'] = $tax_value; } } } // ATTRIBUTE && VARIANTS $attributes_select = mysql_query("SELECT * FROM `" . $wpdb->prefix . "woocommerce_attribute_taxonomies`") or die(mysql_error()); $check = 1; // $variants_detail['attribute_pa_' . strtolower($attributes['attribute_name'])][0] if (mysql_num_rows($attributes_select) != 0) { $keys = array_keys($variants_detail); if (false !== stripos(implode("\n", $keys), "attribute_pa_")) { while ($attributes = mysql_fetch_assoc($attributes_select)) { if (isset($variants_detail['attribute_pa_' . strtolower($attributes['attribute_name'])]) && !empty($variants_detail['attribute_pa_' . strtolower($attributes['attribute_name'])])) { $attribute_name = str_replace('pa_', '', $attributes['attribute_name']); $attribute_query = mysql_query("SELECT attribute_label FROM `" . $wpdb->prefix . "woocommerce_attribute_taxonomies` WHERE `attribute_name` = '" . $attribute_name . "'"); if (mysql_num_rows($attribute_query) != 0) { $attribute_name_result = mysql_fetch_assoc($attribute_query); $name = $attribute_name_result['attribute_label']; } $variant['option_' . $option[$check] . '_name'] = isset($name) ? $name : ''; $query = mysql_query("SELECT name FROM `" . $wpdb->base_prefix . "terms` WHERE `slug` = '" . $variants_detail['attribute_pa_' . strtolower($attributes['attribute_name'])][0] . "'"); if (mysql_num_rows($query) != 0) { $attribute_value = mysql_fetch_assoc($query); $value = $attribute_value['name']; } $variant['option_' . $option[$check] . '_value'] = isset($value) ? $value : ''; $check++; } } } else { if (isset($post_detail['_product_attributes'][0]) && !empty($post_detail['_product_attributes'][0])) { $_product_attributes = unserialize($post_detail['_product_attributes'][0]); foreach ($_product_attributes as $attribute_value) { $attributeName = $attribute_value['name']; $_attribute = explode('|', $attribute_value['value']); $value = trim($_attribute[$total_var_product]); $variant['option_' . $option[$check] . '_name'] = isset($attributeName) ? $attributeName : ''; $variant['option_' . $option[$check] . '_value'] = isset($value) ? $value : ''; $check++; } } } } else { if (isset($post_detail['_product_attributes'][0]) && !empty($post_detail['_product_attributes'][0])) { $_product_attributes = unserialize($post_detail['_product_attributes'][0]); foreach ($_product_attributes as $attribute_value) { $attributeName = $attribute_value['name']; $_attribute = explode('|', $attribute_value['value']); $value = trim($_attribute[$total_var_product]); $variant['option_' . $option[$check] . '_name'] = isset($attributeName) ? $attributeName : ''; $variant['option_' . $option[$check] . '_value'] = isset($value) ? $value : ''; $check++; } } } #qunantity-----UPDATE--variant--- if (get_option('ps_quantity') == 'on') { if (get_option('ps_wc_to_vend_outlet') == 'on') { $getoutlets = get_option('wc_to_vend_outlet_detail'); if (isset($getoutlets) && !empty($getoutlets)) { $outlets = explode('|', $getoutlets); if (isset($variants_detail['_stock'][0]) && !empty($variants_detail['_stock'][0])) { $variant['outlets'] = array(array('name' => $outlets[0], 'quantity' => $variants_detail['_stock'][0])); } else { $variant['outlets'] = array(array('name' => html_entity_decode($outlets[0]), 'quantity' => NULL)); } } else { $variant['outlets'] = NULL; } } } else { $variant['outlets'] = array(array('quantity' => NULL)); } $product['variants'][] = $variant; $total_var_product++; } } $data = json_encode($product); $response = $apicall->linksync_postProduct($data); linksync_class::add('Product Sync Woo to Vend', 'success', 'Product synced SKU:' . $product['sku'], $LAIDKey); } } } } } } }
if (!empty($_POST['apikey'])) { $result = linksync::checkForConnection($_POST['apikey']); if (isset($result['success'])) { $status = 'Connected'; } else { $status = 'InValid'; } $data_array = array('api_key' => trim($_POST['apikey']), 'date_add' => date('Y/m/d'), 'status' => $status); if ($wpdb->update($table_name, $data_array, $where)) { linksync_class::add('Manage API Keys', 'success', 'API key Updated Successfully', $_POST['apikey']); $response = 'API key Updated Successfully!! '; } else { linksync_class::add('Manage API Keys', 'fail', 'Unable to Update!!', $_POST['apikey']); } } else { linksync_class::add('Manage API Keys', 'fail', 'API key is empty!!', '-'); $response = "API key is empty!!"; } ?> <script> linksync_jQuery1('#response').removeClass('error').addClass('updated').html("<?php echo $response; ?> ").fadeIn().delay(3000).fadeOut(4000); </script> <?php } if (isset($_POST['apikey_delete'])) { $api_key = explode('|', $_POST['id']); $table_name = $wpdb->prefix . 'linksync_laidKey';
public function importOrderToWoocommerce($orders) { if (isset($orders) && !empty($orders)) { $order_status = get_option('order_vend_to_wc'); //Order Status from order config setting foreach ($orders['orders'] as $order) { if (isset($order['id']) && !empty($order['id'])) { $OrderIds = get_option("Vend_orderIDs"); if (isset($OrderIds) && !empty($OrderIds)) { $Ids = unserialize($OrderIds); } else { $Ids = array(); } if (!in_array($order['id'], $Ids)) { update_option('Vend_orderIDs', serialize(array_merge($Ids, array($order['id'])))); $order_data = array('post_name' => 'order-' . date('M-d-Y-hi-a'), 'post_type' => 'shop_order', 'post_title' => date('M d, Y @ h:i A'), 'post_excerpt' => 'Source: ' . ucfirst($order['source']) . ' Order #' . $order['orderId'], 'post_status' => $order_status, 'ping_status' => 'closed', 'comment_status' => 'open'); $order_id = wp_insert_post($order_data, true); // create order if (is_wp_error($order_id)) { $order->errors = $order_id; } else { if (isset($order['payment']['transactionNumber']) && !empty($order['payment']['transactionNumber'])) { add_post_meta($order_id, 'transaction_id', $order['payment']['transactionNumber'], true); } /* ---------------------------------------Payment Mapping --------------------------------- */ if (isset($order['payment']['retailer_payment_type_id']) && !empty($order['payment']['retailer_payment_type_id'])) { $all_payment = get_option('vend_to_wc_payments'); if (isset($all_payment) && !empty($all_payment)) { $explode_payment = explode(',', $all_payment); foreach ($explode_payment as $payments_method) { $payment_method = explode('|', $payments_method); if (in_array($order['payment']['retailer_payment_type_id'], $payment_method)) { $gatways = new WC_Payment_Gateways(); $payment = $gatways->get_available_payment_gateways(); $wocoomercepayment = $payment_method[1]; foreach ($payment as $payment_method_id => $payment_method_title) { if ($payment_method_title->title == $wocoomercepayment) { add_post_meta($order_id, '_payment_method_title', $wocoomercepayment, true); add_post_meta($order_id, '_payment_method', $payment_method_id, true); } } } } } } $customer_import = get_option('vend_to_wc_customer'); if (isset($customer_import) && $customer_import == 'customer_data') { if (isset($order['billingAddress'])) { add_post_meta($order_id, '_billing_first_name', isset($order['billingAddress']['firstName']) ? $order['billingAddress']['firstName'] : NULL, true); add_post_meta($order_id, '_billing_last_name', isset($order['billingAddress']['lastName']) ? $order['billingAddress']['lastName'] : NULL, true); add_post_meta($order_id, '_billing_company', isset($order['billingAddress']['company']) ? $order['billingAddress']['company'] : NULL, true); add_post_meta($order_id, '_billing_address_1', isset($order['billingAddress']['street1']) ? $order['billingAddress']['street1'] : NULL, true); add_post_meta($order_id, '_billing_address_2', isset($order['billingAddress']['street2']) ? $order['billingAddress']['street2'] : NULL, true); add_post_meta($order_id, '_billing_city', isset($order['billingAddress']['city']) ? $order['billingAddress']['city'] : NULL, true); add_post_meta($order_id, '_billing_postcode', isset($order['billingAddress']['postalCode']) ? $order['billingAddress']['postalCode'] : NULL, true); add_post_meta($order_id, '_billing_country', isset($order['billingAddress']['country']) ? $order['billingAddress']['country'] : NULL, true); add_post_meta($order_id, '_billing_state', isset($order['billingAddress']['state']) ? $order['billingAddress']['state'] : NULL, true); add_post_meta($order_id, '_billing_phone', isset($order['billingAddress']['phone']) ? $order['billingAddress']['phone'] : NULL, true); } if (isset($order['deliveryAddress'])) { add_post_meta($order_id, '_shipping_first_name', isset($order['deliveryAddress']['firstName']) ? $order['deliveryAddress']['firstName'] : NULL, true); add_post_meta($order_id, '_shipping_last_name', isset($order['deliveryAddress']['lastName']) ? $order['deliveryAddress']['lastName'] : NULL, true); add_post_meta($order_id, '_shipping_company', isset($order['deliveryAddress']['company']) ? $order['deliveryAddress']['company'] : NULL, true); add_post_meta($order_id, '_shipping_address_1', isset($order['deliveryAddress']['street1']) ? $order['deliveryAddress']['street1'] : NULL, true); add_post_meta($order_id, '_shipping_address_2', isset($order['deliveryAddress']['street2']) ? $order['deliveryAddress']['street2'] : NULL, true); add_post_meta($order_id, '_shipping_city', isset($order['deliveryAddress']['city']) ? $order['deliveryAddress']['city'] : NULL, true); add_post_meta($order_id, '_shipping_postcode', isset($order['deliveryAddress']['postalCode']) ? $order['deliveryAddress']['postalCode'] : NULL, true); add_post_meta($order_id, '_shipping_country', isset($order['deliveryAddress']['country']) ? $order['deliveryAddress']['country'] : NULL, true); add_post_meta($order_id, '_shipping_state', isset($order['deliveryAddress']['state']) ? $order['deliveryAddress']['state'] : NULL, true); } if (isset($order['primary_email'])) { require_once ABSPATH . 'wp-includes/user.php'; require_once ABSPATH . 'wp-includes/pluggable.php'; $user_email = $order['primary_email']; $user_name = $order['billingAddress']['firstName'] . ' ' . $order['billingAddress']['lastName']; $user_id = email_exists($user_email); $email_password = false; if (!$user_id) { $user_password = wp_generate_password(12, false); $user_id = wp_create_user($user_name, $user_password, $user_email); update_user_option($user_id, 'default_password_nag', true, true); $email_password = true; $message = " Username: {$user_name}\n Password: {$user_password}\n " . wp_login_url(); if (isset($order['billingAddress'])) { add_user_meta($user_id, 'billing_first_name', isset($order['billingAddress']['firstName']) ? $order['billingAddress']['firstName'] : NULL, true); add_user_meta($user_id, 'billing_last_name', isset($order['billingAddress']['lastName']) ? $order['billingAddress']['lastName'] : NULL, true); add_user_meta($user_id, 'billing_company', isset($order['billingAddress']['company']) ? $order['billingAddress']['company'] : NULL, true); add_user_meta($user_id, 'billing_address_1', isset($order['billingAddress']['street1']) ? $order['billingAddress']['street1'] : NULL, true); add_user_meta($user_id, 'billing_address_2', isset($order['billingAddress']['street2']) ? $order['billingAddress']['street2'] : NULL, true); add_user_meta($user_id, 'billing_city', isset($order['billingAddress']['city']) ? $order['billingAddress']['city'] : NULL, true); add_user_meta($user_id, 'billing_postcode', isset($order['billingAddress']['postalCode']) ? $order['billingAddress']['postalCode'] : NULL, true); add_user_meta($user_id, 'billing_country', isset($order['billingAddress']['country']) ? $order['billingAddress']['country'] : NULL, true); add_user_meta($user_id, 'billing_state', isset($order['billingAddress']['state']) ? $order['billingAddress']['state'] : NULL, true); add_user_meta($user_id, 'billing_phone', isset($order['billingAddress']['phone']) ? $order['billingAddress']['phone'] : NULL, true); } if (isset($order['deliveryAddress'])) { add_user_meta($user_id, 'shipping_first_name', isset($order['deliveryAddress']['firstName']) ? $order['deliveryAddress']['firstName'] : NULL, true); add_user_meta($user_id, 'shipping_last_name', isset($order['deliveryAddress']['lastName']) ? $order['deliveryAddress']['lastName'] : NULL, true); add_user_meta($user_id, 'shipping_company', isset($order['deliveryAddress']['company']) ? $order['deliveryAddress']['company'] : NULL, true); add_user_meta($user_id, 'shipping_address_1', isset($order['deliveryAddress']['street1']) ? $order['deliveryAddress']['street1'] : NULL, true); add_user_meta($user_id, 'shipping_address_2', isset($order['deliveryAddress']['street2']) ? $order['deliveryAddress']['street2'] : NULL, true); add_user_meta($user_id, 'shipping_city', isset($order['deliveryAddress']['city']) ? $order['deliveryAddress']['city'] : NULL, true); add_user_meta($user_id, 'shipping_postcode', isset($order['deliveryAddress']['postalCode']) ? $order['deliveryAddress']['postalCode'] : NULL, true); add_user_meta($user_id, 'shipping_country', isset($order['deliveryAddress']['country']) ? $order['deliveryAddress']['country'] : NULL, true); add_user_meta($user_id, 'shipping_state', isset($order['deliveryAddress']['state']) ? $order['deliveryAddress']['state'] : NULL, true); } wp_mail($user_email, 'Your username and password', $message); $user = new WP_User($user_id); $user->set_role('customer'); } add_post_meta($order_id, '_customer_user', $user_id); } } if (isset($order['total']) && !empty($order['total'])) { if (isset($order['total_tax'])) { $order['total'] = $order['total_tax'] + $order['total']; } add_post_meta($order_id, '_order_total', $order['total'], true); } if (isset($order['taxes_included']) && $order['taxes_included'] == true) { add_post_meta($order_id, '_order_tax', $order['total_tax'], true); } if (isset($order['updated_at']) && !empty($order['updated_at'])) { add_post_meta($order_id, '_completed_date', $order['updated_at'], true); } if (isset($order['id']) && !empty($order['id'])) { add_post_meta($order_id, '_vend_orderid', $order['id'], true); } if (isset($order['currency']) && !empty($order['currency'])) { add_post_meta($order_id, '_order_currency', $order['currency'], true); } // billing info if (isset($order['user_name']) && !empty($order['user_name'])) { add_post_meta($order_id, '_billing_email', $order['user_name'], true); } $i = 0; foreach ($order['products'] as $products) { $product_id = $this->isReferenceExists_order($products['sku']); if ($product_id['result'] == 'success' && !empty($product_id['data'])) { $product = new WC_Product($product_id['data']); if ($product->post->post_type == 'product_variation') { $variant_id = $product->id; $product->id = $product->post->post_parent; } $wcproduct = $product->post; if ($product) { // add item $item_id = wc_add_order_item($order_id, array('order_item_name' => $wcproduct->post_title, 'order_item_type' => 'line_item')); if ($item_id) { $line_tax = array(); $line_subtax = array(); // add item meta data if (isset($products['price']) && !empty($products['price'])) { $products['price'] = (double) ($products['price'] * $products['quantity']); } $line_total = (double) $products['price']; wc_add_order_item_meta($item_id, '_qty', $products['quantity']); //Product Order Quantity From Vend wc_add_order_item_meta($item_id, '_product_id', $product->id); wc_add_order_item_meta($item_id, '_line_total', $line_total); wc_add_order_item_meta($item_id, '_variation_id', isset($variant_id) ? $variant_id : ''); $result_tax_class = $this->linksync_tax_classes_vend_to_wc($products['taxId']); if ($result_tax_class['result'] == 'success') { $tax_class = $result_tax_class['tax_class']; } wc_add_order_item_meta($item_id, '_tax_class', isset($tax_class) ? $tax_class : ''); wc_add_order_item_meta($item_id, '_line_tax', $products['taxValue']); wc_add_order_item_meta($item_id, '_line_subtotal', $products['price']); wc_add_order_item_meta($item_id, '_line_subtotal_tax', $products['taxValue']); $line_tax['total'][1] = $products['taxValue']; $line_subtax['subtotal'][1] = $products['taxValue']; $line_tax_data = array_merge($line_tax, $line_subtax); wc_add_order_item_meta($item_id, '_line_tax_data', $line_tax_data); if (isset($variant_id) && !empty($variant_id)) { global $wpdb; $query = mysql_query("SELECT meta_key,meta_value FROM `" . $wpdb->prefix . "postmeta` WHERE post_id='" . $variant_id . "' AND meta_key LIKE 'attribute_pa_%'"); while ($result = mysql_fetch_assoc($query)) { $meta_key = str_replace('attribute_', '', $result['meta_key']); wc_add_order_item_meta($item_id, $meta_key, $result['meta_value']); } } } } else { $order->errors = 'Product SKU (' . $order->{$item_id} . ') not found.'; } } elseif ($products['sku'] == 'shipping') { $taxes = array(); // add item $shipping_id = wc_add_order_item($order_id, array('order_item_name' => $products['title'], 'order_item_type' => 'shipping')); if ($shipping_id) { wc_add_order_item_meta($shipping_id, 'cost', $products['price']); wc_add_order_item_meta($shipping_id, 'method_id', ''); wc_add_order_item_meta($shipping_id, 'taxes', ''); add_post_meta($order_id, '_order_shipping', $products['price']); add_post_meta($order_id, '_order_shipping_tax', $products['taxValue']); $shippping_tax_amount = $products['taxValue']; $taxes[1] = $products['taxValue']; wc_add_order_item_meta($shipping_id, 'taxes', $taxes); } } elseif ($products['sku'] == 'vend-discount') { add_post_meta($order_id, '_cart_discount', $products['price']); } /* ---------------------------------------Tax Mapping --------------------------------- */ if ($products['sku'] != 'shipping' || $products['sku'] != 'vend-discount') { if ($i == 0) { $tax_class_name = $this->linksync_tax_classes_vend_to_wc($products['taxId']); if ($tax_class_name['result'] == 'success') { // add item $tax_id = wc_add_order_item($order_id, array('order_item_name' => $tax_class_name['tax_class_name'] . '-' . $tax_class_name['tax_rate_id'], 'order_item_type' => 'tax')); if ($tax_id) { wc_add_order_item_meta($tax_id, 'rate_id', $tax_class_name['tax_rate_id']); wc_add_order_item_meta($tax_id, 'label', $tax_class_name['tax_class_name']); wc_add_order_item_meta($tax_id, 'compound', 0); $tax_amount = $order['total_tax']; wc_add_order_item_meta($tax_id, 'tax_amount', isset($tax_amount) ? $tax_amount : 0); wc_add_order_item_meta($tax_id, 'shipping_tax_amount', isset($shippping_tax_amount) ? $shippping_tax_amount : 0); } } $i++; } } } } linksync_class::add('Order Sync Vend to Woo', 'success', 'Vend Order no:' . $order['orderId'] . ', Woo Order no:' . $order_id, get_option('linksync_laid')); } } } } return true; }
public static function checkForConnection($api_key) { global $wpdb; // Start - Saving API Key and Connecting to Server # On Save button clicking , it should be saved and connected as well. $LAIDKey = trim($api_key); $testMode = get_option('linksync_test'); if (isset($testMode) && $testMode == 'on') { $testMode = 'on'; } else { $testMode = 'off'; } update_option('linksync_test', $testMode); $apicall = new linksync_class($LAIDKey, $testMode); $result = $apicall->testConnection(); if (isset($result) && !empty($result)) { if (isset($result['errorCode']) && !empty($result['userMessage'])) { mysql_query("UPDATE `" . $wpdb->prefix . "linksync_laidKey` SET status='Invalid' WHERE api_key='{$LAIDKey}'"); update_option('linksync_status', "Inactive"); update_option('linksync_last_test_time', current_time('mysql')); update_option('linksync_connected_url', ""); update_option('linksync_connectedto', ''); update_option('linksync_connectionwith', ''); update_option('linksync_addedfile', ''); update_option('linksync_frequency', $result['userMessage']); linksync_class::add('checkAPI Key', 'Fail', $result['userMessage'], $LAIDKey); $class1 = 'updated'; $class2 = 'error'; $response['error'] = 'Connection Not Established because of ' . $result['userMessage']; } else { if (isset($result['app']) && !empty($result['app'])) { $app_name = self::appid_app($result['app']); if (isset($app_name) && !empty($app_name['success'])) { update_option('linksync_connectionwith', $app_name['success']); $app_name_status = 'Active'; } else { update_option('linksync_connectionwith', 'Supplied API Key not valid'); $checkKey = 'Supplied API Key not valid'; $app_name_status = 'Inactive'; } } if (isset($result['connected_app']) && !empty($result['connected_app'])) { $connected_app = self::appid_app($result['connected_app']); if (isset($connected_app) && !empty($connected_app['success'])) { update_option('linksync_connectedto', $connected_app['success']); $status = 'Active'; } else { update_option('linksync_connectedto', "The supplied API Key is not valid for use with linksync for WooCommerce."); $checkKey = 'Supplied API Key not valid for use with WooCommerce'; $status = 'Inactive'; } } if (isset($status) && isset($app_name_status) && $status == 'Active' && $app_name_status == 'Active') { // if (isset($result['connected_app_version']) && !empty($result['connected_app_version'])) {woocommerce/woocommerce.php $plugin_file = dirname(__FILE__) . '/linksync.php'; $plugin_data = get_plugin_data($plugin_file, $markup = true, $translate = true); $linksync_version = $plugin_data['Version']; update_option('linksync_version', $linksync_version); $webhook = $apicall->webhookConnection(content_url() . '/plugins/linksync/update.php?c=' . get_option('webhook_url_code'), $linksync_version, 'no'); if (isset($webhook) && !empty($webhook)) { if (isset($webhook['result']) && $webhook['result'] == 'success') { linksync_class::add('WebHookConnection', 'success', 'Connected to a file ' . content_url() . '/plugins/linksync/update.php?c=' . get_option('webhook_url_code'), $LAIDKey); update_option('linksync_addedfile', '<a href="' . content_url() . '/plugins/linksync/update.php?c=' . get_option('webhook_url_code') . '">' . content_url() . '/linksync/update.php?c=' . get_option('webhook_url_code') . '</a>'); } } //} if (isset($result['time']) && !empty($result['time'])) { $server_response = strtotime($result['time']); $server_time = time(); $time = $server_response - $server_time; linksync_class::add('Time Offset', 'success', 'Server Response:' . $result['time'] . ' Current Server Time:' . date("Y-m-d H:i:s") . ' Time Offset: ' . date("H:i:s", abs($time)), $LAIDKey); update_option('linksync_time_offset', $time); } if (get_option('linksync_connectionwith') == 'Vend' || get_option('linksync_connectedto') == 'Vend') { // Add Default setting into DB $response_outlets = $apicall->linksync_getOutlets(); if (@get_option('ps_outlet') == 'on') { #VEND TO WC if (isset($response_outlets) && !empty($response_outlets)) { if (isset($response_outlets['errorCode']) && !empty($response_outlets['userMessage'])) { update_option('ps_outlet_details', 'off'); $response_ = $response_outlets['userMessage']; linksync_class::add('linksync_getOutlets', 'fail', $response_, $LAIDKey); } else { foreach ($response_outlets['outlets'] as $key => $value) { $oulets["{$key}"] = $value['id']; } $ouletsdb = implode('|', $oulets); update_option('ps_outlet_details', $ouletsdb); update_option('ps_outlet', 'on'); } } else { $class2 = 'updated'; $class1 = 'error'; $response_ = 'Error in getting outlets'; echo "<br>"; echo "<span style='color:red';>" . $response_ . "</span>"; echo "<br>"; } } if (@get_option('ps_wc_to_vend_outlet') == 'on') { if (isset($response_outlets['errorCode']) && !empty($response_outlets['userMessage'])) { update_option('wc_to_vend_outlet_detail', 'off'); $response_ = $response_outlets['userMessage']; linksync_class::add('linksync_getOutlets', 'fail', $response_, $LAIDKey); } else { $two_way_wc_to_vend = $response_outlets['outlets'][0]['name'] . '|' . $response_outlets['outlets'][0]['id']; update_option('wc_to_vend_outlet_detail', $two_way_wc_to_vend); update_option('ps_wc_to_vend_outlet', 'on'); } } /* * display_retail_price_tax_inclusive(0 or 1) */ $vend_config = $apicall->getVendConfig(); if (isset($vend_config) && !empty($vend_config)) { if (!isset($vend_config['errorCode'])) { update_option('linksync_tax_inclusive', $vend_config['display_retail_price_tax_inclusive']); } else { update_option('linksync_tax_inclusive', ''); echo "<span style='color:red;font-weight:bold;'>Error in getting VEND Config : {$vend_config['userMessage']}</span><br>"; } } } mysql_query("UPDATE `" . $wpdb->prefix . "linksync_laidKey` SET status='connected' WHERE api_key='{$LAIDKey}'"); update_option('linksync_status', 'Active'); update_option('linksync_last_test_time', current_time('mysql')); update_option('linksync_connected_url', get_option('linksync_connected_url')); update_option('linksync_frequency', 'Valid API Key'); update_option('laid_message', isset($result['message']) ? $result['message'] : null); linksync_class::add('isConnected', 'success', 'Connected URL is ' . get_option('linksync_connected_url'), $LAIDKey); $class2 = 'updated'; $class1 = 'error'; update_option('linksync_laid', $LAIDKey); $response['success'] = 'Connection is established Successfully!!'; } else { mysql_query("UPDATE `" . $wpdb->prefix . "linksync_laidKey` SET status='Invalid' WHERE api_key='{$LAIDKey}'"); update_option('linksync_status', "Inactive"); update_option('linksync_last_test_time', current_time('mysql')); update_option('linksync_connected_url', get_option('linksync_connected_url')); update_option('linksync_addedfile', ''); update_option('linksync_connectedto', "The supplied API Key is not valid for use with linksync for WooCommerce."); update_option('linksync_connectionwith', 'Supplied API Key not valid'); update_option('linksync_frequency', 'Invalid API Key'); linksync_class::add('checkAPI Key', 'fail', 'Invalid API Key', '-'); $class1 = 'updated'; $class2 = 'error'; $response['error'] = "The supplied API Key is not valid for use with linksync for WooCommerce."; } } } else { mysql_query("UPDATE `" . $wpdb->prefix . "linksync_laidKey` SET status='Invalid' WHERE api_key='{$LAIDKey}'"); update_option('linksync_status', "Inactive"); update_option('linksync_last_test_time', current_time('mysql')); update_option('linksync_connected_url', get_option('linksync_connected_url')); update_option('linksync_addedfile', ''); update_option('linksync_connectedto', "The supplied API Key is not valid for use with linksync for WooCommerce."); update_option('linksync_connectionwith', 'Supplied API Key not valid'); update_option('linksync_frequency', 'Invalid API Key'); linksync_class::add('checkAPI Key', 'fail', 'Invalid API Key', '-'); $class1 = 'updated'; $class2 = 'error'; $response['error'] = "The supplied API Key is not valid for use with linksync for WooCommerce."; } return $response; }