最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

woocommerce offtopic - Set post categories to include parents when setting child category

programmeradmin0浏览0评论

I have a WooCommerce setup and when I create a new product, I assign the product a category as normal.

Clothing
- Womens
-- Accessories
- Mens
-- Accessories <-- assigned to this term

What I am trying to achieve:
When I choose a child category, I want to also set that post to every parent category direclty above it. In this case, that would look like:

Clothing <-- assigned to this term
- Womens
-- Accessories
- Mens <-- assigned to this term
-- Accessories <-- assigned to this term

Note: Most of my products are created from the front-end by other users, so I can't just select the other boxes, I know that is an option.

My attempt so far:

function set_product_parent_categories( $post_id ) {

  $term_ids = wp_get_post_terms( $post_id, 'product_cat' );

  foreach( $term_ids as $term_id ) {

    if( $term_id->parent > 0 ) {

      // Assign product to the parent category too.
      wp_set_object_terms( $post_id, $term_id->parent, 'product_cat' );

    }

  }

}
add_action( 'woocommerce_update_product', __NAMESPACE__.'\\set_product_parent_categories', 10, 1 );

This sets the top parent term only.

I have a WooCommerce setup and when I create a new product, I assign the product a category as normal.

Clothing
- Womens
-- Accessories
- Mens
-- Accessories <-- assigned to this term

What I am trying to achieve:
When I choose a child category, I want to also set that post to every parent category direclty above it. In this case, that would look like:

Clothing <-- assigned to this term
- Womens
-- Accessories
- Mens <-- assigned to this term
-- Accessories <-- assigned to this term

Note: Most of my products are created from the front-end by other users, so I can't just select the other boxes, I know that is an option.

My attempt so far:

function set_product_parent_categories( $post_id ) {

  $term_ids = wp_get_post_terms( $post_id, 'product_cat' );

  foreach( $term_ids as $term_id ) {

    if( $term_id->parent > 0 ) {

      // Assign product to the parent category too.
      wp_set_object_terms( $post_id, $term_id->parent, 'product_cat' );

    }

  }

}
add_action( 'woocommerce_update_product', __NAMESPACE__.'\\set_product_parent_categories', 10, 1 );

This sets the top parent term only.

Share Improve this question asked Apr 26, 2020 at 12:03 wharfdalewharfdale 3484 silver badges17 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

This will check to see if there is a hierarchy and set all parent categories as checked. This function assumes that if there's multiple categories already set, then don't do the function, since the categories would be correct if multiples are set.

function set_product_parent_categories( $post_id ) {
    $category = wp_get_post_terms( $post_id, 'product_cat' );
    // If multiple categories are set. Bail Out
    if (count ($category) > 1 ) return;

    $terms = array($category[0]->term_id);
    if ($category[0]->parent > 0){
        $parent = $category[0]->parent;
        while ($parent > 0){
            // Make an array of all term ids up to the parent.
            $terms[] = $parent;
            $grandpa = get_term($parent, 'product_cat');
            $parent = $grandpa->parent;
        }
    }
    // If multiple terms are returned, update the object terms
    if (count($terms) > 1) wp_set_object_terms( $post_id, $terms, 'product_cat' );
}
add_action( 'woocommerce_update_product', 'set_product_parent_categories', 10, 1 );
发布评论

评论列表(0)

  1. 暂无评论