예제 #1
0
function wc1c_replace_product_variation($guid, $parent_post_id, $order)
{
    $post_id = wc1c_post_id_by_meta('_wc1c_guid', $guid);
    $args = array('menu_order' => $order);
    if (!$post_id) {
        $args = array_merge($args, array('post_type' => 'product_variation', 'post_parent' => $parent_post_id, 'post_title' => "Product #{$parent_post_id} Variation", 'post_status' => 'publish'));
        $post_id = wp_insert_post($args, true);
        wc1c_check_wpdb_error();
        wc1c_check_wp_error($post_id);
        update_post_meta($post_id, '_wc1c_guid', $guid);
        $is_added = true;
    }
    $post = get_post($post_id);
    if (!$post) {
        wc1c_error("Failed to get post");
    }
    if (empty($is_added)) {
        foreach ($args as $key => $value) {
            if ($post->{$key} == $value) {
                continue;
            }
            $is_changed = true;
            break;
        }
        if (!empty($is_changed)) {
            $args = array_merge($args, array('ID' => $post_id));
            $post_id = wp_update_post($args, true);
            wc1c_check_wp_error($post_id);
        }
    }
    return $post_id;
}
예제 #2
0
파일: orders.php 프로젝트: anderpo/himik
function wc1c_replace_document($document)
{
    global $wpdb;
    if ($document['ХозОперация'] != "Заказ товара" || $document['Роль'] != "Продавец") {
        return;
    }
    $order = wc_get_order($document['Номер']);
    if (!$order) {
        $args = array('status' => 'on-hold', 'customer_note' => @$document['Комментарий']);
        $contragent_name = @$document['Контрагенты'][0]['Наименование'];
        if ($contragent_name == "Гость") {
            $user_id = 0;
        } elseif (strpos($contragent_name, ' ') !== false) {
            list($first_name, $last_name) = explode(' ', $contragent_name, 2);
            $result = $wpdb->get_var($wpdb->prepare("SELECT u1.user_id FROM {$wpdb->usermeta} u1 JOIN {$wpdb->usermeta} u2 ON u1.user_id = u2.user_id WHERE (u1.meta_key = 'billing_first_name' AND u1.meta_value = %s AND u2.meta_key = 'billing_last_name' AND u2.meta_value = %s) OR (u1.meta_key = 'shipping_first_name' AND u1.meta_value = %s AND u2.meta_key = 'shipping_last_name' AND u2.meta_value = %s)", $first_name, $last_name, $first_name, $last_name));
            wc1c_check_wpdb_error();
            if ($result) {
                $user_id = $result;
            }
        }
        if (isset($user_id)) {
            $args['customer_id'] = $user_id;
        }
        $order = wc_create_order($args);
        wc1c_check_wp_error($order);
        if (!isset($user_id)) {
            update_post_meta($order->id, 'wc1c_contragent', $contragent_name);
        }
        $args = array('ID' => $order->id);
        $date = @$document['Дата'];
        if ($date && !empty($document['Время'])) {
            $date .= " {$document['Время']}";
        }
        $timestamp = strtotime($date);
        $args['post_date'] = date("Y-m-d H:i:s", $timestamp);
        $result = wp_update_post($args);
        wc1c_check_wp_error($result);
        if (!$result) {
            wc1c_error("Failed to update order post");
        }
        update_post_meta($order->id, '_wc1c_guid', $document['Ид']);
    } else {
        $args = array('order_id' => $order->id, 'status' => 'on-hold');
        $is_paid = false;
        foreach ($document['ЗначенияРеквизитов'] as $requisite) {
            if (!in_array($requisite['Наименование'], array("Дата оплаты по 1С", "Дата отгрузки по 1С"))) {
                continue;
            }
            $is_paid = true;
            break;
        }
        if ($is_paid) {
            $args['status'] = 'processing';
        }
        $is_passed = false;
        foreach ($document['ЗначенияРеквизитов'] as $requisite) {
            if ($requisite['Наименование'] != 'Проведен' || $requisite['Значение'] != 'true') {
                continue;
            }
            $is_passed = true;
            break;
        }
        if ($is_passed) {
            $args['status'] = 'completed';
        }
        $order = wc_update_order($args);
        wc1c_check_wp_error($order);
    }
    $is_deleted = false;
    foreach ($document['ЗначенияРеквизитов'] as $requisite) {
        if ($requisite['Наименование'] != 'ПометкаУдаления' || $requisite['Значение'] != 'true') {
            continue;
        }
        $is_deleted = true;
        break;
    }
    if ($is_deleted && $order->post_status != 'trash') {
        wp_trash_post($order->id);
    } elseif (!$is_deleted && $order->post_status == 'trash') {
        wp_untrash_post($order->id);
    }
    $post_meta = array();
    if (isset($document['Валюта'])) {
        $post_meta['_order_currency'] = $document['Валюта'];
    }
    if (isset($document['Сумма'])) {
        $post_meta['_order_total'] = wc1c_parse_decimal($document['Сумма']);
    }
    $document_products = array();
    $document_services = array();
    foreach ($document['Товары'] as $i => $document_product) {
        foreach ($document_product['ЗначенияРеквизитов'] as $document_product_requisite) {
            if ($document_product_requisite['Наименование'] != 'ТипНоменклатуры') {
                continue;
            }
            if ($document_product_requisite['Значение'] == 'Услуга') {
                $document_services[] = $document_product;
            } else {
                $document_products[] = $document_product;
            }
            break;
        }
    }
    wc1c_replace_document_products($order, $document_products);
    $post_meta['_order_shipping'] = wc1c_replace_document_services($order, $document_services);
    $current_post_meta = get_post_meta($order->id);
    foreach ($current_post_meta as $meta_key => $meta_value) {
        $current_post_meta[$meta_key] = $meta_value[0];
    }
    foreach ($post_meta as $meta_key => $meta_value) {
        $current_meta_value = @$current_post_meta[$meta_key];
        if ($current_meta_value == $meta_value) {
            continue;
        }
        update_post_meta($order->id, $meta_key, $meta_value);
    }
}
예제 #3
0
파일: exchange.php 프로젝트: anderpo/himik
function wc1c_exchange()
{
    wc1c_set_strict_mode();
    wc1c_set_output_callback();
    wc1c_fix_fastcgi_get();
    if (empty($_GET['type'])) {
        wc1c_error("No type");
    }
    if (empty($_GET['mode'])) {
        wc1c_error("No mode");
    }
    if ($_GET['mode'] == 'checkauth') {
        wc1c_mode_checkauth();
    }
    wc1c_check_auth();
    define('WC1C_DEBUG', true);
    if ($_GET['mode'] == 'init') {
        wc1c_mode_init($_GET['type']);
    } elseif ($_GET['mode'] == 'file') {
        wc1c_mode_file($_GET['type'], $_GET['filename']);
    } elseif ($_GET['mode'] == 'import') {
        wc1c_mode_import($_GET['type'], $_GET['filename']);
    } elseif ($_GET['mode'] == 'query') {
        wc1c_mode_query($_GET['type']);
    } elseif ($_GET['mode'] == 'success') {
        wc1c_mode_success($_GET['type']);
    } else {
        wc1c_error("Unknown mode");
    }
}
예제 #4
0
파일: query.php 프로젝트: anderpo/himik
if (!defined('ABSPATH')) {
    exit;
}
if (!defined('WC1C_CURRENCY')) {
    define('WC1C_CURRENCY', null);
}
WC();
$order_statuses = array_keys(wc_get_order_statuses());
$order_posts = get_posts(array('post_type' => 'shop_order', 'post_status' => $order_statuses, 'meta_query' => array(array('key' => 'wc1c_queried', 'compare' => "NOT EXISTS"))));
$order_post_ids = array();
$documents = array();
foreach ($order_posts as $order_post) {
    $order = wc_get_order($order_post);
    if (!$order) {
        wc1c_error("Failed to get order");
    }
    $order_post_ids[] = $order_post->ID;
    $order_line_items = $order->get_items();
    // $has_missing_item = false;
    foreach ($order_line_items as $key => $order_line_item) {
        $product_id = $order_line_item['variation_id'] ? $order_line_item['variation_id'] : $order_line_item['product_id'];
        $guid = get_post_meta($product_id, '_wc1c_guid', true);
        // if (!$guid) {
        //   $has_missing_item = true;
        //   break;
        // }
        $order_line_items[$key]['wc1c_guid'] = $guid;
    }
    // if ($has_missing_item) continue;
    $order_shipping_items = $order->get_shipping_methods();
예제 #5
0
function wc1c_replace_product($is_full, $guid, $product)
{
    global $wc1c_is_moysklad;
    $product = apply_filters('wc1c_import_product_xml', $product, $is_full);
    if (!$product) {
        return;
    }
    $preserve_fields = apply_filters('wc1c_import_preserve_product_fields', array(), $product, $is_full);
    $is_deleted = @$product['Статус'] == 'Удален';
    $is_draft = @$product['Статус'] == 'Черновик';
    $post_title = @$product['Наименование'];
    if (!$post_title) {
        return;
    }
    $post_content = '';
    foreach ($product['ЗначенияРеквизитов'] as $i => $requisite) {
        if ($requisite['Наименование'] == "Полное наименование" && @$requisite['Значение'][0]) {
            $value = $requisite['Значение'][0];
            if ($wc1c_is_moysklad) {
                $post_content = $value;
            } else {
                $post_title = $value;
            }
            unset($product['ЗначенияРеквизитов'][$i]);
        } elseif ($requisite['Наименование'] == "ОписаниеВФорматеHTML" && @$requisite['Значение'][0]) {
            $post_content = $requisite['Значение'][0];
            unset($product['ЗначенияРеквизитов'][$i]);
        }
    }
    $post_meta = array('_sku' => @$product['Артикул'], '_manage_stock' => 'yes');
    $post_name = sanitize_title($post_title);
    $post_name = apply_filters('wc1c_import_product_slug', $post_name, $product, $is_full);
    $description = isset($product['Описание']) ? $product['Описание'] : '';
    list($is_added, $post_id, $post_meta) = wc1c_replace_post($guid, 'product', $is_deleted, $is_draft, $post_title, $post_name, $description, $post_content, $post_meta, 'product_cat', @$product['Группы'], $preserve_fields);
    // if (isset($product['Пересчет']['Единица'])) {
    //   $quantity = wc1c_parse_decimal($product['Пересчет']['Единица']);
    //   if (isset($product['Пересчет']['Коэффициент'])) $quantity *= wc1c_parse_decimal($product['Пересчет']['Коэффициент']);
    //   wc_update_product_stock($post_id, $quantity);
    //
    //   $stock_status = $quantity > 0 ? 'instock' : 'outofstock';
    //   wc_update_product_stock_status($post_id, $stock_status);
    // }
    $current_product_attributes = isset($post_meta['_product_attributes']) ? maybe_unserialize($post_meta['_product_attributes']) : array();
    $current_product_attribute_variations = array();
    foreach ($current_product_attributes as $current_product_attribute_key => $current_product_attribute) {
        if (!$current_product_attribute['is_variation']) {
            continue;
        }
        unset($current_product_attributes[$current_product_attribute_key]);
        $current_product_attribute_variations[$current_product_attribute_key] = $current_product_attribute;
    }
    $product_attributes = array();
    $product_attribute_values = array();
    if (!empty($product['Изготовитель']['Наименование'])) {
        $product_attribute_values["Наименование изготовителя"] = $product['Изготовитель']['Наименование'];
    }
    if (!empty($product['БазоваяЕдиница']) && trim($product['БазоваяЕдиница'])) {
        $product_attribute_values["Базовая единица"] = trim($product['БазоваяЕдиница']);
    }
    foreach ($product_attribute_values as $product_attribute_name => $product_attribute_value) {
        $product_attribute_key = sanitize_title($product_attribute_name);
        $product_attribute_position = count($product_attributes);
        $product_attributes[$product_attribute_key] = array('name' => wc_clean($product_attribute_name), 'value' => $product_attribute_value, 'position' => $product_attribute_position, 'is_visible' => 0, 'is_variation' => 0, 'is_taxonomy' => 0);
    }
    if ($product['ЗначенияСвойств']) {
        $attribute_guids = get_option('wc1c_guid_attributes', array());
        $terms = array();
        foreach ($product['ЗначенияСвойств'] as $property) {
            $attribute_guid = $property['Ид'];
            $attribute_id = @$attribute_guids[$attribute_guid];
            if (!$attribute_id) {
                continue;
            }
            $attribute = wc1c_woocommerce_attribute_by_id($attribute_id);
            if (!$attribute) {
                wc1c_error("Failed to get attribute");
            }
            $attribute_terms = array();
            $attribute_values = array();
            $property_values = @$property['Значение'];
            if ($property_values) {
                foreach ($property_values as $property_value) {
                    if (!$property_value) {
                        continue;
                    }
                    if ($attribute['attribute_type'] == 'select' && preg_match("/^\\w+-\\w+-\\w+-\\w+-\\w+\$/", $property_value)) {
                        $term_id = wc1c_term_id_by_meta('wc1c_guid', "{$attribute['taxonomy']}::{$property_value}");
                        if ($term_id) {
                            $attribute_terms[] = (int) $term_id;
                        }
                    } else {
                        if (!defined('WC1C_MULTIPLE_VALUES_DELIMETER')) {
                            $attribute_values[] = $property_value;
                        } else {
                            $term_names = explode(WC1C_MULTIPLE_VALUES_DELIMETER, $property_value);
                            $term_names = array_map('trim', $term_names);
                            foreach ($term_names as $term_name) {
                                $result = get_term_by('name', $term_name, $attribute['taxonomy'], ARRAY_A);
                                if (!$result) {
                                    $slug = wc1c_unique_term_slug($term_name, $attribute['taxonomy']);
                                    $args = array('slug' => $slug);
                                    $result = wp_insert_term($term_name, $attribute['taxonomy'], $args);
                                    wc1c_check_wpdb_error();
                                    wc1c_check_wp_error($result);
                                }
                                $attribute_terms[] = $result['term_id'];
                            }
                        }
                    }
                }
            }
            if ($attribute_terms || $attribute_values) {
                $product_attribute = array('name' => null, 'value' => '', 'position' => count($product_attributes), 'is_visible' => 1, 'is_variation' => 0, 'is_taxonomy' => 0);
                if ($attribute_terms) {
                    $product_attribute['name'] = $attribute['taxonomy'];
                    $product_attribute['is_taxonomy'] = 1;
                } elseif ($attribute_values) {
                    $product_attribute['name'] = $attribute['attribute_label'];
                    $product_attribute['value'] = implode(" | ", $attribute_values);
                }
                $product_attribute_key = sanitize_title($attribute['taxonomy']);
                $product_attributes[$product_attribute_key] = $product_attribute;
            }
            if ($attribute_terms) {
                if (!isset($terms[$attribute['taxonomy']])) {
                    $terms[$attribute['taxonomy']] = array();
                }
                $terms[$attribute['taxonomy']] = array_merge($terms[$attribute['taxonomy']], $attribute_terms);
            }
        }
        foreach ($terms as $attribute_taxonomy => $attribute_terms) {
            register_taxonomy($attribute_taxonomy, null);
            $result = wp_set_post_terms($post_id, $attribute_terms, $attribute_taxonomy);
            wc1c_check_wp_error($result);
        }
    }
    foreach ($product['ЗначенияРеквизитов'] as $requisite) {
        $attribute_values = @$requisite['Значение'];
        if (!$attribute_values) {
            continue;
        }
        if (strpos($attribute_values[0], "import_files/") === 0) {
            continue;
        }
        $requisite_name = $requisite['Наименование'];
        $product_attribute_name = strpos($requisite_name, ' ') === false ? preg_replace_callback("/(?<!^)\\p{Lu}/u", 'wc1c_replace_requisite_name_callback', $requisite_name) : $requisite_name;
        $product_attribute_key = sanitize_title($requisite_name);
        $product_attribute_position = count($product_attributes);
        $product_attributes[$product_attribute_key] = array('name' => wc_clean($product_attribute_name), 'value' => implode(" | ", $attribute_values), 'position' => $product_attribute_position, 'is_visible' => 0, 'is_variation' => 0, 'is_taxonomy' => 0);
    }
    foreach ($product['ХарактеристикиТовара'] as $characteristic) {
        $attribute_value = @$characteristic['Значение'];
        if (!$attribute_value) {
            continue;
        }
        $product_attribute_name = $characteristic['Наименование'];
        $product_attribute_key = sanitize_title($product_attribute_name);
        $product_attribute_position = count($product_attributes);
        $product_attributes[$product_attribute_key] = array('name' => wc_clean($product_attribute_name), 'value' => $attribute_value, 'position' => $product_attribute_position, 'is_visible' => 1, 'is_variation' => 0, 'is_taxonomy' => 0);
    }
    if (!in_array('attributes', $preserve_fields)) {
        $old_product_attributes = array_diff_key($current_product_attributes, $product_attributes);
        $old_taxonomies = array();
        foreach ($old_product_attributes as $old_product_attribute) {
            if ($old_product_attribute['is_taxonomy']) {
                $old_taxonomies[] = $old_product_attribute['name'];
            } else {
                $key = array_search($old_product_attribute, $product_attributes);
                if ($key !== false) {
                    unset($product_attributes[$key]);
                }
            }
        }
        foreach ($old_taxonomies as $old_taxonomy) {
            register_taxonomy($old_taxonomy, null);
        }
        wp_delete_object_term_relationships($post_id, $old_taxonomies);
        ksort($current_product_attributes);
        $product_attributes_copy = $product_attributes;
        ksort($product_attributes_copy);
        if ($current_product_attributes != $product_attributes_copy) {
            $product_attributes = array_merge($product_attributes, $current_product_attribute_variations);
            update_post_meta($post_id, '_product_attributes', $product_attributes);
        }
    }
    if (!in_array('attachments', $preserve_fields)) {
        $attachments = array();
        if (!empty($product['Картинка'])) {
            $attachments = array_filter($product['Картинка']);
            $attachments = array_fill_keys($attachments, array());
        }
        if ($product['ЗначенияРеквизитов']) {
            $attachment_keys = array('ОписаниеФайла' => 'description');
            foreach ($product['ЗначенияРеквизитов'] as $requisite) {
                $attribute_name = $requisite['Наименование'];
                if (!isset($attachment_keys[$attribute_name])) {
                    continue;
                }
                $attribute_values = @$requisite['Значение'];
                if (!$attribute_values) {
                    continue;
                }
                $attribute_value = $attribute_values[0];
                if (strpos($attribute_value, "import_files/") !== 0) {
                    continue;
                }
                list($picture_path, $attribute_value) = explode('#', $attribute_value, 2);
                if (!isset($attachments[$picture_path])) {
                    continue;
                }
                $attachment_key = $attachment_keys[$attribute_name];
                $attachments[$picture_path][$attachment_key] = $attribute_value;
            }
        }
        if ($attachments) {
            $attachment_ids = wc1c_replace_post_attachments($post_id, $attachments);
            $new_post_meta = array('_product_image_gallery' => implode(',', array_slice($attachment_ids, 1)), '_thumbnail_id' => @$attachment_ids[0]);
            foreach ($new_post_meta as $meta_key => $meta_value) {
                if ($meta_value != @$post_meta[$meta_key]) {
                    update_post_meta($post_id, $meta_key, $meta_value);
                }
            }
        }
    }
    do_action('wc1c_post_product', $post_id, $is_added, $product, $is_full);
    return $post_id;
}