Customizing product categories breadcrumb links in Woocommerce via the theme

15,752

Solution 1

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.

Solution 2

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/

Share:
15,752

Related videos on Youtube

AlbertoV
Author by

AlbertoV

Simple webdev wordpress and prestashop who want to learn and share my knowledges

Updated on June 04, 2022

Comments

  • AlbertoV
    AlbertoV over 1 year

    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?

    • Samvel Aleqsanyan
      Samvel Aleqsanyan over 5 years
      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 goal
    • AlbertoV
      AlbertoV over 5 years
      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 products
  • AlbertoV
    AlbertoV over 5 years
    Thank you, I read this page, but what's happen if I do updates ? Do my code will not be erased ?
  • sagar
    sagar over 5 years
    you need to perform this in child theme and there is 'woocommerce_breadcrumb_defaults' as filter which you can modify according to your needs
  • AlbertoV
    AlbertoV over 5 years
    Awesome, it was exactly what i needed. I add the following code into the function :