【问题标题】:Disable links for a specific product category in Woocommerce shop在 Woocommerce 商店中禁用特定产品类别的链接
【发布时间】:2018-10-24 18:24:06
【问题描述】:

如何禁用指向特定类别的 woocommerce 单一产品页面的链接?

我的意思是要禁用单击以打开特定类别而不是所有类别的单个产品页面的选项。因此,我需要一些类别才能正常处理单页。

澄清一下:我希望在此类别中显示产品照片和名称,并在尝试单击打开产品页面时禁用。

【问题讨论】:

    标签: php wordpress woocommerce permalinks product


    【解决方案1】:

    对于定义的产品类别(或产品类别),以下代码将:

    • 删除产品上的链接
    • 移除产品照片下方的按钮

    在商店和存档页面中。

    您必须在这两个函数中定义您的目标产品类别(或产品类别):

    add_action( 'init', 'custom_shop_loop_items' );
    function custom_shop_loop_items() {
        remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 );
        add_action( 'woocommerce_before_shop_loop_item', 'custom_template_loop_product_link_open', 10 );
    }
    
    // Removing the product link
    function custom_template_loop_product_link_open() {
        global $product;
    
        // HERE define your product categories in the array (can be either IDs, slugs or names)
        $terms =  array( 't-shirts', 'hoods' ); // Coma separated
    
        $link = apply_filters( 'woocommerce_loop_product_link', get_the_permalink(), $product );
    
        if( has_term( $terms, 'product_cat', $product->get_id() ) )
            echo '<a class="woocommerce-LoopProduct-link woocommerce-loop-product__link">';
        else
            echo '<a href="' . esc_url( $link ) . '" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">';
    }
    
    // Removing the product button
    add_filter( 'woocommerce_loop_add_to_cart_link', 'filter_loop_add_to_cart_link', 20, 3 );
    function filter_loop_add_to_cart_link( $button, $product, $args = array() ) {
    
        // HERE define your product categories in the array (can be either IDs, slugs or names)
        $terms =  array( 't-shirts', 'hoods' ); // Coma separated
    
        if( has_term( $terms, 'product_cat', $product->get_id() ) )
            $button = '';
    
        return $button;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 感谢 Loic 的分享,在移除产品按钮时效果很好。但它仍然可以单击并在单页中打开产品。我该如何解决。
    • @Mohamed 抱歉,但此代码适用于商店和存档页面……它完全删除了仅在目标产品类别中的相关产品的任何链接和按钮。所以我不明白是什么问题。这是经过测试和工作的。如果不是,是因为您的主题或插件发生了变化,在这种情况下我无法猜测。
    猜你喜欢
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-19
    相关资源
    最近更新 更多