wc_product_canonical_redirect()
Ensure that the product_cat value determined in wc_product_post_type_link is the canonical value.
If other values are used in this part of the permalink, it will be redirected.
No Hooks.
Returns
null. Nothing (null).
Usage
wc_product_canonical_redirect(): void;
wc_product_canonical_redirect() wc product canonical redirect code WC 10.3.6
function wc_product_canonical_redirect(): void {
global $wp_rewrite;
if (
! did_action( 'woocommerce_init' )
|| ! is_product()
|| ! is_a( $wp_rewrite, WP_Rewrite::class )
) {
return;
}
// In the event we are dealing with ugly permalinks, this will be empty.
$specified_category_slug = get_query_var( 'product_cat' );
$specified_category_slug = urldecode( $specified_category_slug );
if ( ! is_string( $specified_category_slug ) || strlen( $specified_category_slug ) < 1 ) {
return;
}
// What category slug did we expect? Normally this maps back to the first assigned product_cat
// term. However, this is filterable so we use the relevant helper function to figure this out.
$expected_category_slug = wc_product_post_type_link( '%product_cat%', get_post( get_the_ID() ) );
$expected_category_slug = urldecode( $expected_category_slug );
if ( $specified_category_slug === $expected_category_slug ) {
return;
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$query_vars = isset( $_GET ) && is_array( $_GET ) ? $_GET : array();
wp_safe_redirect( add_query_arg( $query_vars, wc_get_product( get_the_ID() )->get_permalink() ), 301 );
exit();
}