我想显示 WooCommerce 商店通知,不是在整个网站范围内,而是仅针对特定类别或产品。我想将其显示在 woocommerce_before_shop_loop
和 woocommerce_before_single_product
上。就像这些视觉指南一样:
https://businessbloomer.com/woocommerce-visual-hook-guide-archiveshopcat-page/ https://businessbloomer.com/woocommerce-visual-hook-guide-single-product-page/
我怎样才能实现这个目标?我以为我可以使用 woocommerce_demo_store
打印 WooCommerce 商店通知,但它不起作用。谢谢。
add_action( 'woocommerce_before_shop_loop', 'woocommerce_demo_store' );
没有显示任何内容
woocommerce_demo_store
是一个 woocommerce Hook http://hookr.io/filters/woocommerce_demo_store/
最佳答案
首先,我们将删除显示默认商店通知的操作:
remove_action( 'wp_footer', 'woocommerce_demo_store' );
我们将在所选页面上所需的位置重新添加商店通知:
if ( is_product_category( array( 'clothing', 'decor' ) ) ) {
add_action( 'woocommerce_before_shop_loop', 'woocommerce_demo_store' );
}
将'clothing'、'decor'
更改为您的产品类别的别名。
if ( is_single( array( 159, 160 ) ) ) {
add_action( 'woocommerce_before_single_product', 'woocommerce_demo_store' );
}
将 '159', '160'
更改为您的产品 ID。
注意:您可以在 is_product_category()
和 is_single()
函数的输入数组中使用名称、slug 或 ID。
以下是放入子主题的 functions.php
文件中的完整代码:
function lh_conditional_store_notice() {
// Remove default 'woocommerce_demo_store' notice
remove_action( 'wp_footer', 'woocommerce_demo_store' );
// Add back the woocommerce_demo_store' notice, but on the selected pages
// Show notice on the 'clothing' and 'decor' categories at 'woocommerce_before_shop_loop'
if ( is_product_category( array( 'clothing', 'decor' ) ) ) {
add_action( 'woocommerce_before_shop_loop', 'woocommerce_demo_store' );
}
// Show notice on single products having ID '159' and '160' at 'woocommerce_before_single_product'
if ( is_single( array( 159, 160 ) ) ) {
add_action( 'woocommerce_before_single_product', 'woocommerce_demo_store' );
}
}
add_action( 'template_redirect', 'lh_conditional_store_notice' );
已测试并正在处理:
- WordPress 5.0.3
- 二十十九 1.2
- WooCommerce 3.5.4
- 本地主机(适用于 Windows 5.6.15 的 XAMPP)
关于php - 在特定产品/类别上显示 WooCommerce 商店通知/演示商店,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54603305/