Created
March 24, 2020 09:06
-
-
Save blogjunkie/b9bc36bf4604e7ab44609c1023c92ecc to your computer and use it in GitHub Desktop.
Add Yoast Primary Category to WooCommerce Breadcrumbs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Add Yoast Primary Category to WooCommerce Breadcrumbs | |
* | |
* Uses the `woocommerce_get_breadcrumb` filter | |
*/ | |
add_filter( 'woocommerce_get_breadcrumb', 'probb_filter_wc_crumbs', 10, 2 ); | |
function probb_filter_wc_crumbs( $crumbs, $object ) | |
{ | |
//* Checking Single Product Page | |
if( is_singular('product') ) | |
{ | |
//* Do nothing if shop page set as static front page | |
if ( get_option( 'page_on_front' ) == wc_get_page_id( 'shop' ) ) | |
{ | |
return $crumbs; | |
} | |
$permalinks = get_option( 'woocommerce_permalinks' ); //* getting premalink structure | |
$arr_shift = array_shift( $crumbs ); //* removing the first element "Home" | |
$new_crumbs = array(); | |
//* Premalink is default then adding the shop page title into crumbs array | |
if ( empty( $permalinks['product_base'] ) ) | |
{ | |
$_name = wc_get_page_id( 'shop' ) ? get_the_title( wc_get_page_id( 'shop' ) ) : ''; | |
if ( ! $_name ) | |
{ | |
$product_post_type = get_post_type_object( 'product' ); | |
$_name = $product_post_type->labels->singular_name; | |
} | |
$new_crumbs = array( array( $_name, get_post_type_archive_link( 'product' ) ) ); | |
} | |
/** | |
* Checking Yoast Plugin active | |
* If it is there, then fetching the primary category name and | |
* adding into crumbs array | |
*/ | |
if ( class_exists( 'WPSEO_Breadcrumbs' ) ) | |
{ | |
global $post; | |
$terms = get_the_terms( $post, 'product_cat' ); | |
if ( is_array( $terms ) && $terms !== array() ) { | |
$primary_term = new WPSEO_Primary_Term( 'product_cat', get_the_ID() ); | |
if ( $primary_term->get_primary_term() ) { | |
if ( ! empty( $permalinks['product_base'] ) ) | |
{ | |
$new_crumbs[] = array_shift( $crumbs ); | |
} | |
$breadcrumb_term = get_term( $primary_term->get_primary_term(), 'product_cat' ); | |
$ancestors = get_ancestors( $breadcrumb_term->term_id, 'product_cat' ); | |
$ancestors = array_reverse( $ancestors ); | |
foreach ( $ancestors as $ancestor ) { | |
$ancestor = get_term( $ancestor, 'product_cat' ); | |
if ( ! is_wp_error( $ancestor ) && $ancestor ) { | |
$new_crumbs[] = array( $ancestor->name, get_term_link( $ancestor ) ); | |
} | |
} | |
$new_crumbs[] = array( $breadcrumb_term->name, get_term_link( $breadcrumb_term ) ); | |
$last_element = array_pop( $crumbs); | |
$crumbs = array_merge( array($arr_shift), $new_crumbs, array($last_element) ); | |
return $crumbs; | |
} | |
} | |
} | |
//* Making the new crumbs array | |
$crumbs = array_merge( array($arr_shift), $new_crumbs, $crumbs ); | |
} | |
//* Returning the crumbs array for process | |
return $crumbs; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment