php - 如何更改 woocommerce 价格样式但仅在当前产品的单个产品页面上

标签 php wordpress woocommerce

<分区>

您好,我需要更改 Woocommerce 上所选产品的产品价格

我有这个功能

add_filter( 'woocommerce_get_price_html', 'custom_price_html', 11, 2 );

function custom_price_html($price_html, $product){
    if (is_product()) {

      // here I change the product price or styling
    }

}

问题是函数在产品页面上执行...但也在页面底部的相关产品上执行。

我怎样才能只识别当前产品价格?!

谢谢!

最佳答案

您可以在 if 语句中添加额外的检查,以避免更改相关产品循环的价格 html。

add_filter( 'woocommerce_get_price_html', 'custom_price_html', 11, 2 );

    function custom_price_html($price_html, $product){

        global $woocommerce_loop;

        if( is_product() && !$woocommerce_loop['name'] == 'related' ) {
          // here I change the product price or styling
        }
    
    }

关于php - 如何更改 woocommerce 价格样式但仅在当前产品的单个产品页面上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65092908/

相关文章:

javascript - 使用 Javascript 读取 Rails 4 Cookie/Session

php - 如何在此液体 for 循环上添加迭代

php - 从数组输出 (WordPress PHP) 在纯 css 中创建 slider 图像和文本

mysql - 为什么我不断收到 posts.ID 未知列,而该列存在?

php - 将 WooCommerce 结帐自定义字段保存为用户元数据

php - 付款前在结账页面获取订单ID

php - 以 HTML 提交表单后重定向到同一页面上的 div 覆盖

php - 添加分页后PHP函数不再显示mysql数组数据

mysql - 使用一个 SQL 表 wp_posts 和 wp_postmeta 进行多站点 WordPress

php - 需要使用此代码 woocommerce wordpress 按 ID 显示某些类别