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); } } }
function wc1c_mode_checkauth() { foreach (array('HTTP_AUTHORIZATION', 'REDIRECT_HTTP_AUTHORIZATION') as $server_key) { if (!isset($_SERVER[$server_key])) { continue; } list(, $auth_value) = explode(' ', $_SERVER[$server_key], 2); $auth_value = base64_decode($auth_value); list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = explode(':', $auth_value); break; } if (!isset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])) { wc1c_error("No authentication credentials"); } $user = wp_authenticate($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']); wc1c_check_wp_error($user); wc1c_check_permissions($user); $expiration = time() + apply_filters('auth_cookie_expiration', DAY_IN_SECONDS, $user->ID, false); $auth_cookie = wp_generate_auth_cookie($user->ID, $expiration); exit("success\nwc1c-auth\n{$auth_cookie}"); }
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); } }
function wc1c_clean_product_terms() { global $wpdb; $wpdb->query("UPDATE {$wpdb->term_taxonomy} tt SET count = (SELECT COUNT(*) FROM {$wpdb->term_relationships} WHERE term_taxonomy_id = tt.term_taxonomy_id) WHERE taxonomy LIKE 'pa_%'"); wc1c_check_wpdb_error(); $rows = $wpdb->get_results("SELECT tm.term_id, taxonomy FROM {$wpdb->term_taxonomy} tt LEFT JOIN {$wpdb->termmeta} tm ON tt.term_id = tm.term_id AND meta_key = 'wc1c_guid' WHERE meta_value IS NULL AND taxonomy LIKE 'pa_%' AND count = 0"); wc1c_check_wpdb_error(); foreach ($rows as $row) { register_taxonomy($row->taxonomy, null); $result = wp_delete_term($row->term_id, $row->taxonomy); wc1c_check_wp_error($result); } }