1
votes

Information:
1. Using Wordpress and WooCommerce, alongside the Genesis theme (+ Genesis Connect for WooCommerce plugin)
2. Shop archive page is /Shop
3. Breadcrumbs are activated for all pages (e.g. Shop, Category/Archive and Single Product pages)
4. "Products" (permalink: /Products) is a separate Wordpress page (not a category)

Trying to achieve the following Breadcrumb Conditions:
1. Add "Products" crumb (name: Products, link: /products) right after "Home" crumb (e.g. Home > Products > Category > Single Product)
2. Do not show "Products" crumb if inside Shop page (e.g. Home > Shop)

I have achieved a functioning result using the following code:

/*
 * Add "Products" to breadcrumb (ex. Home > Products > etc), but do not show "Products" crumb in Shop page (ex. Home > Shop)...
 */

add_filter( 'woocommerce_breadcrumb_defaults', 'custom_woocommerce_breadcrumbs' );
function custom_woocommerce_breadcrumbs() {
    $home = get_home_url();
    $home_name = (is_shop()) ? 'Home' : 'Products';
    $home_encoded_link = (is_shop()) ? '' : '<a href="' . $home . '">Home</a> &raquo; ';

    return array(
            'delimiter'   => ' &raquo; ',
            'wrap_before' => '<nav class="woocommerce-breadcrumb" itemprop="breadcrumb">' . $home_encoded_link,
            'wrap_after'  => '</nav>',
            'before'      => '',
            'after'       => '',
            'home'        => _x( $home_name, 'breadcrumb', 'woocommerce' ),
        );
}
add_filter( 'woocommerce_breadcrumb_home_url', 'custom_breadcrumb_home_url' );
function custom_breadcrumb_home_url() {
    $home = get_home_url();
    return $home . '/products/';
}

The question is: Is there a better way to do this? My solution feels a little "hacky" to me and I was wondering if I'm overlooking an obvious & smarter way to achieve the results I want.

2

2 Answers

0
votes

There is a short way to do it.

 add_filter( 'woocommerce_get_breadcrumb', 'remove_shop_crumb', 20, 2 );
function remove_shop_crumb( $crumbs, $breadcrumb ){
    foreach( $crumbs as $key => $crumb ){
        if( $crumb[0] === __('Home') ) {

            array_push($crumbs," /customlink/ ");
       }
    }

    return $crumbs;
}

I havent had a chance to test it yet so you may need to play around with $crumbs[0] to find the correct placement and not sure if you will need the forward slashes surrounding your link or not.

0
votes

To add a custom link after home we can use the array_splice function

add_filter('woocommerce_get_breadcrumb', 'custom_breadcrumb', 20, 2);
function custom_breadcrumb($crumbs, $breadcrumb)
{
    if (!empty($crumbs)) {
        array_splice($crumbs, 1, 0, array(array(
            'Custom Link',
            'http://localhost/mysite/custom-link/'
        )));
    }

    return $crumbs;
}

This is the structure of the variable $crumbs

array(2)
0:array(2)
    0:"Home"
    1:"http://localhost/mysite"
1:array(2)
    0:"Shop"
    1:"http://localhost/mysite/shop/"

And that is why I use array twice, so that the result will be the following

array(3)
0:array(2)
    0:"Home"
    1:"http://localhost/mysite"
1:array(2)
    0:"Custom Link"
    1:"http://localhost/mysite/custom-link/"
2:array(2)
    0:"Shop"
    1:"http://localhost/mysite/shop/"