请求帮助:移除WooCommerce非供应商商品跳转改为提示消息
Hey Lisa, let's fix this redirect headache once and for all—those thousands of redirect records are totally unideal, and a clear, user-friendly message is way better for your site's experience. Here's how to swap out that login redirect logic for a proper access denied notice:
Step 1: Remove the Existing Redirect Logic
First, we need to shut off the code causing the redirect. If you added a custom function (like the one you referenced), we'll unhook it. Let's assume your current redirect uses the template_redirect hook—add this line to disable it:
// Replace 'my_vendor_product_redirect' with the exact name of your existing redirect function remove_action( 'template_redirect', 'my_vendor_product_redirect' );
If the redirect is built into WCVendors itself (not custom code), you can still disable it by targeting their core hook. Test this if needed:
remove_action( 'template_redirect', 'WCV_Vendor_Restrictions::restrict_product_access' );
Step 2: Add the Access Denied Message Logic
Now we'll add custom logic to check permissions and show a message instead of redirecting. Paste this code into your theme's functions.php file (or use a custom code plugin like Code Snippets to avoid losing changes when updating your theme):
// Check permissions on single product pages add_action( 'template_redirect', 'custom_vendor_product_access_check' ); function custom_vendor_product_access_check() { // Only run on single product pages, and only if the user isn't a vendor if ( is_product() && !wcv_is_vendor() ) { // Inject our access denied message at the top of the page add_action( 'woocommerce_before_single_product', 'custom_vendor_access_denied_notice', 10 ); // Optional: Remove core product elements to restrict full access (title, price, add to cart) remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 ); remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 ); remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 ); remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 ); } } // The custom access denied message function custom_vendor_access_denied_notice() { echo '<div class="woocommerce-error" style="padding: 1.5em; margin-bottom: 2em; background-color: #f8d7da; color: #721c24; border-radius: 4px;">'; echo 'Sorry, this product is only visible to approved vendors. Please <a href="' . wp_login_url( get_permalink() ) . '" style="color: #721c24; text-decoration: underline;">log in as a vendor</a> to view full details.'; echo '</div>'; }
Quick Tips:
- Verify the Vendor Check: Double-check that
wcv_is_vendor()matches how your original code identifies vendors. If you used a different method (like checking for a vendor ID), replace!wcv_is_vendor()with that logic (e.g.,!wcv_get_vendor( get_current_user_id() )). - Customize the Message: Tweak the text and styling in
custom_vendor_access_denied_notice()to fit your site's tone and design. - Test Thoroughly: Log out, use a non-vendor account, and visit a restricted product page to confirm the message shows up instead of redirecting.
Cleanup Existing Redirect Records
Now that the new logic is live, future visits won't generate redirects. For existing records in your analytics or server logs, you can filter them out (most tools let you exclude specific redirect URLs) or just let them age out over time—no need to manually delete thousands of entries.
内容的提问来源于stack exchange,提问作者Lisa Demairs

