简体   繁体   English

在Woocommerce中根据产品标签自动将产品分配到类别

[英]Automatically assign products to categories based on their product tags in Woocommerce

I currently have a function which successfully adds products to Woocommerce using wp_insert_post(). 我目前有一个使用wp_insert_post()成功将产品添加到Woocommerce的功能。

I'm now trying to assign products to relevant categories based on their product tags. 我现在正尝试根据产品标签将产品分配到相关类别。 For example, if the product is added with the product tags 'ring' or 'necklace' then it is auto assigned to the 'Jewellery' category. 例如,如果添加了带有“ ring”或“ necklace”产品标签的产品,则会自动将其分配给“珠宝”类别。

Using the following function, I'm able to achieve the correct functionality on posts, but have had no luck in attempting to make this work for the products post type used in woocommerce. 使用以下功能,我能够在帖子上实现正确的功能,但是对于woocommerce中使用的产品帖子类型尝试使此功能不起作用。

Works for posts: 职位作品:

function auto_add_category ($post_id = 0) {
 if (!$post_id) return;
 $tag_categories = array (
   'ring' => 'Jewellery',
   'necklace' => 'Jewellery',
   'dress' => 'Clothing',
 );
 $post_tags = get_the_tags($post_id);
 foreach ($post_tags as $tag) {
   if ($tag_categories[$tag->name] ) {
     $cat_id = get_cat_ID($tag_categories[$tag->name]);
     if ($cat_id) {
       $result =  wp_set_post_terms( $post_id, $tags = $cat_id, $taxonomy = 'category', $append = true );
     }
   }
   }
 }
 add_action('publish_post','auto_add_category');


I've tried to repurpose the code to work for products as follows: 我试图将代码重新用于产品,如下所示:

function auto_add_category ($product_id = 0) {
    if (!$product_id) return;
 $tag_categories = array (
    'ring' => 'Jewellery'
    'necklace' => 'Jewellery',
    'dress' => 'Clothing',
 );
 $product_tags = get_terms( array( 'taxonomy' => 'product_tag') );
 foreach ($product_tags as $tag) {
    if ($tag_categories[$tag->name] ) {
        $cat = get_term_by( 'name', $tag_categories[$tag->name], 'product_cat' );
        $cat_id = $cat->term_id;
        if ($cat_id) {
            $result =  wp_set_post_terms( $product_id, $tags = $cat_id, $taxonomy = 'product_cat', $append = true );
        }
    }
 }
}
add_action('publish_product','auto_add_category');


However, it doesn't assign the relevant categories upon product creation. 但是,它不会在创建产品时分配相关类别。 Any assistance would be greatly appreciated by this novice coder muddling his way through wordpress! 这位新手编码员在wordpress上摸索自己的方式,将对您的帮助非常感激!

Try this code: 试试这个代码:

function auto_add_category ($product_id = 0) {

    if (!$product_id) return;

    // because we use save_post action, let's check post type here
    $post_type = get_post_type($post_id);
    if ( "product" != $post_type ) return;

    $tag_categories = array (
        'ring' => 'Jewellery'
        'necklace' => 'Jewellery',
        'dress' => 'Clothing',
    );

    // get_terms returns ALL terms, so we have to add object_ids param to get terms to a specific product
    $product_tags = get_terms( array( 'taxonomy' => 'product_tag', 'object_ids' => $product_id ) );
    foreach ($product_tags as $term) {
        if ($tag_categories[$term->slug] ) {
            $cat = get_term_by( 'name', $tag_categories[$term->slug], 'product_cat' );
            $cat_id = $cat->term_id;
            if ($cat_id) {
                $result =  wp_set_post_terms( $product_id, $cat_id, 'product_cat', true );
            }
        }
    }
}
add_action('save_post','auto_add_category');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在导入时自动从产品标签中分配Woocommerce产品类别 - Auto assign Woocommerce Product Categories from Product Tags on import 根据产品Woocommerce / PHP的标签显示产品类别 - Display Product Categories based on Tags of a Product Woocommerce/PHP WooCommerce - 按标签和类别分类的相关产品 - WooCommerce - related products by tags and categories 根据产品类别自动添加WooCommerce优惠券代码 - Add WooCommerce coupon code automatically based on product categories 使用 Woocommerce 产品创建下拉菜单,并根据选择自动将产品添加到购物车 - Creating dropdown with Woocommerce products and automatically add product to cart based on selection WooCommerce单品有子品类时按品类展示内容 - Display content on WooCommerce single products based on product categories when they have child categories 在产品上附加WooCommerce产品类别而不是覆盖 - Append WooCommerce product categories on products instead of overwriting 相关产品仅按类别分类,不按WooCommerce 3中的标签分类 - Related Products only by categories, not by tags in WooCommerce 3 在 WooCommerce 3 中仅按标签相关产品,而不是按类别 - Related Products only by Tags, Not by Categories in WooCommerce 3 WooCommerce从产品/类别中删除包装标签 - WooCommerce removing wrapping tags from products/categories
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM