Woocommerce 仅显示可变产品的一种折扣价格

2023-12-28

这是我的 WooCommerce 网站:sweetworldcandy.com

问题是,可变产品价格中的最低和最高 值显示我想要的是,如果产品不打折,则显示最低价值,如果产品打折,则通过添加斜杠作为删除标签,显示最低价值和报价的最低价值

我分享了下面三张图片供参考:


2020年更新 (适用于 Woocommerce 3+)

替换已弃用的函数woocommerce_price() by wc_price()自 Woocommerce 3+ 起:

add_filter('woocommerce_variable_sale_price_html', 'shop_variable_product_price', 10, 2);
add_filter('woocommerce_variable_price_html','shop_variable_product_price', 10, 2 );
function shop_variable_product_price( $price, $product ){
    $variation_min_reg_price = $product->get_variation_regular_price('min', true);
    $variation_min_sale_price = $product->get_variation_sale_price('min', true);
    if ( $product->is_on_sale() && !empty($variation_min_sale_price)){
        if ( !empty($variation_min_sale_price) )
            $price = '<del class="strike">' .  wc_price($variation_min_reg_price) . '</del>
        <ins class="highlight">' .  wc_price($variation_min_sale_price) . '</ins>';
    } else {
        if(!empty($variation_min_reg_price))
            $price = '<ins class="highlight">'.wc_price( $variation_min_reg_price ).'</ins>';
        else
            $price = '<ins class="highlight">'.wc_price( $product->regular_price ).'</ins>';
    }
    return $price;
}

原线(WooCommerce 3 之前):下面的代码应该符合您的预期:

add_filter('woocommerce_variable_sale_price_html', 'shop_variable_product_price', 10, 2);
add_filter('woocommerce_variable_price_html','shop_variable_product_price', 10, 2 );
function shop_variable_product_price( $price, $product ){
    $variation_min_reg_price = $product->get_variation_regular_price('min', true);
    $variation_min_sale_price = $product->get_variation_sale_price('min', true);
    if ( $product->is_on_sale() && !empty($variation_min_sale_price)){
        if ( !empty($variation_min_sale_price) )
            $price = '<del class="strike">' .  woocommerce_price($variation_min_reg_price) . '</del>
        <ins class="highlight">' .  woocommerce_price($variation_min_sale_price) . '</ins>';
    } else {
        if(!empty($variation_min_reg_price))
            $price = '<ins class="highlight">'.woocommerce_price( $variation_min_reg_price ).'</ins>';
        else
            $price = '<ins class="highlight">'.woocommerce_price( $product->regular_price ).'</ins>';
    }
    return $price;
}

该代码位于活动子主题(或主题)的 function.php 文件中或任何插件文件中。

这段代码已经过测试并且可以工作。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Woocommerce 仅显示可变产品的一种折扣价格 的相关文章

随机推荐

  • 如何在 DO 块中执行选择查询?

    我想将以下 SQL 代码从 MS SQL Server 移植到 PostgreSQL DECLARE iStartYear integer DECLARE iStartMonth integer DECLARE iEndYear integ
  • Material-ui 从 React-router 添加 Link 组件

    我正在努力添加我的 Material ui AppBar 的组件 这是我的导航类 class Navigation extends Component constructor props super props render var sty
  • JavaScript 检查图像是否存在

    我正在尝试显示谷歌地图或图像 所以 我想要的是默认情况下我想显示图像 如果图像不存在 则显示谷歌地图 为此 默认情况下我禁用谷歌地图 map canvas display none 然后 我需要类似布尔条件的东西来检查图像的存在 然后返回
  • 如何在 Python 中的泛型类型上使用 isinstance

    我试图检查参数是否是类声明中指定的泛型类型的实例 然而Python似乎不允许这样做 T TypeVar T class MyTypeChecker Generic T def is right type self x Any return
  • 如何通过单击按钮将其打印在一张纸上[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我正在寻找使用按钮打印页面的解决方案OnClick event 让我举一个例子 带按钮OnClick事件时 代码将随机地将某些图像彼此相邻
  • 在 ListView WPF 中设置项目焦点

    有什么方法可以从 WPF 中的 WinForms 实现此功能吗 ListView FocusedItem ListView Items itemToFocusIndex 我正在尝试手动设置focus 不是选择 WPF ListView 中的
  • C# 检查MySQL数据库是否存在

    我正在使用顶级语句用 C 编写一个简单的控制台应用程序 我想在开始时检查是否存在数据库 这是我的代码 using MySql Data MySqlClient using MySqlConnection connection new MyS
  • 在主键上创建索引并建立索引有什么好处吗?

    我检查了这个帖子 MySQL中主键 唯一键 索引有什么区别 https stackoverflow com questions 3844899 whats the difference between primary key unique
  • 加入现有 NLB 集群

    我一直在尝试编写一个 powershell 程序来检查集群是否存在 如果没有 那么它会创建它并将自己添加到其中 如果另一台计算机醒来 它会检查集群是否存在 如果存在 则将自己添加到集群中 我在尝试从集群 IP 地址获取对集群对象的引用时遇到
  • 如何使用 q 建立回调链?

    我在理解如何使用 JavaScript 的 Promise 库 q https github com kriskowal q 时遇到一些问题 var delayOne function setTimeout function return
  • jQuery 的 .clone(true, true) 不会克隆子级上的事件绑定

    我正在使用克隆 true true 来复制选择和一些按钮以克隆和删除 我认为第二个 true 参数应该确保事件处理程序传递到克隆的子按钮 这是标记 div div class clone div div
  • 如何使用 jQuery .keyup() 函数捕获键盘输入

    正在开发一个简单的刽子手游戏 我正在尝试使用以下命令捕获用户输入keyup 但是当我将其记录到控制台时 我意识到有些东西无法正常工作 这是我的代码 document keyup function e userInput e value co
  • 在 GridSearchCV 中使用精度作为评分时如何指定正标签

    model sklearn model selection GridSearchCV estimator est param grid param grid scoring precision verbose 1 n jobs 1 iid
  • 合并多个 cv::Mat?

    基本上我有 3 个这样的垫子 Mat descriptors1 Mat descriptors2 Mat descriptors3 每个描述符的加载方式如下 extractor gt compute object kp descriptor
  • 如何在 Scala 中为具有构造函数参数的 Actor 创建 TestActorRef?

    The Akka 测试文档 http doc akka io docs akka snapshot scala testing html给出以下创建 TestActorRef 的方法 import akka testkit TestActo
  • 在 R 中并行训练多个网络(使用 Keras)

    我想在 R 中训练 n 个不同的神经网络parallel 我怎样才能做到这一点 我对并行化相当陌生 所以我发现这有点困难 这是我的 1 个网络的代码 到目前为止 model lt keras model sequential Define
  • 按列对 Pandas DataFrame 进行排序

    如何按列对 pandas 数据框进行排序 我在这里读到http pandas pydata org sort values http pandas pydata org pandas docs version 0 19 2 generate
  • MigLayout 对齐中心不会使 JLabel 组件居中

    我在用米格布局 http www miglayout com 我发现它很灵活等 但我在用它居中放置东西时遇到问题 我尝试使用gapleft 50 但似乎百分比数字需要根据不同的帧大小进行更改 因为它也取决于组件的大小 所以如果组件使用居中g
  • 如何在编译时设置 Electron 变量?

    我正在使用 electro builder 来打包 React 应用程序 我的应用程序使用 create react app 样板 有没有一种方法可以在编译时设置变量 以便可以在运行时在主进程的代码内的条件中使用它 我正在寻找 C 预处理器
  • Woocommerce 仅显示可变产品的一种折扣价格

    这是我的 WooCommerce 网站 sweetworldcandy com 问题是 可变产品价格中的最低和最高 值显示我想要的是 如果产品不打折 则显示最低价值 如果产品打折 则通过添加斜杠作为删除标签 显示最低价值和报价的最低价值 我