/**
 * Order terms when a new term is created.
 *
 * @access public
 * @param mixed $term_id
 * @param mixed $tt_id
 * @param mixed $taxonomy
 * @return void
 */
function woocommerce_create_term($term_id, $tt_id, $taxonomy)
{
    if (!$taxonomy == 'product_cat' && !strstr($taxonomy, 'pa_')) {
        return;
    }
    $next_id = null;
    $term = get_term($term_id, $taxonomy);
    if ($term != null) {
        // gets the sibling terms
        $siblings = get_terms($taxonomy, "parent={$term->parent}&menu_order=ASC&hide_empty=0");
        foreach ($siblings as $sibling) {
            if ($sibling->term_id == $term_id) {
                continue;
            }
            $next_id = $sibling->term_id;
            // first sibling term of the hierarchy level
            break;
        }
        // reorder
        woocommerce_order_terms($term, $next_id, $taxonomy);
    }
}
예제 #2
0
/**
 * Ajax request handling for categories ordering
 *
 * @access public
 * @return void
 */
function woocommerce_term_ordering()
{
    global $wpdb;
    $id = (int) $_POST['id'];
    $next_id = isset($_POST['nextid']) && (int) $_POST['nextid'] ? (int) $_POST['nextid'] : null;
    $taxonomy = isset($_POST['thetaxonomy']) ? esc_attr($_POST['thetaxonomy']) : null;
    $term = get_term_by('id', $id, $taxonomy);
    if (!$id || !$term || !$taxonomy) {
        die(0);
    }
    woocommerce_order_terms($term, $next_id, $taxonomy);
    $children = get_terms($taxonomy, "child_of={$id}&menu_order=ASC&hide_empty=0");
    if ($term && sizeof($children)) {
        echo 'children';
        die;
    }
}
/**
 * Move a term before the a	given element of its hierarchy level
 *
 * @access public
 * @param int $the_term
 * @param int $next_id the id of the next sibling element in save hierarchy level
 * @param string $taxonomy
 * @param int $index (default: 0)
 * @param mixed $terms (default: null)
 * @return int
 */
function woocommerce_order_terms($the_term, $next_id, $taxonomy, $index = 0, $terms = null)
{
    if (!$terms) {
        $terms = get_terms($taxonomy, 'menu_order=ASC&hide_empty=0&parent=0');
    }
    if (empty($terms)) {
        return $index;
    }
    $id = $the_term->term_id;
    $term_in_level = false;
    // flag: is our term to order in this level of terms
    foreach ($terms as $term) {
        if ($term->term_id == $id) {
            // our term to order, we skip
            $term_in_level = true;
            continue;
            // our term to order, we skip
        }
        // the nextid of our term to order, lets move our term here
        if (null !== $next_id && $term->term_id == $next_id) {
            $index++;
            $index = woocommerce_set_term_order($id, $index, $taxonomy, true);
        }
        // set order
        $index++;
        $index = woocommerce_set_term_order($term->term_id, $index, $taxonomy);
        // if that term has children we walk through them
        $children = get_terms($taxonomy, "parent={$term->term_id}&menu_order=ASC&hide_empty=0");
        if (!empty($children)) {
            $index = woocommerce_order_terms($the_term, $next_id, $taxonomy, $index, $children);
        }
    }
    // no nextid meaning our term is in last position
    if ($term_in_level && null === $next_id) {
        $index = woocommerce_set_term_order($id, $index + 1, $taxonomy, true);
    }
    return $index;
}