php - 如果产品属于 WooCommerce 中的某个类别,请在存档页面上的价格下方添加自定义文本
问题描述
我正在尝试在某个类别(或两个)内的任何产品的价格下方(不是在单个产品页面上)添加自定义消息。
这是我已经走了多远:
add_action( 'woocommerce_after_shop_loop_item', 'custom_text', 5 );
function custom_text() {
global $product;
if ( is_product_category( 'swim-trunks' ) ) {
echo '<span class="custom-text">Buy Any 3 Swim Trunks For $99</span>';
}
}
但这仅适用于类别 slug,这意味着它仅适用于该特定类别,并且没有考虑该产品是否也属于另一个类别。最后,它不适用于主页或相关产品上的产品滑块。
编辑:
所以,我在这里更新了代码,这似乎有效:
/* Begin Custom Text below price on shop page */
add_action( 'woocommerce_after_shop_loop_item', 'custom_text', 5 );
function custom_text() {
global $product;
if ( has_term( 76, 'product_cat' ) || has_term( 71, 'product_cat' )) {
echo '<span class="custom-text">Buy Any 3 Swim Trunks For $99</span>';
}
}
/* End Custom Text below price on shop page */
如果有人有更好的解决方案,我很想听听!
解决方案
您可以使用 acf 字段或元框或自定义字段 woocommerce。您需要做的就是将其钩入到位。例如woocommerce_after_shop_loop_item
推荐阅读
- python - 如何对数据框的特定行操作公式?
- python - 如何在不保存的情况下将视频和输出转换为 BytesIO?
- python - OpenAI 的 A2C 基线中是否默认使用循环策略?
- javascript - FF 的奇怪行为“und\x65\x66\151n\x65\144”转换为“d”
- autodesk-forge - 使用设计自动化访问 bim 360 文件
- xml - 使用 xslt 将文本转换为 xml 而不提供输入文件名
- ios - NSOperationQueue、异步任务和 maxConcurrentOperationCount
- mysql - 连接选择结果的用户函数
- css - Antd内联子窗体宽度
- css - 如何修改 SweetAlert2 的 Toast 动画设置?