Be aware that it's not recommended to redirect or serve HTTP 404 for out of stock products. Read more in How to Optimize Out of Stock Product Pages.
Last active
October 8, 2018 05:48
-
-
Save luiseduardobraschi/cf1c9dfee81f994519475f7dd5c8d057 to your computer and use it in GitHub Desktop.
[WP BR] Remove out of stock product URL and redirect single to home
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 | |
/** | |
* adpated from https://stackoverflow.com/questions/42488432/redirect-out-of-stock-product-to-custom-page | |
*/ | |
add_action('wp', 'wpbr_out_of_stock_redirect'); | |
/** | |
* Redirects out of stock product page to the home page | |
* @return [type] [description] | |
*/ | |
function wpbr_out_of_stock_redirect() { | |
if (is_product()) { | |
global $post; | |
$product = wc_get_product($post->ID); | |
if (!$product->is_in_stock()) { | |
wp_redirect(home_url()); | |
exit(); | |
} | |
} | |
} |
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 | |
add_filter( 'woocommerce_loop_product_link', 'wpbr_remove_link', 99, 2 ); | |
add_filter( 'woocommerce_product_add_to_cart_url', 'wpbr_remove_link', 99, 2 ); | |
/** | |
* Will remove the product URL in case it is out of stock | |
* @param string $link the product permalink | |
* @param WC_Product $product | |
* @return string | |
*/ | |
function wpbr_remove_link($link, $product){ | |
if(!$product->is_in_stock()) { | |
$link = '#'; | |
} | |
return $link; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment