[英]Append WooCommerce product categories on products instead of overwriting
I am trying to force woocommerce to append product categories instead of overwriting when uploading new categories via csv. 我试图强迫woocommerce在通过csv上传新类别时附加产品类别而不是覆盖。
I have tried finding code snippets.searched the codex and tried to use wp-includes/post.php to create function. 我已经尝试找到代码片段。搜索了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 );
}
I expect woocommerce product categories to be appended rather than overwritten. 我希望附加而不是覆盖woocommerce产品类别。
actual result 实际结果
fatal error on line 29: Cannot redeclare wp_set_post_terms() (previously declared in /var/www/html/wp-includes/post.php:4108) 第29行致命错误:无法重新声明wp_set_post_terms()(之前在/var/www/html/wp-includes/post.php:4108中声明)
tried this code. 试过这段代码。 no errors but does not append categories 没有错误但不附加类别
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 );
}
The post type for WooCommerce products is product
but not post
and the taxonomy for "product category" is not category
but product_cat
instead as it is a custom taxonomy for product
custom post type… WooCommerce产品的帖子类型是product
但不是post
,“产品类别”的分类不是category
而是product_cat
因为它是product
自定义帖子类型的自定义分类...
So if you want to use your function for WooCommerce product category on products, try this: 因此,如果您想在产品上使用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 );
}
It should better works for WooCommerce Product categories (untested) 它应该更适用于WooCommerce产品类别(未经测试)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.