Skip to content

Instantly share code, notes, and snippets.

@attitude
Last active October 28, 2020 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save attitude/f29ec65d5245b4624f0a21bc9a75b636 to your computer and use it in GitHub Desktop.
Save attitude/f29ec65d5245b4624f0a21bc9a75b636 to your computer and use it in GitHub Desktop.
Use less confusing links for the WooCommerce built-in filters that always point to the base of the shop, the `shop` page.
<?php
// NOTE: This filter has no effect on "Active Filters" widget
add_filter('woocommerce_layered_nav_link', function(string $link) {
static $shopPageId, $shopPermalink, $silent;
if (!isset($silent)) {
$silent = wp_get_environment_type() === 'production';
}
if (!isset($shopPageId)) {
$shopPageId = wc_get_page_id('shop');
}
if ($shopPageId === -1) {
if (!$silent) {
trigger_error('Skipping: WooCommerce Shop page is not set. Could not change the filter links.');
}
return $link;
}
if (!isset($shopPermalink)) {
$shopPermalink = get_permalink($shopPageId);
}
if (!$shopPermalink) {
if (!$silent) {
throw new \Exception('Unable to get Shop permalink. Please investigate.', 500);
}
return $link;
}
return strstr($link, '?')
? preg_replace('/^.+\?/', "${shopPermalink}?", $link)
: $link;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment