Woocommerce Multiple Categories and assigning URL to primary category.
-
I have a problem with my site’s product URL’s and Primary Categories with products assigned to multiple categories.
I am using the following:
? Divi Theme
Within the theme builder I have created multiple category pages.
So e.g.
1. Commercial & Industrial (mining header image), the products that is assigned to this category also have the mining header image as set by the category page.
2. Farming & Agricultural (tractor header image), the products that is assigned to this category also have the tractor header image as set by the category page.? Yoast SEO Pro
? WP All Import Pro + Yoast SEO Pro add onThe issue:
I have products that is assigned to multiple main categories BUT each product belongs to ONE PRIMARY category. I can set within my import file in WP All Import a primary category BUT WP still assigns the products to RANDOM categories!I have googled and found the code below to FORCE the URL and Breadcrumb to display the primary category (code pasted in my child theme function.php) BUT the category page set in Divi Builder still picks up the site is assigning the product to the RANDOM category even if the Breadcrumb and URL displays correct.
This causes the product to display URL & Breadcrumb -> Commercial & Industrial but the category page loading is Farming & Agricultural page example. What I can understand is get_the_category assigns the product to the category with the nearest ID and NOT THE PRIMARY CATEGORY as I set it.
I also tried to use the import file as simple as possible – WP All Import gives the option that you can list all your categories and set the file to assign the product to the last category in the hierarchy (so I set my primary category last) – but once again WP assigns the products to what ever it feels like and ignore the this setting.
I am not a developer, Im a website Designer – please help to make the site assign the product to the primary category so that it can display the correct category template of Divi.
If someone can please assist I have a deadline and are overwhelmed with this issue.
Herewith the code:
To Force the URL to display the Primary Category in the URL// FIX PERMALINK OF CATEGORIES MATCHING PRIMARY CATEGORY add_filter( 'wc_product_post_type_link_product_cat', function( $term, $terms, $post ) { // Get the primary term as saved by Yoast $primary_cat_id = get_post_meta( $post->ID, '_yoast_wpseo_primary_product_cat', true ); // If there is a primary and it's not currently chosen as primary if ( $primary_cat_id && $term->term_id != $primary_cat_id ) { // Find the primary term in the term list foreach ( $terms as $term_key => $term_object ) { if ( $term_object->term_id == $primary_cat_id ) { // Return this as the primary term $term = $terms[ $term_key ]; break; } } } return $term; }, 10, 3 );
To force the breadcrumb to display the primary category
add_filter('woocommerce_breadcrumb_main_term', function($main, $terms) { $url_cat = get_query_var( 'product_cat', false ); if ($url_cat) { foreach($terms as $term) { if (preg_match('/'.$term->slug.'$/', $url_cat)) { return $term; } } } return $main; }, 100, 2);
The page I need help with: [log in to see the link]
- The topic ‘Woocommerce Multiple Categories and assigning URL to primary category.’ is closed to new replies.