如何在WooCommerce中禁用单个产品页面

时间:2019-06-27 16:01:43

标签: php wordpress woocommerce

我正在使用 Woocommerce 3.6.4 + Wordpress 5.2.2 构建电子商务网站。

我试图禁用WooCommerce的单个产品页面,以便人们可以直接从我的商店页面购物。

尝试:

1。将下面的代码添加到我的子主题的functions.php文件中,无效。

remove_action( ‘woocommerce_before_shop_loop_item’,‘woocommerce_template_loop_product_link_open’, 10 );
remove_action( ‘woocommerce_after_shop_loop_item’,‘woocommerce_template_loop_product_link_close’, 5 );

2。将相同的代码添加到我的WooCommerce content-product.php file。没用。

3。/wp-content/plugins/woocommerce/templates/content-product.php

将这些代码作为注释:

        do_action( ‘woocommerce_before_shop_loop_item’);
        do_action( ‘woocommerce_after_shop_loop_item’);

成为这样:

       //do_action( ‘woocommerce_before_shop_loop_item’);
       //do_action( ‘woocommerce_after_shop_loop_item’);

它删除了购物页面的链接,但没有删除单个产品页面。

有什么建议或帮助吗?

作为一个对编码一无所知的人,这确实使我发疯了!!!

请问有人可以帮我吗?任何建议都会很棒!

下面是 wp_content / themes / my_theme / woocommerce / content-product.php


    `<?php
/**
 * The template for displaying product content within loops
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/content-product.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see     https://docs.woocommerce.com/document/template-structure/
 * @author  WooThemes
 * @package WooCommerce/Templates
 * @version 3.6.0
 */
defined( 'ABSPATH' ) || exit;
`

    <li  <?php wc_product_class( '', $product ); ?> data-product-id="<?php the_ID(); ?>">
    <?php
    /**
     * Hook: woocommerce_before_shop_loop_item.
     *
     * @hooked woocommerce_template_loop_product_link_open - 10
     */
    do_action( 'woocommerce_before_shop_loop_item' );?>

    <?php if ('yes' != $themify->hide_product_image) : ?>
        <?php

        /**
         * Hook: woocommerce_before_shop_loop_item_title.
         *
         * @hooked woocommerce_show_product_loop_sale_flash - 10
         * @hooked woocommerce_template_loop_product_thumbnail - 10
         */
        do_action( 'woocommerce_before_shop_loop_item_title' );

        ?>
    <?php endif; ?>
    <div class="product-content">
        <div class="product-content-inner-wrapper">
            <div class="product-content-inner">
                <?php
                /**
                 * Hook: woocommerce_shop_loop_item_title.
                 *
                 * @hooked woocommerce_template_loop_product_title - 10
                 */
                do_action( 'woocommerce_shop_loop_item_title' );


                /**
                 * Hook: woocommerce_after_shop_loop_item.
                 *
                 * @hooked woocommerce_template_loop_product_link_close - 5
                 * @hooked woocommerce_template_loop_add_to_cart - 10
                 */
                do_action( 'woocommerce_after_shop_loop_item' );

                ?>
                <div class="product-share-wrap">
                    <?php Themify_Wishlist::button() ?>
                    <?php if (themify_hide_quick_look()): ?>
                        <a onclick="return false;" data-image="<?php echo wc_placeholder_img_src() ?>" class="quick-look themify-lightbox" href="<?php echo add_query_arg(array('post_in_lightbox' => '1'), get_permalink()) ?>"><span class="tooltip"><?php _e('Quick Look', 'themify'); ?></span></a>
                    <?php endif; ?>
                    <?php if (themify_hide_social_share()): ?>
                        <?php get_template_part('includes/social-share', 'product'); ?>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
    <!-- /.summary -->
</li>

1 个答案:

答案 0 :(得分:0)

在您的主题functions.php中添加代码,

// remove single product page link
remove_action( 'woocommerce_before_shop_loop_item','woocommerce_template_loop_product_link_open', 10 );
remove_action( 'woocommerce_after_shop_loop_item','woocommerce_template_loop_product_link_close', 5 );

// hide single product page completely
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;
}

解决方案部分基于@Vitaly Gritsienko的答案