我正在使用 woocommerce 可变产品。我正在处理 WordPress 片段,产品的图像和标题重定向到单个产品页面,如果产品没有我想要的示例的描述,它会重定向到“domain/product/{product-name}/”删除重定向。
add_action("template_redirect", "disable_single_product_access", 1);
function disable_single_product_access() {
if (is_singular('product')) {
$product = wc_get_product(get_the_ID());
$has_variations = $product->is_type('variable');
$has_price = $product->get_price();
$has_description = $product->get_description();
if (!$has_variations || !$has_price || !$has_description) {
// If product has no description or something above remove redirection
exit;
}
}
}
我尝试删除 a href 并使用 wp_redirect 进行重定向,还尝试将 a href 设置为重定向到空链接,但没有结果。
要在没有描述的情况下从产品档案中删除产品永久链接(不适用于可变产品),请尝试:
add_action( 'woocommerce_before_shop_loop_item', 'remove_loop_product_link_conditionally', 5 );
function remove_loop_product_link_conditionally() {
global $product;
if ( ! $product->is_type('variable') && ! $product->get_description() ) {
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 );
}
}
代码位于活动子主题(或活动主题)的functions.php 文件中。
已测试并适用于 WooCommerce 店面主题。在一些有自己相关定制的主题上,它可能不起作用。