Last active
October 28, 2020 09:47
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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