• Resolved rkapps0

    (@rkapps0)


    
    add_action('save_post', 'xx_assign_parent_terms');
    add_action('save_post_product', 'xx_assign_parent_terms');
    add_action('woocommerce_update_product', 'xx_assign_parent_terms');
    
    function xx_assign_parent_terms($post_id){
    
    global $post;
    
    if(isset($post) && $post->post_type != 'product')
        return $post_id;
    
    // get all assigned terms   
    $terms = wp_get_post_terms($post_id, 'product_cat' );
    foreach($terms as $term){
        while($term->parent != 0 && !has_term( $term->parent, 'product_cat', $post )){
            // move upward until we get to 0 level terms
            wp_set_object_terms($post_id, array($term->parent), 'product_cat', true);
            $term = get_term($term->parent, 'product_cat');
        }
      }
    }
    
    

    I am using this code to add parent terms when a product category is updated. It works on default WP product create/edit pages. It also works on BEAR when category of a SINGLE product is modified.
    It does NOT work when category is modified for multiple products.

    Is there a different hook I should be using?

Viewing 7 replies - 1 through 7 (of 7 total)
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Custom Hook save_post’ is closed to new replies.