【发布时间】:2018-10-24 18:24:06
【问题描述】:
如何禁用指向特定类别的 woocommerce 单一产品页面的链接?
我的意思是要禁用单击以打开特定类别而不是所有类别的单个产品页面的选项。因此,我需要一些类别才能正常处理单页。
澄清一下:我希望在此类别中显示产品照片和名称,并在尝试单击打开产品页面时禁用。
【问题讨论】:
标签: php wordpress woocommerce permalinks product
如何禁用指向特定类别的 woocommerce 单一产品页面的链接?
我的意思是要禁用单击以打开特定类别而不是所有类别的单个产品页面的选项。因此,我需要一些类别才能正常处理单页。
澄清一下:我希望在此类别中显示产品照片和名称,并在尝试单击打开产品页面时禁用。
【问题讨论】:
标签: php wordpress woocommerce permalinks product
对于定义的产品类别(或产品类别),以下代码将:
在商店和存档页面中。
您必须在这两个函数中定义您的目标产品类别(或产品类别):
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 文件中。经过测试并且可以工作。
【讨论】: