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

问题描述 投票:0回答:1

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

我已经尝试找到代码片段。搜索了codex并尝试使用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 );
}
php wordpress woocommerce custom-taxonomy taxonomy-terms
1个回答
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产品类别(未经测试)

© www.soinside.com 2019 - 2024. All rights reserved.