I am trying to modify class-wc-breadcrumb.php
to customize the product category links in the breadcrumb of my products pages.
This file is located in : wp-content/plugins/woocommerce/includes
I tried to copy and edit this file in my child theme into:
wp-content/themes/Divi-child/woocommerce/includes/lass-wc-breadcrumb.php
But it doesn't work.
How to customize product categories breadcrumb links in Woocommerce via my child theme?
It is NOT possible to change cores files via your theme and it is strictly prohibited to do it for many reasons.
global/breadcrumb.php
via your active child themeThe best choice in your case is the 1st option. In the code below you will be able to customize each link of Woocommerce product categories links.
In the code below, I use a foreach loop to iterate through each $crumb
. Each $crumb
is an array where:
$crumb[0]
is the displayed label name$crumb[1]
is the link (or the url) that will be changed using $crumbs[$key][1]
…We will check that the current $crumb[0]
is a product category name before allowing to customize it's link in $crumbs[$key][1]
.
You will have to set your new links for each product category adding your own necessary code to this filtered hooked function example.
The code:
add_filter( 'woocommerce_get_breadcrumb', 'custom_breadcrumb', 10, 2 );
function custom_breadcrumb( $crumbs, $object_class ){
// Loop through all $crumb
foreach( $crumbs as $key => $crumb ){
$taxonomy = 'product_cat'; // The product category taxonomy
// Check if it is a product category term
$term_array = term_exists( $crumb[0], $taxonomy );
// if it is a product category term
if ( $term_array !== 0 && $term_array !== null ) {
// Get the WP_Term instance object
$term = get_term( $term_array['term_id'], $taxonomy );
// HERE set your new link with a custom one
$crumbs[$key][1] = home_url( '/'.$term->slug.'/' ); // or use all other dedicated functions
}
}
return $crumbs;
}
Code goes in function.php file of the active child theme (or active theme).
Tested and works.