Warning: include_once(/home4/aquapan/public_html/cjbarnaby/wp-content/plugins/wp-super-cache/wp-cache-phase1.php): failed to open stream: No such file or directory in /home/revela25/public_html/cjbarnaby/wp-content/advanced-cache.php on line 22

Warning: include_once(): Failed opening '/home4/aquapan/public_html/cjbarnaby/wp-content/plugins/wp-super-cache/wp-cache-phase1.php' for inclusion (include_path='.:/opt/cpanel/ea-php74/root/usr/share/pear') in /home/revela25/public_html/cjbarnaby/wp-content/advanced-cache.php on line 22

Warning: include(/home4/aquapan/public_html/cjbarnaby/wp-content/plugins/wp-super-cache/wp-cache-base.php): failed to open stream: No such file or directory in /home/revela25/public_html/cjbarnaby/wp-content/plugins/wp-super-cache/wp-cache.php on line 137

Warning: include(): Failed opening '/home4/aquapan/public_html/cjbarnaby/wp-content/plugins/wp-super-cache/wp-cache-base.php' for inclusion (include_path='.:/opt/cpanel/ea-php74/root/usr/share/pear') in /home/revela25/public_html/cjbarnaby/wp-content/plugins/wp-super-cache/wp-cache.php on line 137

Warning: include_once(/home4/aquapan/public_html/cjbarnaby/wp-content/plugins/wp-super-cache/ossdl-cdn.php): failed to open stream: No such file or directory in /home/revela25/public_html/cjbarnaby/wp-content/plugins/wp-super-cache/wp-cache.php on line 174

Warning: include_once(): Failed opening '/home4/aquapan/public_html/cjbarnaby/wp-content/plugins/wp-super-cache/ossdl-cdn.php' for inclusion (include_path='.:/opt/cpanel/ea-php74/root/usr/share/pear') in /home/revela25/public_html/cjbarnaby/wp-content/plugins/wp-super-cache/wp-cache.php on line 174
Copy Post Categories To Woocommerce Categories - cjbarnaby

Copy Post Categories To Woocommerce Categories

This was a time saver thanks to AI helper. Use at your own risk.  
				
					

// category post to product copy specific id's

function copy_specific_post_categories_to_product_categories() {
    $args = array(
        'taxonomy' => 'category',
        'orderby' => 'name',
        'order' => 'ASC',
        'hide_empty' => false,
        'include' => array(76,77,78,79,80,81,82,83,87,88,89,90,91,91,92,93,94,95) // Add the category IDs you want to copy here
    );

    $post_categories = get_categories($args);

    foreach ($post_categories as $post_category) {
        $parent_id = $post_category->parent;
        $cat_args = array(
            'name' => $post_category->name,
            'description' => $post_category->description,
            'slug' => $post_category->slug,
            'parent' => 0,
        );
        $cat_id = wp_insert_term($post_category->name, 'product_cat', $cat_args);

        if (!is_wp_error($cat_id)) {
            if ($parent_id > 0) {
                $parent_term = get_term_by('id', $parent_id, 'product_cat');
                if ($parent_term) {
                    wp_update_term($cat_id['term_id'], 'product_cat', array('parent' => $parent_term->term_id));
                }
            }
        }
    }
}

add_action( 'init', 'copy_specific_post_categories_to_product_categories' );
//


				
			

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment