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; }
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_delete_woocommerce_attribute($attribute_id) { global $wpdb; $attribute = wc1c_woocommerce_attribute_by_id($attribute_id); if (!$attribute) { return false; } delete_option("{$attribute['taxonomy']}_children"); $terms = get_terms($attribute['taxonomy'], "hide_empty=0"); foreach ($terms as $term) { wp_delete_term($term->term_id, $attribute['taxonomy']); } $wpdb->delete("{$wpdb->prefix}woocommerce_attribute_taxonomies", compact('attribute_id')); if (function_exists('wc1c_check_wpdb_error')) { wc1c_check_wpdb_error(); } }
function wc1c_post_id_by_meta($key, $value) { global $wpdb; if ($value === null) { return; } $cache_key = "wc1c_post_id_by_meta-{$key}-{$value}"; $post_id = wp_cache_get($cache_key); if ($post_id === false) { $post_id = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} JOIN {$wpdb->posts} ON post_id = ID WHERE meta_key = %s AND meta_value = %s", $key, $value)); wc1c_check_wpdb_error(); if ($post_id) { wp_cache_set($cache_key, $post_id); } } return $post_id; }
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); } }