Skip to content

Instantly share code, notes, and snippets.

@edmundcwm
Last active March 22, 2024 00:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edmundcwm/085d074949d727e47c817a785fb67bcf to your computer and use it in GitHub Desktop.
Save edmundcwm/085d074949d727e47c817a785fb67bcf to your computer and use it in GitHub Desktop.
Woocommerce: Remove single product page link in archive page and redirect single product page to shop page
<?php
/**
* Removes single product link in archive page and enable template redirect
*
* A snippet that removes the <a href="(single product permalink)"> in the archive page
* and redirects user back to the shop page when they try to access the single page directly
*
* WC tested up to: 3.4.3
*
* Hook ref:
* https://github.com/woocommerce/woocommerce/search?utf8=%E2%9C%93&q=woocommerce_before_shop_loop_item
* https://github.com/woocommerce/woocommerce/search?utf8=%E2%9C%93&q=woocommerce_after_shop_loop_item
**/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
function edm_template_redirects() {
if ( is_product() ) {
//redirect to shop page. If you want to redirect to the home page instead then change
//'wc_get_page_permalink('shop' )' to 'get_site_url()'
wp_redirect( wc_get_page_permalink('shop' ) );
}
exit;
}
add_action( 'template_redirect', 'edm_template_redirects' );
//remove the opening <a> tag in archive page
remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 );
//remove the closing <a> tag in archive page
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment