簡體   English   中英

在產品上附加WooCommerce產品類別而不是覆蓋

[英]Append WooCommerce product categories on products instead of overwriting

我試圖強迫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 );
}

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產品類別(未經測試)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM