first commit
This commit is contained in:
681
hrc-webcommerce.php
Normal file
681
hrc-webcommerce.php
Normal file
@@ -0,0 +1,681 @@
|
||||
<?php
|
||||
/**
|
||||
* Plugin Name: HRC WebCommerce
|
||||
* Plugin URI: https://hrc.by/
|
||||
* Description: Integrate HRC system into HRC WebCommerce
|
||||
* Version: 1.0.0
|
||||
* Author: HRC Team
|
||||
* Author URI: https://hrc.by
|
||||
* Requires at least: 5.1
|
||||
* Tested up to: 5.2
|
||||
* Text Domain: hrc-webcommerce
|
||||
*
|
||||
* @package HRC_WebCommerce
|
||||
*/
|
||||
|
||||
add_action('admin_menu', 'hrc_webcommerce_menu');
|
||||
|
||||
function hrc_webcommerce_menu()
|
||||
{
|
||||
add_options_page('HRC WebCommerce settings', 'HRC WebCommerce', 'manage_options', 'hrc_webcommerce', 'hrc_webcommerce_settings');
|
||||
}
|
||||
|
||||
function hrc_webcommerce_settings()
|
||||
{
|
||||
$error = false;
|
||||
|
||||
if (isset($_POST['hrc_webcommerce_table'])) {
|
||||
update_option('hrc_webcommerce_table', $_POST['hrc_webcommerce_table']);
|
||||
}
|
||||
|
||||
if (isset($_FILES['hrc_webcommerce_key'])) {
|
||||
$content = file_get_contents($_FILES["hrc_webcommerce_key"]["tmp_name"]);
|
||||
|
||||
$content = base64_decode($content);
|
||||
$content = json_decode($content, true);
|
||||
|
||||
if (isset($content['ip']) && isset($content['token'])) {
|
||||
update_option('hrc_webcommerce_token', $content['token']);
|
||||
update_option('hrc_webcommerce_ip', $content['ip']);
|
||||
} else {
|
||||
$error = 'wrong_file';
|
||||
}
|
||||
}
|
||||
|
||||
$token = get_option('hrc_webcommerce_token', '');
|
||||
$ip = get_option('hrc_webcommerce_ip', '');
|
||||
$table = get_option('hrc_webcommerce_table', 0);
|
||||
|
||||
$is_key = !empty($token) && !empty($ip);
|
||||
|
||||
if ($is_key) {
|
||||
$answer = Requests::post('http://' . $ip . '/api/licence/check', [], [
|
||||
'unn' => $token,
|
||||
'key' => $_SERVER['HTTP_HOST'],
|
||||
'work_code' => '0',
|
||||
'work_group' => '0',
|
||||
'soft' => 'HRC WebCommerce'
|
||||
]);
|
||||
|
||||
$is_licence = false;
|
||||
|
||||
if ($answer !== false) {
|
||||
$data = json_decode($answer->body, true);
|
||||
if ($data['status'] == 'success') {
|
||||
$is_licence = true;
|
||||
update_option('hrc_webcommerce_terminal_token', $data['token']);
|
||||
|
||||
if (!wp_next_scheduled('hrc_webcommerce_hourly_event')) {
|
||||
wp_schedule_event(time(), 'hourly', 'hrc_webcommerce_hourly_event');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$last_update = get_option('hrc_webcommerce_last_update', '');
|
||||
|
||||
include plugin_dir_path(__FILE__) . 'templates/settings.php';
|
||||
}
|
||||
|
||||
add_action('hrc_webcommerce_hourly_event', 'hrc_webcommerce_hourly');
|
||||
|
||||
function hrc_webcommerce_hourly()
|
||||
{
|
||||
$token = get_option('hrc_webcommerce_terminal_token', '');
|
||||
$ip = get_option('hrc_webcommerce_ip', '');
|
||||
|
||||
$answer = Requests::get('http://' . $ip . '/api/sync/hello?token=' . $token);
|
||||
|
||||
if ($answer !== false) {
|
||||
$data = json_decode($answer->body, true);
|
||||
if ($data['status'] == 'success') {
|
||||
foreach ($data['methods'] as $method) {
|
||||
if ($method['name'] == 'generatemenu') {
|
||||
$answer = Requests::get('http://' . $ip . '/api/menu/generate?token=' . $token . '&format=json', [], [
|
||||
'timeout' => 600
|
||||
]);
|
||||
}
|
||||
|
||||
if ($method['name'] == 'getmenu') {
|
||||
$answer = Requests::get('http://' . $ip . '/api/download/menu?token=' . $token, [], [
|
||||
'timeout' => 600
|
||||
]);
|
||||
|
||||
$tmp_path = wp_upload_dir()['basedir'] . '/menu';
|
||||
if (!file_exists($tmp_path)) {
|
||||
mkdir($tmp_path);
|
||||
}
|
||||
|
||||
file_put_contents($tmp_path . '/menu.zip', $answer->body);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function file_get_contents_utf8($fn)
|
||||
{
|
||||
$content = file_get_contents($fn);
|
||||
return mb_convert_encoding($content, 'UTF-8', 'CP1251');
|
||||
}
|
||||
|
||||
function wc_get_product_by_sku($sku)
|
||||
{
|
||||
global $wpdb;
|
||||
|
||||
if (defined('ICL_LANGUAGE_CODE')) {
|
||||
$product_id = $wpdb->get_var($wpdb->prepare("SELECT pm.post_id FROM " . $wpdb->postmeta . " AS pm LEFT JOIN " . $wpdb->prefix . "icl_translations AS tr ON pm.post_id = tr.element_id WHERE pm.meta_key='_sku' AND pm.meta_value='%s' AND tr.language_code = '" . ICL_LANGUAGE_CODE . "'", $sku));
|
||||
} else {
|
||||
$product_id = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku));
|
||||
}
|
||||
|
||||
if ($product_id) {
|
||||
return new WC_Product($product_id);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function getToken($length)
|
||||
{
|
||||
$token = "";
|
||||
$codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
$codeAlphabet .= "abcdefghijklmnopqrstuvwxyz";
|
||||
$codeAlphabet .= "0123456789";
|
||||
$max = strlen($codeAlphabet);
|
||||
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
$token .= $codeAlphabet[random_int(0, $max - 1)];
|
||||
}
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
add_action('wp_ajax_hrc_webcommerce_update', 'hrc_webcommerce_update_callback');
|
||||
function hrc_webcommerce_update_callback()
|
||||
{
|
||||
$tmp_path = wp_upload_dir()['basedir'] . '/menu';
|
||||
if (!file_exists($tmp_path)) {
|
||||
mkdir($tmp_path);
|
||||
}
|
||||
|
||||
$step = $_POST['step'] ?? false;
|
||||
|
||||
if ($step == 'check') {
|
||||
hrc_webcommerce_hourly();
|
||||
$last_update = intval(get_option('hrc_webcommerce_last_update', 0));
|
||||
|
||||
echo json_encode([
|
||||
'last_update' => $last_update,
|
||||
'file_version' => !file_exists($tmp_path . '/menu.zip') ? 0 : (filemtime($tmp_path . '/menu.zip'))
|
||||
]);
|
||||
}
|
||||
|
||||
if ($step == 'update') {
|
||||
$file_menu = $tmp_path . '/menu.zip';
|
||||
$menu_dir = $tmp_path . '/menu';
|
||||
|
||||
if (!file_exists($menu_dir)) {
|
||||
mkdir($menu_dir);
|
||||
}
|
||||
|
||||
$zipArchive = new ZipArchive();
|
||||
$result = $zipArchive->open($file_menu);
|
||||
if ($result === TRUE) {
|
||||
$zipArchive->extractTo($menu_dir);
|
||||
$zipArchive->close();
|
||||
}
|
||||
|
||||
echo json_encode([
|
||||
'file_exist' => file_exists($menu_dir . '/Menu.json')
|
||||
]);
|
||||
}
|
||||
|
||||
if ($step == 'folder') {
|
||||
$menu_dir = $tmp_path . '/menu';
|
||||
$file_result = $menu_dir . '/Menu.json';
|
||||
$data = json_decode(file_get_contents_utf8($file_result), true);
|
||||
|
||||
$folders_count = 0;
|
||||
foreach ($data['Folders'] as $item) {
|
||||
$category = get_term_by('slug', $item['Code'], 'product_cat');
|
||||
|
||||
if ($item['CodeParent'] == 0) {
|
||||
$parent_id = 0;
|
||||
} else {
|
||||
$parent_category = get_term_by('slug', $item['CodeParent'], 'product_cat');
|
||||
|
||||
if ($parent_category !== false) {
|
||||
$parent_id = $parent_category->term_id;
|
||||
} else {
|
||||
$parent_id = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if ($category === false) {
|
||||
wp_insert_term($item['Name'], 'product_cat', array(
|
||||
'parent' => $parent_id,
|
||||
'slug' => $item['Code']
|
||||
));
|
||||
} else {
|
||||
wp_update_term($category->term_id, 'product_cat', [
|
||||
'name' => $item['Name'],
|
||||
'parent' => $parent_id
|
||||
]);
|
||||
}
|
||||
|
||||
$folders_count++;
|
||||
}
|
||||
|
||||
echo json_encode([
|
||||
'folder_count' => $folders_count,
|
||||
'menu_total' => count($data['Menu'])
|
||||
]);
|
||||
}
|
||||
|
||||
if ($step == 'menu') {
|
||||
$key = $_POST['key'] ?? 0;
|
||||
|
||||
$menu_dir = $tmp_path . '/menu';
|
||||
$file_result = $menu_dir . '/Menu.json';
|
||||
$data = json_decode(file_get_contents_utf8($file_result), true);
|
||||
|
||||
$border_color = '#d7d7d7';
|
||||
$text_color = '#333333';
|
||||
|
||||
$unit_text = 'Размер';
|
||||
$unit_text_size = '14';
|
||||
$unit_text_weight = '600';
|
||||
|
||||
$modificators_text = 'Добавки';
|
||||
$modificators_text_size = '14';
|
||||
$modificators_text_weight = '600';
|
||||
|
||||
global $wpdb;
|
||||
|
||||
$wpdb->query("DELETE FROM wp_options where option_name like '%builder_css_cached_for%'");
|
||||
$wpdb->query("DELETE FROM wp_2_options where option_name like '%builder_css_cached_for%'");
|
||||
$wpdb->query("DELETE FROM wp_3_options where option_name like '%builder_css_cached_for%'");
|
||||
|
||||
$item = $data['Menu'][$key];
|
||||
$name = $item['Name'];
|
||||
|
||||
$post = wc_get_product_by_sku($item['Code']);
|
||||
|
||||
$units_general_cpo = include 'cpo/units-general.php';
|
||||
$units_style_cpo = include 'cpo/units-style.php';
|
||||
$units_rules_cpo = include 'cpo/units-rules.php';
|
||||
$units_validation_cpo = include 'cpo/units-validation.php';
|
||||
$units_conditional_cpo = include 'cpo/units-conditional.php';
|
||||
$units_general_advanced_cpo = include 'cpo/units-general-advanced.php';
|
||||
$units_advanced_cpo = include 'cpo/units-advanced.php';
|
||||
|
||||
$modificators_general_cpo = include 'cpo/modificators-general.php';
|
||||
$modificators_style_cpo = include 'cpo/modificators-style.php';
|
||||
$modificators_rules_cpo = include 'cpo/modificators-rules.php';
|
||||
$modificators_validation_cpo = include 'cpo/modificators-validation.php';
|
||||
$modificators_conditional_cpo = include 'cpo/modificators-conditional.php';
|
||||
$modificators_general_advanced_cpo = include 'cpo/modificators-general-advanced.php';
|
||||
$modificators_advanced_cpo = include 'cpo/modificators-advanced.php';
|
||||
|
||||
if ($post === false) {
|
||||
$post = array(
|
||||
'post_author' => get_current_user_id(),
|
||||
'post_content' => '',
|
||||
'post_status' => 'publish',
|
||||
'post_title' => $item['Name'],
|
||||
'post_parent' => '',
|
||||
'post_type' => 'product',
|
||||
'post_excerpt' => $item['Description']
|
||||
);
|
||||
|
||||
$post_id = wp_insert_post($post);
|
||||
} else {
|
||||
wp_update_post([
|
||||
'ID' => $post->get_id(),
|
||||
'post_title' => $item['Name'],
|
||||
'post_excerpt' => $item['Description']
|
||||
]);
|
||||
|
||||
$post_id = $post->get_id();
|
||||
}
|
||||
|
||||
if (count($item['Units']) > 0) {
|
||||
$post_info = get_page_by_title('uni_cpo_unit_dishes_' . $item['Code'], OBJECT, 'uni_cpo_option');
|
||||
|
||||
if (!isset($post_info->ID)) {
|
||||
$post_unit_dishes = array(
|
||||
'post_author' => get_current_user_id(),
|
||||
'post_content' => '',
|
||||
'post_status' => 'publish',
|
||||
'post_title' => 'uni_cpo_unit_dishes_' . $item['Code'],
|
||||
'post_parent' => '',
|
||||
'post_type' => 'uni_cpo_option',
|
||||
);
|
||||
|
||||
$post_unit_dishes_id = wp_insert_post($post_unit_dishes);
|
||||
$post_unit_dishes_slug = 'uni_cpo_unit_dishes_' . $item['Code'];
|
||||
} else {
|
||||
$post_unit_dishes_id = $post_info->ID;
|
||||
$post_unit_dishes_slug = 'uni_cpo_unit_dishes_' . $item['Code'];
|
||||
}
|
||||
}
|
||||
|
||||
if (count($item['Modifiers']) > 0) {
|
||||
$post_info = get_page_by_title('uni_cpo_modificators_' . $item['Code'], OBJECT, 'uni_cpo_option');
|
||||
|
||||
if (!isset($post_info->ID)) {
|
||||
$post_modificators = array(
|
||||
'post_author' => get_current_user_id(),
|
||||
'post_content' => '',
|
||||
'post_status' => 'publish',
|
||||
'post_title' => 'uni_cpo_modificators_' . $item['Code'],
|
||||
'post_parent' => '',
|
||||
'post_type' => 'uni_cpo_option',
|
||||
);
|
||||
|
||||
$post_modificators_id = wp_insert_post($post_modificators);
|
||||
$post_modificators_slug = 'uni_cpo_modificators_' . $item['Code'];
|
||||
} else {
|
||||
$post_modificators_id = $post_info->ID;
|
||||
$post_modificators_slug = 'uni_cpo_modificators_' . $item['Code'];
|
||||
}
|
||||
}
|
||||
|
||||
$modificators_items = [];
|
||||
foreach ($item['Modifiers'] as $item_modifier) {
|
||||
foreach ($data['Modifiers'] as $modifier) {
|
||||
if ($item_modifier['Code'] == $modifier['Code']) {
|
||||
$modificators_items[] = [
|
||||
'label' => $modifier['Name'],
|
||||
'slug' => 'modificator_' . $modifier['Code'],
|
||||
'rate' => $modifier['Price'],
|
||||
'suboption_class' => '',
|
||||
'suboption_text' => '',
|
||||
'suboption_colour' => '',
|
||||
'attach_id' => '',
|
||||
'attach_uri' => '',
|
||||
'attach_name' => '',
|
||||
'attach_id_r' => '',
|
||||
'attach_uri_r' => '',
|
||||
];
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (count($modificators_items) > 0) {
|
||||
update_post_meta($post_modificators_id, '_module_type', 'checkbox');
|
||||
update_post_meta($post_modificators_id, '_option_version', '4.7.7');
|
||||
update_post_meta($post_modificators_id, '_general', $modificators_general_cpo);
|
||||
update_post_meta($post_modificators_id, '_style', $modificators_style_cpo);
|
||||
update_post_meta($post_modificators_id, '_cpo_rules', $modificators_rules_cpo);
|
||||
update_post_meta($post_modificators_id, '_cpo_validation', $modificators_validation_cpo);
|
||||
update_post_meta($post_modificators_id, '_cpo_conditional', $modificators_conditional_cpo);
|
||||
update_post_meta($post_modificators_id, '_cpo_general', $modificators_general_advanced_cpo);
|
||||
update_post_meta($post_modificators_id, '_advanced', $modificators_advanced_cpo);
|
||||
update_post_meta($post_modificators_id, '_cpo_suboptions', [
|
||||
'data' => [
|
||||
'cpo_radio_options' => $modificators_items
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
$unit_dishes_items = [];
|
||||
foreach ($item['Units'] as $item_unit) {
|
||||
foreach ($data['Units'] as $unit) {
|
||||
if ($unit['Code'] == $item_unit['Code']) {
|
||||
$unit_dishes_items[] = [
|
||||
'def' => count($unit_dishes_items) == 0 ? 'checked' : '',
|
||||
'label' => $unit['Name'],
|
||||
'slug' => 'unit_' . $unit['Code'],
|
||||
'rate' => $unit['Coefficient'],
|
||||
'suboption_class' => '',
|
||||
'suboption_text' => '',
|
||||
'suboption_colour' => '',
|
||||
'attach_id' => '',
|
||||
'attach_uri' => '',
|
||||
'attach_name' => '',
|
||||
'attach_id_r' => '',
|
||||
'attach_uri_r' => '',
|
||||
];
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (count($unit_dishes_items) > 0) {
|
||||
update_post_meta($post_unit_dishes_id, '_module_type', 'radio');
|
||||
update_post_meta($post_unit_dishes_id, '_option_version', '4.7.7');
|
||||
update_post_meta($post_unit_dishes_id, '_general', $units_general_cpo);
|
||||
update_post_meta($post_unit_dishes_id, '_style', $units_style_cpo);
|
||||
update_post_meta($post_unit_dishes_id, '_cpo_rules', $units_rules_cpo);
|
||||
update_post_meta($post_unit_dishes_id, '_cpo_validation', $units_validation_cpo);
|
||||
update_post_meta($post_unit_dishes_id, '_cpo_conditional', $units_conditional_cpo);
|
||||
update_post_meta($post_unit_dishes_id, '_cpo_general', $units_general_advanced_cpo);
|
||||
update_post_meta($post_unit_dishes_id, '_advanced', $units_advanced_cpo);
|
||||
update_post_meta($post_unit_dishes_id, '_cpo_suboptions', [
|
||||
'data' => [
|
||||
'cpo_radio_options' => $unit_dishes_items
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
$options = include 'cpo/default.php';
|
||||
|
||||
update_post_meta($post_id, '_visibility', 'visible');
|
||||
update_post_meta($post_id, '_stock_status', 'instock');
|
||||
update_post_meta($post_id, 'total_sales', '0');
|
||||
update_post_meta($post_id, '_regular_price', $item['Price']);
|
||||
update_post_meta($post_id, '_sale_price', $item['Price']);
|
||||
update_post_meta($post_id, '_sku', $item['Code']);
|
||||
update_post_meta($post_id, '_price', $item['Price']);
|
||||
|
||||
update_post_meta($post_id, '_cpo_starting_price', '');
|
||||
update_post_meta($post_id, '_cpo_sold_individually', 'off');
|
||||
update_post_meta($post_id, '_cpo_silent_validation_on', 'off');
|
||||
update_post_meta($post_id, '_cpo_reset_form_btn', 'off');
|
||||
update_post_meta($post_id, '_cpo_qty_field', 'wc');
|
||||
update_post_meta($post_id, '_cpo_price_suffix', '');
|
||||
update_post_meta($post_id, '_cpo_price_postfix', '');
|
||||
update_post_meta($post_id, '_cpo_price_disabled_msg', '');
|
||||
update_post_meta($post_id, '_cpo_price_archives_sale', '');
|
||||
update_post_meta($post_id, '_cpo_price_archives', '');
|
||||
update_post_meta($post_id, '_cpo_min_price', '');
|
||||
update_post_meta($post_id, '_cpo_max_price', '');
|
||||
update_post_meta($post_id, '_cpo_main_formula', '{uni_cpo_price}*{' . $post_unit_dishes_slug . '}+{' . $post_modificators_slug . '}');
|
||||
update_post_meta($post_id, '_cpo_layered_image_enable', 'off');
|
||||
update_post_meta($post_id, '_cpo_imagify_enable', 'off');
|
||||
update_post_meta($post_id, '_cpo_imagify_base_image', '');
|
||||
update_post_meta($post_id, '_cpo_formula_scheme', '');
|
||||
update_post_meta($post_id, '_cpo_formula_rules_enable', 'off');
|
||||
update_post_meta($post_id, '_cpo_enable', 'on');
|
||||
update_post_meta($post_id, '_cpo_content', base64_encode(maybe_serialize($options)));
|
||||
update_post_meta($post_id, '_cpo_calc_enable', 'on');
|
||||
update_post_meta($post_id, '_cpo_calc_btn_enable', 'off');
|
||||
|
||||
if ($item['CodeParent'] != 0) {
|
||||
$parent_category = get_term_by('slug', $item['CodeParent'], 'product_cat');
|
||||
if ($parent_category !== false) {
|
||||
wp_set_object_terms($post_id, $parent_category->term_id, 'product_cat');
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($item['ImageName'])) {
|
||||
$image_url = $tmp_path . '/menu/' . $item['ImageName'];
|
||||
$upload_dir = wp_upload_dir();
|
||||
|
||||
$image_data = file_get_contents($image_url);
|
||||
$filename = basename($image_url);
|
||||
|
||||
if (wp_mkdir_p($upload_dir['path'])) {
|
||||
$file = $upload_dir['path'] . '/' . $filename;
|
||||
} else {
|
||||
$file = $upload_dir['basedir'] . '/' . $filename;
|
||||
}
|
||||
|
||||
file_put_contents($file, $image_data);
|
||||
|
||||
$wp_filetype = wp_check_filetype($filename, null);
|
||||
|
||||
$attachment = array(
|
||||
'post_mime_type' => $wp_filetype['type'],
|
||||
'post_title' => sanitize_file_name($filename),
|
||||
'post_content' => '',
|
||||
'post_status' => 'inherit'
|
||||
);
|
||||
|
||||
$attach_id = wp_insert_attachment($attachment, $file);
|
||||
require_once(ABSPATH . 'wp-admin/includes/image.php');
|
||||
$attach_data = wp_generate_attachment_metadata($attach_id, $file);
|
||||
wp_update_attachment_metadata($attach_id, $attach_data);
|
||||
|
||||
update_post_meta($post_id, '_thumbnail_id', $attach_id);
|
||||
}
|
||||
|
||||
update_option('hrc_webcommerce_last_update', time());
|
||||
|
||||
echo json_encode([
|
||||
'name' => $name,
|
||||
'is_last' => (count($data['Menu']) - 1) == $key
|
||||
]);
|
||||
}
|
||||
|
||||
wp_die();
|
||||
}
|
||||
|
||||
function extract_meta($items, $name, $default = '')
|
||||
{
|
||||
foreach ($items as $meta_item) {
|
||||
$item = $meta_item->get_data();
|
||||
|
||||
if ($item['key'] == $name) {
|
||||
return $item['value'];
|
||||
}
|
||||
}
|
||||
|
||||
return $default;
|
||||
}
|
||||
|
||||
add_action('woocommerce_thankyou', 'hrc_webcommerce_order');
|
||||
function hrc_webcommerce_order($order_id)
|
||||
{
|
||||
$order = wc_get_order($order_id);
|
||||
$ip = get_option('hrc_webcommerce_ip', '');
|
||||
$code = get_option('hrc_webcommerce_token', '');
|
||||
$token = get_option('hrc_webcommerce_terminal_token', '');
|
||||
$table = get_option('hrc_webcommerce_table', 0);
|
||||
|
||||
$user = $order->get_user();
|
||||
$info = $order->get_data();
|
||||
$items = $order->get_items();
|
||||
|
||||
if ($info['total'] == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$order_items = [];
|
||||
$positions = [];
|
||||
foreach ($items as $item) {
|
||||
$data = $item->get_data();
|
||||
$product = $item->get_product();
|
||||
|
||||
$unit_id = extract_meta($data['meta_data'], '_uni_cpo_unit_dishes_' . $product->get_sku(), 0);
|
||||
$modificators = extract_meta($data['meta_data'], '_uni_cpo_modificators_' . $product->get_sku(), []);
|
||||
|
||||
if (!is_array($modificators)) {
|
||||
$modificators = [$modificators];
|
||||
}
|
||||
|
||||
$unit_id = str_replace('unit_', '', $unit_id);
|
||||
|
||||
foreach ($modificators as $key => $value) {
|
||||
$modificators[$key] = [
|
||||
'code' => str_replace('modificator_', '', $value)
|
||||
];
|
||||
}
|
||||
|
||||
$order_items[] = [
|
||||
'id' => $data['id'],
|
||||
'remote_id' => $product->get_sku(),
|
||||
'count' => $data['quantity'],
|
||||
'price' => $product->get_price(),
|
||||
'sum_price' => $data['total'],
|
||||
'full_price' => $data['subtotal'],
|
||||
'sale_price' => $data['total'],
|
||||
'name' => $data['name'],
|
||||
'discount' => 0,
|
||||
'unit_id' => $unit_id,
|
||||
'modifiers' => $modificators
|
||||
];
|
||||
|
||||
$positions[] = [
|
||||
'code' => $product->get_sku(),
|
||||
'count' => $data['quantity']
|
||||
];
|
||||
}
|
||||
|
||||
if ($info['customer_id'] == 0) {
|
||||
$site_url = parse_url(get_site_url())['host'];
|
||||
|
||||
preg_match_all("/([0-9]+)/", extract_meta($info['meta_data'], '_shipping_phone_hrc'), $matches);
|
||||
$phone = '';
|
||||
|
||||
foreach ($matches[1] as $number) {
|
||||
$phone .= $number;
|
||||
}
|
||||
|
||||
$user = get_user_by('email', $phone . '@' . $site_url);
|
||||
if ($user == false) {
|
||||
$info['customer_id'] = wc_create_new_customer($phone . '@' . $site_url, $phone . '.' . $site_url, uniqid());
|
||||
} else {
|
||||
$info['customer_id'] = $user->ID;
|
||||
}
|
||||
}
|
||||
|
||||
$data = [
|
||||
'id' => $info['id'],
|
||||
'price' => $info['total'],
|
||||
'delivery_price' => $info['shipping_total'],
|
||||
'discount_price' => $info['discount_total'],
|
||||
'paid' => 0,
|
||||
'currency' => $info['currency'],
|
||||
'is_pay' => $info['date_paid'] !== null,
|
||||
'is_allow_delivery' => false,
|
||||
'table' => $table,
|
||||
'pay_by' => [
|
||||
/* [
|
||||
'id' => payment_id,
|
||||
'name' => payment_name,
|
||||
'sum' => payment_sum,
|
||||
'is_paid' => is_paid,
|
||||
'is_presale' => is_presale
|
||||
] */
|
||||
],
|
||||
'delivery_by' => [
|
||||
/* [
|
||||
'id' => id,
|
||||
'name' => name
|
||||
] */
|
||||
],
|
||||
'positions' => $positions,
|
||||
'items' => $order_items,
|
||||
'client' => [
|
||||
'id' => $info['customer_id'],
|
||||
'name' => extract_meta($info['meta_data'], '_shipping_name_hrc'),
|
||||
'email' => extract_meta($info['meta_data'], '_shipping_email_hrc'),
|
||||
'phone' => formatPhone(extract_meta($info['meta_data'], '_shipping_phone_hrc')),
|
||||
'address' => extract_meta($info['meta_data'], '_shipping_address_1_hrc') . ' ' . extract_meta($info['meta_data'], '_shipping_address_2_hrc'),
|
||||
],
|
||||
'description' => $info['customer_note'],
|
||||
'shipping_method' => $order->get_shipping_method(),
|
||||
'payment_method' => $order->get_payment_method_title()
|
||||
];
|
||||
|
||||
file_put_contents(plugin_dir_path(__FILE__) . 'order.json', json_encode($data));
|
||||
|
||||
Requests::post('http://' . $ip . '/api/import/order', [], [
|
||||
'order' => base64_encode(json_encode($data)),
|
||||
'code' => $code,
|
||||
'token' => $token
|
||||
]);
|
||||
}
|
||||
|
||||
function formatPhone($value)
|
||||
{
|
||||
preg_match_all("/([0-9]+)/", $value, $matches);
|
||||
$phone = '';
|
||||
|
||||
foreach ($matches[1] as $number) {
|
||||
$phone .= $number;
|
||||
}
|
||||
|
||||
if (strlen($phone) == 12) {
|
||||
return '+' . substr($phone, 0, 3) . ' (' . substr($phone, 3, 2) . ') ' . substr($phone, 5, 3) . '-' . substr($phone, 8, 2) . '-' . substr($phone, 10, 2);
|
||||
}
|
||||
|
||||
if (strlen($phone) == 11) {
|
||||
return '+375 (' . substr($phone, 2, 2) . ') ' . substr($phone, 4, 3) . '-' . substr($phone, 7, 2) . '-' . substr($phone, 9, 2);
|
||||
}
|
||||
|
||||
if (strlen($phone) == 9) {
|
||||
return '+375 (' . substr($phone, 0, 2) . ') ' . substr($phone, 2, 3) . '-' . substr($phone, 5, 2) . '-' . substr($phone, 7, 2);
|
||||
}
|
||||
}
|
||||
|
||||
add_filter('woocommerce_checkout_fields', 'remove_billing_fields_from_checkout');
|
||||
function remove_billing_fields_from_checkout($fields)
|
||||
{
|
||||
$fields['billing'] = array();
|
||||
|
||||
$fields['shipping']['phone'] = array(
|
||||
'label' => 'Phone',
|
||||
'required' => false,
|
||||
'class' => array('form-row-wide'),
|
||||
'priority' => 25,
|
||||
);
|
||||
|
||||
return $fields;
|
||||
}
|
||||
Reference in New Issue
Block a user