0
votes

I'm redesigning my website and am having trouble with the breadcrumbs. I would like to replace the product category with the page number which the currently selected product originated from. So instead of: Home > Shop > abstract > product name I would like: Home > Shop > Page 3 > product name

The reason for this is my site is used for displaying/selling my art and the product category in the breadcrumb just doesn't make sense. I would like the customer to be able to return to the page they came from and not a new page of products that happen to be in the same category.

My site is https://dev.paulruskin.com and I'm using the Storefront theme in WordPress.

I have searched extensively on here and googled the heck out of it but can't seem to find a good enough example.

Any help will be greatly appreciated.

EDIT:
So this is what I came up with so far.

add_filter( 'woocommerce_get_breadcrumb', 'custom_breadcrumb', 10, 2 );
function custom_breadcrumb( $crumbs ) {
    $url = $_SERVER['HTTP_REFERER'];
    $url_array = array_values( explode("/", $url) );
    $pg_num = $url_array[count($url_array) -2];

    if ( strpos( $url, '/page/') !== false ) {

        foreach( $crumbs as $key => $crumb ) {
            $taxonomy = 'product_cat';

            $term_array = term_exists( $crumb[0], $taxonomy );

            if ( $term_array !== 0 && $term_array !== null ) {
                $crumbs[$key][0] = "Page " . $pg_num;
                $crumbs[$key][1] = $url;
            }
        }
    }
    return $crumbs;
}'  

This only partially works. What I realized is if the user clicks the prev or next buttons then the previous url won't still list the page number.

Is there a way to get the page number from Woocommerce or WordPress that a product belongs to?

1
Do you mean go back to the previous page which had pagination from search where you selected a product to view? - fja3omega
@fja3omega, Yes, I want the breadcrumbs to list the path the user took to get to a specific product page. For instance, on my shop page there are 20 pages. If a user is on page 12 and chooses to click on a product the breadcrumbs will show Home > Shop > Expressionism > Product Title which if they clicked either Shop or Expressionism would take them to a page they didn't originate from. I would like to change the breadcrumbs to Home > Shop > Page 12 > Product Title so they can return to the page they came from and not a new search result. - PaulR

1 Answers

0
votes

You could add a link on top of the breadcrumbs to go back to previous page:

<a href="#" onclick="window.history.go(-1); return false;">Back to previous page</a>

This is the same as clicking the browsers back button.