【问题标题】:Automatically assign products to categories based on their product tags in Woocommerce根据 Woocommerce 中的产品标签自动将产品分配到类别
【发布时间】:2018-03-26 22:43:38
【问题描述】:

我目前有一个函数可以使用 wp_insert_post() 成功地将产品添加到 Woocommerce。

我现在正在尝试根据产品标签将产品分配到相关类别。例如,如果产品添加了产品标签“戒指”或“项链”,那么它会自动分配到“珠宝”类别。

使用以下功能,我能够在帖子上实现正确的功能,但在尝试使该功能适用​​于 woocommerce 中使用的产品帖子类型时没有运气。

适用于帖子:

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');


我已尝试将代码重新用于产品,如下所示:

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');


但是,它不会在产品创建时分配相关类别。这位新手编码员在 wordpress 中糊里糊涂,任何帮助都将不胜感激!

【问题讨论】:

    标签: php wordpress function woocommerce


    【解决方案1】:

    试试这个代码:

    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');
    

    【讨论】:

    • 谢谢!这在通过管理后端添加产品时效果很好,但是在以编程方式添加产品时它似乎并没有触发,就像我正在做的那样。我猜这是因为在保存帖子后添加了对象术语(产品标签)?我尝试使用操作“set_object_terms”而不是“save_post”,这似乎有效,但只添加了与第一个标签相关的类别。有什么想法吗?
    • 等等,你说你正在以编程方式进行。在这种情况下,根本不需要使用过滤器。只需在 wp_insert_post() 使用后的某处添加代码即可。
    • 当然——现在你已经说得很有道理了。不知道为什么我一开始没有考虑这样做!哦,好吧 - 非常感谢您的帮助!
    猜你喜欢
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-25
    • 2016-03-20
    • 1970-01-01
    • 2021-07-18
    • 2022-11-05
    相关资源
    最近更新 更多