根据 WooCommerce 商店和存档页面上的产品类别,在产品价格下方显示图片

Display image below product price based on product category on WooCommerce shop and archive pages

我目前正在为客户开发 WooCommerce 商店。她正在销售珠宝,希望我在产品网格中的产品价格下方显示一个 gold/silver 圆圈(如屏幕截图所示)。


我的想法是用这样的东西来获取类别:

<?php if (is_product_category('979')) then do something ?>

我已经尝试回显并将其插入 wc-template-functions.php --> product-title-loop 但没有成功。

如果有人能指出正确的方向,我会很高兴

“我已经尝试回显并将其插入 wc-template-functions.php”

永远不要编辑核心文件!

当您修改插件的核心文件时,您 运行 有破坏插件和可能破坏 WordPress 安装的风险。此外,插件开发人员无法为您提供支持,因为他们不知道您所做的更改。


改用 woocommerce_after_shop_loop_item 动作挂钩与 has_term()

function action_woocommerce_after_shop_loop_item() {
    // Get the global product object
    global $product;
    
    // Is a WC product
    if ( is_a( $product, 'WC_Product' ) ) {
        
        // Has term - specific categories: the term name/term_id/slug. Several could be added, separated by a comma
        if ( has_term( array( 'categorie-1' ), 'product_cat', $product->get_id() ) ) {
            echo '<img src="my-image.jpg">';
        } elseif ( has_term( array( 'categorie-2' ), 'product_cat', $product->get_id() ) ) {
            echo '<img src="another-image.jpg">';
        }
    }
}
add_action( 'woocommerce_after_shop_loop_item', 'action_woocommerce_after_shop_loop_item', 10, 0 );

代码进入活动子主题(或活动主题)的 functions.php 文件。