如何禁用/隐藏woocommerce单品页面?

won*_*der 7 wordpress woocommerce

我想在我的wordpress-woocommerce网站上隐藏单个产品详细信息页面.如何在不破坏woocommerce功能的情况下实现这一目标?

Vit*_*nko 12

把它放在functions.php中

//Removes links
add_filter( 'woocommerce_product_is_visible','product_invisible');
function product_invisible(){
    return false;
}

//Remove single page
add_filter( 'woocommerce_register_post_type_product','hide_product_page',12,1);
function hide_product_page($args){
    $args["publicly_queryable"]=false;
    $args["public"]=false;
    return $args;
}
Run Code Online (Sandbox Code Playgroud)

  • 这不仅隐藏了产品页面,而且将所有产品设置为不可见。正确答案是@Ibad Shah (2认同)

小智 11

您可以删除在商店页面上生成的锚点,该锚点永远不会将用户重定向到单个页面.为此,您必须将此代码粘贴到functions.php文件中.

remove_action( 
  'woocommerce_before_shop_loop_item',
  'woocommerce_template_loop_product_link_open',
  10
);
Run Code Online (Sandbox Code Playgroud)

此代码将删除链接,但之后,您将删除锚关闭标记,只是它不会破坏您的HTML

remove_action(
  'woocommerce_after_shop_loop_item',
  'woocommerce_template_loop_product_link_close',
  5
);
Run Code Online (Sandbox Code Playgroud)


Mhd*_*wan 5

您可以使用is_product()帮助器功能注册一个返回404的挂钩(如果出现产品页面)

function prevent_access_to_product_page(){
    global $post;
    if ( is_product() ) {
        global $wp_query;
        $wp_query->set_404();
        status_header(404);
    }
}

add_action('wp','prevent_access_to_product_page');
Run Code Online (Sandbox Code Playgroud)

解决方案已经过测试并且可以正常工作。

注意:解决方案基于@ale的答案中的某些信息。


Ale*_*Ale 2

The single page is something that is provided from WordPress and there is no way to disable it. But there are some ways to prevent access to single product pages.

The first one is to edit your shop (products-archive) template and to delete all the places where you have a link to the single page.

The second is to do a check on each page load if the page is a single product page and redirect the user to wherever you want:

add_action('init','prevent_access_to_product_page');
function prevent_access_to_product_page(){
    if ( is_product() ) {
        wp_redirect( site_url() );//will redirect to home page
    }
}
Run Code Online (Sandbox Code Playgroud)

You can include this code in your functions.php file of your child-theme's directory. Have in mind that I haven't tested the code.