The code snippet provided below can be used to handle the 404 error on WooCommerce product pages. This code will redirect users to a custom page when they encounter a 404 error on a product page.
/**
* Redirect WooCommerce 404 error on product pages
*/
function wpsnippets_redirect_woocommerce_404() {
if ( is_product() && ! is_user_logged_in() ) {
wp_redirect( home_url( '/custom-404-page/' ) );
exit;
}
}
add_action( 'template_redirect', 'wpsnippets_redirect_woocommerce_404' );
This code should be added to your theme’s functions.php
file or in a custom plugin. It uses the template_redirect
action hook to check if the current page is a product page (is_product()
) and if the user is not logged in (! is_user_logged_in()
). If both conditions are met, it redirects the user to a custom 404 page using wp_redirect()
and exits the script.
You can modify the home_url( '/custom-404-page/' )
part to specify the URL of your custom 404 page. Make sure to replace /custom-404-page/
with the actual URL slug or path of your custom page.
By implementing this code snippet, you can provide a better user experience by redirecting users to a custom 404 page specifically designed for WooCommerce product pages when they encounter a 404 error.
Examples
Example 1: Customizing the WooCommerce 404 error message on product pages
This example demonstrates how to customize the 404 error message that is displayed on WooCommerce product pages when a product is not found. By using the woocommerce_no_product_found
filter hook, you can modify the error message to suit your needs.
function wpsnippets_custom_woocommerce_404_message( $message ) {
$message = 'Oops! The product you are looking for cannot be found.';
return $message;
}
add_filter( 'woocommerce_no_product_found', 'wpsnippets_custom_woocommerce_404_message' );
In this code example, we define a custom function wpsnippets_custom_woocommerce_404_message
that takes the default error message as a parameter and returns the modified message. We then use the add_filter
function to hook our custom function to the woocommerce_no_product_found
filter. This ensures that our custom message is displayed when a product is not found.
Example 2: Redirecting WooCommerce 404 error on product pages
In some cases, you may want to redirect users to a specific page instead of displaying the default 404 error message on WooCommerce product pages. This can be achieved by using the template_redirect
action hook and checking if the current page is a product page.
function wpsnippets_redirect_woocommerce_404() {
if ( is_product() ) {
wp_redirect( home_url( '/custom-page/' ) );
exit;
}
}
add_action( 'template_redirect', 'wpsnippets_redirect_woocommerce_404' );
In this code example, we define a custom function wpsnippets_redirect_woocommerce_404
that checks if the current page is a product page using the is_product
conditional function. If it is, we redirect the user to a custom page using the wp_redirect
function and exit the script using exit
.
Example 3: Logging WooCommerce 404 errors on product pages
If you want to keep track of 404 errors that occur on WooCommerce product pages, you can log them to a file for later analysis. This can be done by using the woocommerce_no_product_found
filter hook and the error_log
function.
function wpsnippets_log_woocommerce_404( $message ) {
$log_message = '404 Error: ' . $message;
error_log( $log_message );
return $message;
}
add_filter( 'woocommerce_no_product_found', 'wpsnippets_log_woocommerce_404' );
In this code example, we define a custom function wpsnippets_log_woocommerce_404
that takes the default error message as a parameter. We then prepend the message with “404 Error: ” and log it using the error_log
function. Finally, we return the original message. By hooking this function to the woocommerce_no_product_found
filter, we ensure that the error messages are logged whenever a product is not found.