Skip to content

Instantly share code, notes, and snippets.

@hereswhatidid
Last active September 21, 2023 20:59
Show Gist options
  • Save hereswhatidid/fb9152a11bc359fa32772e4d18f01c9a to your computer and use it in GitHub Desktop.
Save hereswhatidid/fb9152a11bc359fa32772e4d18f01c9a to your computer and use it in GitHub Desktop.
Integrate Yoast SEO Primary Categories with WooCommerce breadcrumbs
<?php
add_filter( 'woocommerce_breadcrumb_main_term', 'hwid_breadcrumbs_primary_category' );
/**
* Override the default term for WooCommerce cookies if Yoast SEO is installed
*
* @param WP_Term $term
*
* @return WP_Error|WP_Term|null
*/
function hwid_breadcrumbs_primary_category( $term ) {
// check to see if the `yoast_get_primary_term_id` function exists
if ( function_exists( 'yoast_get_primary_term_id' ) ) {
// check if a primary category has been chosen and, if so, return it
if ( $primary_id = yoast_get_primary_term_id( 'product_cat' ) ) {
return get_term( $primary_id, 'product_cat' );
}
}
return $term;
}
@Windaiv
Copy link

Windaiv commented Feb 3, 2023

add_filter( 'woocommerce_product_categories_widget_product_terms_args', 'wp_kama_woocommerce_product_categories_widget_terms_args_filter' );

function wp_kama_woocommerce_product_categories_widget_terms_args_filter( $term ){
// check to see if the yoast_get_primary_term_id function exists
if ( function_exists( 'yoast_get_primary_term_id' ) ) {
// check if a primary category has been chosen and, if so, return it
if ( $primary_id = yoast_get_primary_term_id( 'product_cat' ) ) {
return get_term( $primary_id, 'product_cat' );
}
}

return $term;
}

@AlexGxtr
Copy link

This is not working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment