How to remove product categories from breadcrumb on WooCommerce single product

CoderAuthor Position

This is a static text. Maybe you want to add a tagline or short message here?

remove product categories from breadcrumb on WooCommerce
remove product categories from breadcrumb on WooCommerce

Discover a straightforward guide for developers and WooCommerce store creators on removing product categories from breadcrumbs on single product pages. Enhance user navigation and streamline your site’s structure with our easy-to-follow tips and tricks. Perfect for programmers looking to customize and optimize their WooCommerce store’s user experience

// remove only the category from woocommerce breadcrumbs
add_filter( 'woocommerce_get_breadcrumb', 'custom_breadcrumb', 20, 2 );
function custom_breadcrumb( $crumbs, $breadcrumb ) {
//print the array and look for the key with the category
//echo '<pre>';
//print_r($crumbs);
//echo '</pre>';
//unset($crumbs[2]); in my case it is key 2
 
// only on the single product page
if ( ! is_product() ) {
return $crumbs;
} else {
 
unset($crumbs[2]); // this isn't enough, it would leave a trailing delimiter
$newBreadC = array_values($crumbs); //therefore create new array
 
return $newBreadC; //return the new array
}
}
 
 
 
// change the breadcrumb on the product page
add_filter( 'woocommerce_get_breadcrumb', 'custom_breadcrumb', 20, 2 );
function custom_breadcrumb( $crumbs, $breadcrumb ) {
 
// only on the single product page
if ( ! is_product() ) {
return $crumbs;
}
 
// gets the first element of the array "$crumbs"
$new_crumbs[] = reset( $crumbs );
// gets the last element of the array "$crumbs"
$new_crumbs[] = end( $crumbs );
 
return $new_crumbs;
 
}
 
/**
* @snippet Swap Product with SKU @ WooCommerce Breadcrumb
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @testedwith WooCommerce 3.9
* @community https://businessbloomer.com/club/
*/
 
add_filter( 'woocommerce_get_breadcrumb', 'bbloomer_single_product_edit_prod_name_breadcrumbs', 9999, 2 );
 
function bbloomer_single_product_edit_prod_name_breadcrumbs( $crumbs, $breadcrumb ) {
 
if ( is_product() ) {
global $product;
$index = count( $crumbs ) - 1; // product name is always last item
$value = $crumbs[$index];
$crumbs[$index][0] = $product->get_sku();
}
 
return $crumbs;
}

 

If you looking – how to remove breadcrumb from woocommerce product page, its can be next:

// Remove breadcrumbs from shop & categories
add_filter( 'woocommerce_before_main_content', 'remove_breadcrumbs');
function remove_breadcrumbs() {
if(!is_product()) {
remove_action( 'woocommerce_before_main_content','woocommerce_breadcrumb', 20, 0);
}
}
 
// Remove breadcrumbs only from shop page
add_filter( 'woocommerce_before_main_content', 'remove_breadcrumbs');
function remove_breadcrumbs() {
if(!is_product() && !is_product_category()) {
remove_action( 'woocommerce_before_main_content','woocommerce_breadcrumb', 20, 0);
}
}
 
//Or you can also remove by page id or page slug:
add_action('template_redirect', 'remove_page_breadcrumbs' );
function remove_page_breadcrumbs(){
if (is_page('YOUR_PAGE_ID_OR_SLUG'))
remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0);
}
  
 /**
 * Remove WooCommerce breadcrumbs 
 */
add_action( 'init', 'my_remove_breadcrumbs' );
  
function my_remove_breadcrumbs() {
    remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0 );
}

 

Doc – https://woo.com/document/customise-the-woocommerce-breadcrumb/