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_import_end_element_handler($is_full, $names, $depth, $name) { global $wpdb, $wc1c_groups, $wc1c_group_depth, $wc1c_group_order, $wc1c_property, $wc1c_property_order, $wc1c_requisite_properties, $wc1c_product, $wc1c_subproducts; if (@$names[$depth - 1] == 'Группы' && $name == 'Группа') { if (empty($wc1c_groups[$wc1c_group_depth]['Группы'])) { $result = wc1c_replace_group($is_full, $wc1c_groups[$wc1c_group_depth], $wc1c_group_order, $wc1c_groups); if ($result) { $wc1c_group_order++; } } array_pop($wc1c_groups); $wc1c_group_depth--; } if (@$names[$depth - 1] == 'Классификатор' && $name == 'Группы') { wc1c_clean_woocommerce_categories($is_full); } elseif (@$names[$depth - 1] == 'Свойства' && $name == 'Свойство') { $result = wc1c_replace_property($is_full, $wc1c_property, $wc1c_property_order); if ($result) { $attribute_taxonomy = $result; $wc1c_property_order++; wc1c_clean_woocommerce_attribute_options($is_full, $attribute_taxonomy); } else { $wc1c_requisite_properties[$wc1c_property['Ид']] = $wc1c_property; } } elseif (@$names[$depth - 1] == 'Классификатор' && $name == 'Свойства') { wc1c_clean_woocommerce_attributes($is_full); delete_transient('wc_attribute_taxonomies'); } elseif (@$names[$depth - 1] == 'Товары' && $name == 'Товар') { if ($wc1c_requisite_properties) { foreach ($wc1c_product['ЗначенияСвойств'] as $product_property) { if (!array_key_exists($product_property['Ид'], $wc1c_requisite_properties)) { continue; } $property = $wc1c_requisite_properties[$product_property['Ид']]; $wc1c_product['ЗначенияРеквизитов'][] = array('Наименование' => $property['Наименование'], 'Значение' => $product_property['Значение']); } } if (strpos($wc1c_product['Ид'], '#') === false || WC1C_DISABLE_VARIATIONS) { $guid = $wc1c_product['Ид']; wc1c_replace_product($is_full, $guid, $wc1c_product); } else { $guid = $wc1c_product['Ид']; list($product_guid, ) = explode('#', $guid, 2); if (empty($wc1c_subproducts) || $wc1c_subproducts[0]['product_guid'] != $product_guid) { if ($wc1c_subproducts) { wc1c_replace_subproducts($is_full, $wc1c_subproducts); } $wc1c_subproducts = array(); } $wc1c_subproducts[] = array('guid' => $wc1c_product['Ид'], 'product_guid' => $product_guid, 'characteristics' => $wc1c_product['ХарактеристикиТовара'], 'is_full' => $is_full, 'product' => $wc1c_product); } } elseif (@$names[$depth - 1] == 'Каталог' && $name == 'Товары') { if ($wc1c_subproducts) { wc1c_replace_subproducts($is_full, $wc1c_subproducts); } wc1c_clean_products($is_full); wc1c_clean_product_terms(); } elseif (!$depth && $name == 'КоммерческаяИнформация') { $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_%'"); wc1c_check_wpdb_error(); do_action('wc1c_post_import', $is_full); } }