Skip to content

Instantly share code, notes, and snippets.

@luiseduardobraschi
Last active October 8, 2018 05:48
Show Gist options
  • Save luiseduardobraschi/cf1c9dfee81f994519475f7dd5c8d057 to your computer and use it in GitHub Desktop.
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
<?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();
}
}
}
<?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