Ejemplo n.º 1
0
function wc1c_replace_document_products($order, $document_products)
{
    $line_items = $order->get_items();
    $line_item_ids = array();
    foreach ($document_products as $i => $document_product) {
        $product_id = wc1c_post_id_by_meta('_wc1c_guid', $document_product['Ид']);
        if (!$product_id) {
            continue;
        }
        $product = wc_get_product($product_id);
        if (!$product) {
            wc1c_error("Failed to get product");
        }
        $document_products[$i]['product'] = $product;
        $current_line_item_id = null;
        foreach ($line_items as $line_item_id => $line_item) {
            if ($line_item['product_id'] != $product->id || (int) $line_item['variation_id'] != $product->variation_id) {
                continue;
            }
            $current_line_item_id = $line_item_id;
            break;
        }
        $document_products[$i]['line_item_id'] = $current_line_item_id;
        if ($current_line_item_id) {
            $line_item_ids[] = $current_line_item_id;
        }
    }
    $old_line_item_ids = array_diff(array_keys($line_items), $line_item_ids);
    if ($old_line_item_ids) {
        $order->remove_order_items('line_item');
        foreach ($document_products as $i => $document_product) {
            $document_products[$i]['line_item_id'] = null;
        }
    }
    foreach ($document_products as $document_product) {
        $quantity = isset($document_product['Количество']) ? wc1c_parse_decimal($document_product['Количество']) : 1;
        $coefficient = isset($document_product['Коэффициент']) ? wc1c_parse_decimal($document_product['Коэффициент']) : 1;
        $quantity *= $coefficient;
        if (!empty($document_product['Сумма'])) {
            $total = wc1c_parse_decimal($document_product['Сумма']);
        } else {
            $price = wc1c_parse_decimal(@$document_product['ЦенаЗаЕдиницу']);
            $total = $price * $quantity;
        }
        $args = array('totals' => array('subtotal' => $total, 'total' => $total));
        if (!isset($document_product['product'])) {
            continue;
        }
        $product = $document_product['product'];
        if ($product->variation_id) {
            $attributes = $product->get_variation_attributes();
            $variation = array();
            foreach ($attributes as $attribute_key => $attribute_value) {
                $variation[urldecode($attribute_key)] = urldecode($attribute_value);
            }
            $args['variation'] = $variation;
        }
        $line_item_id = $document_product['line_item_id'];
        if (!$line_item_id) {
            $line_item_id = $order->add_product($product, $quantity, $args);
            if (!$line_item_id) {
                wc1c_error("Failed to add product to the order");
            }
        } else {
            $args['qty'] = $quantity;
            $result = $order->update_product($line_item_id, $product, $args);
            if (!$result) {
                wc1c_error("Failed to update product in the order");
            }
        }
    }
}
Ejemplo n.º 2
0
function wc1c_replace_suboffers($is_full, $suboffers, $are_products = false)
{
    if (!$suboffers) {
        return;
    }
    $product_guid = $suboffers[0]['product_guid'];
    $post_id = wc1c_post_id_by_meta('_wc1c_guid', $product_guid);
    if (!$post_id && !$are_products) {
        return;
    }
    if ($are_products) {
        $product = $suboffers[0]['product'];
        $product['Ид'] = $product_guid;
        $post_id = wc1c_replace_product($suboffers[0]['is_full'], $product_guid, $product);
    }
    if (!WC1C_DISABLE_VARIATIONS) {
        $result = wp_set_post_terms($post_id, 'variable', 'product_type');
        wc1c_check_wp_error($result);
    }
    $offer_characteristics = array();
    foreach ($suboffers as $suboffer) {
        if (isset($suboffer['offer']['ХарактеристикиТовара'])) {
            foreach ($suboffer['offer']['ХарактеристикиТовара'] as $suboffer_characteristic) {
                $characteristic_name = $suboffer_characteristic['Наименование'];
                if (!isset($offer_characteristics[$characteristic_name])) {
                    $offer_characteristics[$characteristic_name] = array();
                }
                $characteristic_value = @$suboffer_characteristic['Значение'];
                if (!in_array($characteristic_value, $offer_characteristics[$characteristic_name])) {
                    $offer_characteristics[$characteristic_name][] = $characteristic_value;
                }
            }
        }
    }
    if ($offer_characteristics) {
        ksort($offer_characteristics);
        foreach ($offer_characteristics as $characteristic_name => &$characteristic_values) {
            sort($characteristic_values);
        }
        $current_product_attributes = get_post_meta($post_id, '_product_attributes', true);
        if (!$current_product_attributes) {
            $current_product_attributes = array();
        }
        $product_attributes = array();
        foreach ($current_product_attributes as $current_product_attribute_key => $current_product_attribute) {
            if (!$current_product_attribute['is_variation']) {
                $product_attributes[$current_product_attribute_key] = $current_product_attribute;
            }
        }
        foreach ($offer_characteristics as $offer_characteristic_name => $offer_characteristic_values) {
            $product_attribute_key = sanitize_title($offer_characteristic_name);
            $product_attribute_position = count($product_attributes);
            $product_attributes[$product_attribute_key] = array('name' => wc_clean($offer_characteristic_name), 'value' => implode(" | ", $offer_characteristic_values), 'position' => $product_attribute_position, 'is_visible' => 1, 'is_variation' => 1, 'is_taxonomy' => 0);
        }
        ksort($current_product_attributes);
        $product_attributes_copy = $product_attributes;
        ksort($product_attributes_copy);
        if ($current_product_attributes != $product_attributes_copy) {
            update_post_meta($post_id, '_product_attributes', $product_attributes);
        }
    }
    $current_product_variation_ids = array();
    $product_variation_posts = get_children("post_parent={$post_id}&post_type=product_variation");
    foreach ($product_variation_posts as $product_variation_post) {
        $current_product_variation_ids[] = $product_variation_post->ID;
    }
    $product_variation_ids = array();
    foreach ($suboffers as $i => $suboffer) {
        $product_variation_id = wc1c_replace_product_variation($suboffer['guid'], $post_id, $i + 1);
        $product_variation_ids[] = $product_variation_id;
        $attributes = array_fill_keys(array_keys($offer_characteristics), '');
        if (isset($suboffer['offer']['ХарактеристикиТовара'])) {
            foreach ($suboffer['offer']['ХарактеристикиТовара'] as $suboffer_characteristic) {
                $suboffer_characteristic_value = @$suboffer_characteristic['Значение'];
                if ($suboffer_characteristic_value) {
                    $attributes[$suboffer_characteristic['Наименование']] = $suboffer_characteristic_value;
                }
            }
        }
        if ($are_products) {
            wc1c_replace_offer_post_meta($is_full, $product_variation_id, array(), $attributes);
        } else {
            wc1c_replace_offer_post_meta($is_full, $product_variation_id, $suboffer['offer'], $attributes);
        }
    }
    if (!WC1C_PRESERVE_PRODUCT_VARIATIONS) {
        $deleted_product_variation_ids = array_diff($current_product_variation_ids, $product_variation_ids);
        foreach ($deleted_product_variation_ids as $deleted_product_variation_id) {
            wp_delete_post($deleted_product_variation_id, true);
        }
    }
}
Ejemplo n.º 3
0
function wc1c_replace_post($guid, $post_type, $is_deleted, $is_draft, $post_title, $post_name, $post_excerpt, $post_content, $post_meta, $category_taxonomy, $category_guids, $preserve_fields)
{
    $post_id = wc1c_post_id_by_meta('_wc1c_guid', $guid);
    if (!$post_excerpt) {
        $post_excerpt = '';
    }
    if (WC1C_PRODUCT_DESCRIPTION_TO_CONTENT) {
        $post_content = $post_excerpt;
        $post_excerpt = '';
    }
    $args = compact('post_type', 'post_title', 'post_excerpt', 'post_content');
    if (!$post_id) {
        $args = array_merge($args, array('post_name' => $post_name, 'post_status' => $is_draft ? 'draft' : 'publish'));
        $post_id = wp_insert_post($args, true);
        wc1c_check_wpdb_error();
        wc1c_check_wp_error($post_id);
        update_post_meta($post_id, '_visibility', 'visible');
        update_post_meta($post_id, '_wc1c_guid', $guid);
        $is_added = true;
    } else {
        $is_added = false;
    }
    $post = get_post($post_id);
    if (!$post) {
        wc1c_error("Failed to get post");
    }
    if (!$is_added) {
        if (in_array('title', $preserve_fields)) {
            unset($args['post_title']);
        }
        if (in_array('excerpt', $preserve_fields)) {
            unset($args['post_excerpt']);
        }
        if (in_array('body', $preserve_fields)) {
            unset($args['post_content']);
        }
        foreach ($args as $key => $value) {
            if ($post->{$key} == $value) {
                continue;
            }
            $is_changed = true;
            break;
        }
        if (!empty($is_changed)) {
            $post_date = current_time('mysql');
            $args = array_merge($args, array('ID' => $post_id, 'post_date' => $post_date, 'post_date_gmt' => get_gmt_from_date($post_date)));
            $post_id = wp_update_post($args, true);
            wc1c_check_wp_error($post_id);
        }
    }
    if ($is_deleted && $post->post_status != 'trash') {
        wp_trash_post($post_id);
    } elseif (!$is_deleted && $post->post_status == 'trash') {
        wp_untrash_post($post_id);
    }
    $current_post_meta = get_post_meta($post_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($post_id, $meta_key, $meta_value);
    }
    if (!in_array('categories', $preserve_fields)) {
        $current_category_ids = wp_get_post_terms($post_id, $category_taxonomy, "fields=ids");
        wc1c_check_wp_error($current_category_ids);
        $category_ids = array();
        if ($category_guids) {
            foreach ($category_guids as $category_guid) {
                $category_id = wc1c_term_id_by_meta('wc1c_guid', "product_cat::{$category_guid}");
                if ($category_id) {
                    $category_ids[] = $category_id;
                }
            }
        }
        sort($current_category_ids);
        sort($category_ids);
        if ($current_category_ids != $category_ids) {
            $result = wp_set_post_terms($post_id, $category_ids, $category_taxonomy);
            wc1c_check_wp_error($result);
        }
    }
    update_post_meta($post_id, '_wc1c_timestamp', WC1C_TIMESTAMP);
    return array($is_added, $post_id, $current_post_meta);
}