예제 #1
0
function wc1c_replace_offer_post_meta($is_full, $post_id, $offer, $attributes = array())
{
    $price = isset($offer['Цена']['ЦенаЗаЕдиницу']) ? wc1c_parse_decimal($offer['Цена']['ЦенаЗаЕдиницу']) : null;
    if (!is_null($price)) {
        $coefficient = isset($offer['Цена']['Коэффициент']) ? wc1c_parse_decimal($offer['Цена']['Коэффициент']) : null;
        if (!is_null($coefficient)) {
            $price *= $coefficient;
        }
    }
    $post_meta = array();
    if (!is_null($price)) {
        $post_meta['_regular_price'] = $price;
        $post_meta['_manage_stock'] = 'yes';
    }
    if ($attributes) {
        foreach ($attributes as $attribute_name => $attribute_value) {
            $meta_key = 'attribute_' . sanitize_title($attribute_name);
            $post_meta[$meta_key] = $attribute_value;
        }
        $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 ($current_post_meta as $meta_key => $meta_value) {
            if (strpos($meta_key, 'attribute_') !== 0 || array_key_exists($meta_key, $post_meta)) {
                continue;
            }
            delete_post_meta($post_id, $meta_key);
        }
    }
    if (!is_null($price)) {
        $sale_price = @$current_post_meta['_sale_price'];
        $sale_price_from = @$current_post_meta['_sale_price_dates_from'];
        $sale_price_to = @$current_post_meta['_sale_price_dates_to'];
        if (empty($current_post_meta['_sale_price'])) {
            $post_meta['_price'] = $price;
        } else {
            if (empty($sale_price_from) && empty($sale_price_to)) {
                $post_meta['_price'] = $current_post_meta['_sale_price'];
            } else {
                $now = strtotime('now', current_time('timestamp'));
                if (!empty($sale_price_from) && strtotime($sale_price_from) < $now) {
                    $post_meta['_price'] = $current_post_meta['_sale_price'];
                }
                if (!empty($sale_price_to) && strtotime($sale_price_to) < $now) {
                    $post_meta['_price'] = $price;
                    $post_meta['_sale_price_dates_from'] = '';
                    $post_meta['_sale_price_dates_to'] = '';
                }
            }
        }
    }
    foreach ($post_meta as $meta_key => $meta_value) {
        $current_meta_value = @$current_post_meta[$meta_key];
        if ($meta_value !== '' && $current_meta_value == $meta_value) {
            continue;
        }
        if ($meta_value === '' && $current_meta_value === $meta_value) {
            continue;
        }
        update_post_meta($post_id, $meta_key, $meta_value);
    }
    $quantity = isset($offer['Количество']) ? $offer['Количество'] : @$offer['КоличествоНаСкладе'];
    if (!is_null($quantity)) {
        $quantity = wc1c_parse_decimal($quantity);
        wc_update_product_stock($post_id, $quantity);
        $stock_status = $quantity > 0 ? 'instock' : 'outofstock';
        @wc_update_product_stock_status($post_id, $stock_status);
    }
    do_action('wc1c_post_offer_meta', $post_id, $offer, $is_full);
}
예제 #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);
    }
}