2
votes

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?

2
it's not template to just copy it in other folder and expect it to work. what part you changed? woocommerce is very changeable plugin and you can use actions/hooks to achieve your goalSamvel Aleqsanyan
I tried to change the following function : private function add_crumbs_single( $post_id = 0, $permalink = '' ) The goal is to change all links off categories Woocommerce by a static page where i'm showing productsAlbertoV

2 Answers

9
votes

It is NOT possible to change cores files via your theme and it is strictly prohibited to do it for many reasons.

Instead you have 2 options:

  1. Use any available action and filter hooks for WooCommerce breadcrumbs customizations.
  2. Customizing WooCommerce template global/breadcrumb.php via your active child theme

The 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.

1
votes

There are numerous option to change the option..

For this

add_filter( 'woocommerce_breadcrumb_defaults', 'jk_woocommerce_breadcrumbs' );
function jk_woocommerce_breadcrumbs() {
return array(
        'delimiter'   => ' / ',
        'wrap_before' => '<nav class="woocommerce-breadcrumb" itemprop="breadcrumb">',
        'wrap_after'  => '</nav>',
        'before'      => '',
        'after'       => '',
        'home'        => _x( 'Home', 'breadcrumb', 'woocommerce' ),
    );
}

You can change all your content via this.

For more information :- https://docs.woocommerce.com/document/customise-the-woocommerce-breadcrumb/