Last active
March 22, 2024 00:17
-
-
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
This file contains 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 | |
/** | |
* 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