/** * Set the sort order of a term. * * @param int $term_id * @param int $index * @param string $taxonomy * @param bool $recursive (default: false) * @return int */ function wc_set_term_order($term_id, $index, $taxonomy, $recursive = false) { $term_id = (int) $term_id; $index = (int) $index; // Meta name if (taxonomy_is_product_attribute($taxonomy)) { $meta_name = 'order_' . esc_attr($taxonomy); } else { $meta_name = 'order'; } update_woocommerce_term_meta($term_id, $meta_name, $index); if (!$recursive) { return $index; } $children = get_terms($taxonomy, "parent={$term_id}&menu_order=ASC&hide_empty=0"); foreach ($children as $term) { $index++; $index = wc_set_term_order($term->term_id, $index, $taxonomy, true); } clean_term_cache($term_id, $taxonomy); return $index; }
function wc1c_replace_term($is_full, $guid, $parent_guid, $name, $taxonomy, $order, $use_guid_as_slug = false) { global $wpdb; $term_id = wc1c_term_id_by_meta('wc1c_guid', "{$taxonomy}::{$guid}"); if ($term_id) { $term = get_term($term_id, $taxonomy); } $parent = $parent_guid ? wc1c_term_id_by_meta('wc1c_guid', "{$taxonomy}::{$parent_guid}") : null; if (!$term_id || !$term) { $name = wc1c_unique_term_name($name, $taxonomy, $parent); $slug = wc1c_unique_term_slug($name, $taxonomy, $parent); $args = array('slug' => $slug, 'parent' => $parent); if ($use_guid_as_slug) { $args['slug'] = $guid; } $result = wp_insert_term($name, $taxonomy, $args); wc1c_check_wpdb_error(); wc1c_check_wp_error($result); $term_id = $result['term_id']; update_woocommerce_term_meta($term_id, 'wc1c_guid', "{$taxonomy}::{$guid}"); $is_added = true; } if (empty($is_added)) { if (trim($name) != $term->name) { $name = wc1c_unique_term_name($name, $taxonomy, $parent); } $parent = $parent_guid ? wc1c_term_id_by_meta('wc1c_guid', "{$taxonomy}::{$parent_guid}") : null; $args = array('name' => $name, 'parent' => $parent); $result = wp_update_term($term_id, $taxonomy, $args); wc1c_check_wp_error($result); } if ($is_full) { wc_set_term_order($term_id, $order, $taxonomy); } update_woocommerce_term_meta($term_id, 'wc1c_timestamp', WC1C_TIMESTAMP); }
/** * @deprecated */ function woocommerce_set_term_order($term_id, $index, $taxonomy, $recursive = false) { return wc_set_term_order($term_id, $index, $taxonomy, $recursive); }
/** * Import product categories * * @return int Number of product categories imported */ private function import_product_categories() { $cat_count = 0; $terms = array(); $taxonomy = 'product_cat'; $term_metakey = '_fgm2wc_old_product_category_id'; $used_slugs = array(); // Set the list of previously imported categories $this->imported_categories = $this->get_term_metas_by_metakey($term_metakey); $categories = $this->get_all_product_categories(); foreach ($categories as $category) { // Check if the category is already imported if (array_key_exists($category['entity_id'], $this->imported_categories)) { continue; // Do not import already imported category } // Other fields $category = array_merge($category, $this->get_attribute_values($category['entity_id'], $category['entity_type_id'], array('name', 'description', 'url_key', 'image'))); // Date $date = $category['created_at']; // Slug $slug = isset($category['url_key']) ? $category['url_key'] : sanitize_title($category['name']); $slug = $this->build_unique_slug($slug, $used_slugs); $used_slugs[] = $slug; // Insert the category $new_category = array('description' => isset($category['description']) ? $category['description'] : '', 'slug' => $slug); // Hook before inserting the category $new_category = apply_filters('fgm2wc_pre_insert_category', $new_category, $category); $new_term = wp_insert_term($category['name'], $taxonomy, $new_category); if (!is_wp_error($new_term)) { $cat_count++; $terms[] = $new_term['term_id']; // Store the Magento category ID add_term_meta($new_term['term_id'], $term_metakey, $category['entity_id'], true); // Category ordering if (function_exists('wc_set_term_order')) { wc_set_term_order($new_term['term_id'], $category['position'], $taxonomy); } // Category image if (!$this->plugin_options['skip_media'] && function_exists('update_woocommerce_term_meta')) { if (isset($category['image']) && !empty($category['image'])) { $image_path = '/media/catalog/category/' . $category['image']; $thumbnail_id = $this->import_media($category['name'], $image_path, $date); if (!empty($thumbnail_id)) { $this->media_count++; update_woocommerce_term_meta($new_term['term_id'], 'thumbnail_id', $thumbnail_id); } } } // Hook after inserting the category do_action('fgm2wc_post_insert_category', $new_term['term_id'], $category); } } // Set the list of imported categories $this->imported_categories = $this->get_term_metas_by_metakey($term_metakey); // Update the categories with their parent ids foreach ($categories as $category) { if (array_key_exists($category['entity_id'], $this->imported_categories) && array_key_exists($category['parent_id'], $this->imported_categories)) { $cat_id = $this->imported_categories[$category['entity_id']]; $parent_cat_id = $this->imported_categories[$category['parent_id']]; $cat = get_term_by('term_taxonomy_id', $cat_id, $taxonomy); $parent_cat = get_term_by('term_taxonomy_id', $parent_cat_id, $taxonomy); if ($cat && $parent_cat) { // Hook before editing the category $cat = apply_filters('fgm2wc_pre_edit_category', $cat, $parent_cat); wp_update_term($cat->term_id, $taxonomy, array('parent' => $parent_cat->term_id)); // Hook after editing the category do_action('fgm2wc_post_edit_category', $cat); } } } // Hook after importing all the categories do_action('fgm2wc_post_import_categories', $categories); // Update cache if (!empty($terms)) { wp_update_term_count_now($terms, $taxonomy); $this->clean_cache($terms, $taxonomy); } $this->display_admin_notice(sprintf(_n('%d product category imported', '%d product categories imported', $cat_count, 'fg-magento-to-woocommerce'), $cat_count)); }