在产品上附加WooCommerce产品类别,而不是覆盖

时间:2019-04-20 14:01:39

标签: php wordpress woocommerce custom-taxonomy taxonomy-terms

在通过CSV上传新类别时,我试图强迫woocommerce附加产品类别而不是覆盖。

我尝试查找代码片段,搜索了代码库,并尝试使用wp-includes / post.php创建函数。

function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = true ) {
    $post_id = (int) $post_id;

    if ( ! $post_id ) {
        return true;
    }

    if ( empty( $tags ) ) {
        $tags = array();
    }

    if ( ! is_array( $tags ) ) {
        $comma = _x( ',', 'tag delimiter' );
        if ( ',' !== $comma ) {
            $tags = str_replace( $comma, ',', $tags );
        }
        $tags = explode( ',', trim( $tags, " \n\t\r\0\x0B," ) );
    }

    if ( is_taxonomy_hierarchical( $taxonomy ) ) {
        $tags = array_unique( array_map( 'intval', $tags ) );
    }

    return wp_set_object_terms( $post_id, $tags, $taxonomy, $append );
}

我希望woocommerce产品类别会附加而不是覆盖。

实际结果

  

第29行出现严重错误:无法重新声明wp_set_post_terms()   (先前在/var/www/html/wp-includes/post.php:4108中声明)

尝试了此代码。没有错误,但没有附加类别

function append_post_categories( $post_ID = array(), $post_categories = array(), $append = true ) {
    $post_ID     = (int) $post_ID;
    $post_type   = get_post_type( $post_ID );
    $post_status = get_post_status( $post_ID );
    // If $post_categories isn't already an array, make it one:
    $post_categories = (array) $post_categories;
    if ( empty( $post_categories ) ) {
        if ( 'post' == $post_type && 'auto-draft' != $post_status ) {
            $post_categories = array( get_option( 'default_category' ) );
            $append          = true;
        } else {
            $post_categories = array();
        }
    } elseif ( 1 == count( $post_categories ) && '' == reset( $post_categories ) ) {
        return true;
    }

    return wp_set_post_terms( $post_ID, $post_categories, 'category', $append );
}

1 个答案:

答案 0 :(得分:0)

WooCommerce产品的发布类型为product,但不是post,“产品类别”的分类法不是category,而是product_cat,因为它是自定义分类法用于product自定义帖子类型…

因此,如果要对产品的WooCommerce产品类别使用功能,请尝试以下操作:

function append_product_categories( $product_id, $term_ids, $append = true ) {
    $product_id  = (int) $product_id;
    $post_type   = get_post_type( $product_id );
    $post_status = get_post_status( $product_id );
    $term_ids    = (array) $term_ids;

    if ( empty( $term_ids ) ) {
        if ( 'product' == $post_type && 'auto-draft' != $post_status ) {
            $term_ids = array( get_option( 'default_product_cat' ) );
            $append   = true;
        } else {
            $term_ids = array();
        }
    } elseif ( 1 == count( $term_ids ) && '' == reset( $term_ids ) ) {
        return true;
    }

    // Check for existing term id in the product | Check if term exist in Woocommerce
    foreach( $term_ids as $key => $term_id ) {
        if( hast_term( $term_id, 'product_cat', $product_id ) || ! term_exists( $term_id, 'product_cat' ) ) {
            unset($term_ids[$key]); // remove term id from the array
        }
    }

    return wp_set_post_terms( $product_id, $term_ids, 'product_cat', $append );
}

它应该更适合WooCommerce产品类别(未试用)