在 WooCommerce 中显示促销购物车商品格式的价格范围

2024-03-18

我尝试在购物车和结帐页面中显示每个产品的小计价格和销售价格。我已经设法使用以下代码将销售价格 (24TL) 添加到正常价格 (33TL):

/**
 * Show sale prices on the Cart and Checkout pages
 */
function my_custom_show_sale_price( $old_display, $cart_item, $cart_item_key ) {
    $price_string = $old_display;

    $product = $cart_item['data'];
    if ( $product ) {
        $price_string = $product->get_price_html();
    }

    return $price_string;
}
add_filter( 'woocommerce_cart_item_price', 'my_custom_show_sale_price', 10, 3 );
add_filter( 'woocommerce_cart_item_subtotal', 'my_custom_show_sale_price', 10, 3 ); 

不幸的是,上面的代码仅显示购物车或结账页面中一件的产品价格,而不是小计价格(在本例中应为 66TL / 48TL)。

经过一番挖掘后,我发现以下价格过滤器在我的默认 cart.php 文件中将每件商品的价格显示为小计 - 在我使用上面的代码表单将销售价格添加到其中之前,价格以正确的方式显示。

    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $_product   = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
        if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
            echo apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key );
        }
    }

知道如何更改第一段代码以将每件商品的产品价格显示为小计吗?


Updated:您可以使用以下命令来显示购物车商品的促销价格格式范围:

add_filter( 'woocommerce_cart_item_price', 'filter_cart_item_price', 10, 3 );
function filter_cart_item_price( $price_html, $cart_item, $cart_item_key ) {
    if( $cart_item['data']->is_on_sale() ) {
        return $cart_item['data']->get_price_html();
    }
    return $price_html;
}

add_filter( 'woocommerce_cart_item_subtotal', 'filter_cart_item_subtotal', 10, 3 );
function filter_cart_item_subtotal( $subtotal_html, $cart_item, $cart_item_key ){
    $product    = $cart_item['data'];
    $quantity   = $cart_item['quantity'];
    $tax_string = '';

    if ( $product->is_taxable() ) {
        if ( WC()->cart->display_prices_including_tax() ) {
            $regular_price = wc_get_price_including_tax( $product, array( 'qty' => $quantity, 'price' => $product->get_regular_price() ) );
            $active_price  = wc_get_price_including_tax( $product, array( 'qty' => $quantity ) );

            if ( ! wc_prices_include_tax() && WC()->cart->get_subtotal_tax() > 0 ) {
                $tax_string = ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
            }
        } else {
            $regular_price = wc_get_price_excluding_tax( $product, array( 'qty' => $quantity, 'price' => $product->get_regular_price() ) );
            $row_price     = wc_get_price_excluding_tax( $product, array( 'qty' => $quantity ) );

            if ( wc_prices_include_tax() && WC()->cart->get_subtotal_tax() > 0 ) {
                $tax_string = ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
            }
        }
    } else {
        $regular_price = $product->get_regular_price() * $quantity;
        $active_price  = $product->get_price() * $quantity;
    }

    if( $product->is_on_sale() ) {
        return wc_format_sale_price( $regular_price, $active_price ) . $product->get_price_suffix() . $tax_string;
    }

    return $subtotal_html;
}

代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。

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

在 WooCommerce 中显示促销购物车商品格式的价格范围 的相关文章

随机推荐

  • 以编程方式阻止 Windows 屏幕保护程序启动

    是否有推荐的方法来阻止 Windows 屏幕保护程序启动 我发现的最接近的是本文 http www codeproject com KB cs ScreenSaverControl aspx 但我真正想做的只是告诉 Windows 计算机没
  • Angular 2抛出错误:插座未激活

    我已经设置了我的应用程序 以便我有一个Recipe Book其中有一个列表Recipies当我点击食谱时 它会显示Recipe Details在嵌套路由中 然后 它还有一个按钮 单击该按钮会将成分加载到嵌套路径中Recipes Detail
  • 如何在 Vim 中普遍使用相对行号

    我喜欢 Vim 7 3 中的相对行编号功能 但我很难让它普遍适用 对于许多文件 行编号会恢复为绝对模式 即使我已指定 set rnu in my vimrc文件 知道是什么原因造成的吗 我在 OSX 10 6 上使用 Vim 7 3 以及
  • 等待线程是否重新访问synchronized方法内的代码

    我正在阅读有关线程同步和等待 通知结构的内容tutorial http docs oracle com javase tutorial essential concurrency guardmeth html 它指出 当调用 wait 时
  • 将 $cond 运算符与 Spring-data-mongodb 一起使用[重复]

    这个问题在这里已经有答案了 我希望汇总以下数据 user user1 error true user user2 error false user user1 error false Into id user1 errorCount 1 t
  • Docker run --volume 不断创建随机卷而不使用指定的卷

    Docker 不断创建随机卷 而不是使用我在运行时指定的卷docker run 我将从无卷开始 docker volume ls DRIVER VOLUME NAME 我将创建一个 docker volume create myvol 它将
  • 缓存一致性有什么意义?

    在像 x86 这样提供缓存一致性的 CPU 上 从实际角度来看这有何用处 据我所知 这个想法是让一个核心上完成的内存更新在所有其他核心上立即可见 这是一个有用的属性 然而 如果不是用汇编语言编写 就不能太依赖它 因为编译器可以将变量赋值存储
  • 如何在 Mac 上恢复 .bash_profile?我的 Unix 终端都无法工作 [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我在尝试将 Playframework 添加到我的路径时向我的 bash profile 添加了一些内容 但有些内容严重混乱 我将以下行添
  • Angular2-Webpack-Typescript - 3rd 方库

    我开始了一个美丽的种子项目 https github com AngularClass angular2 webpack starter https github com AngularClass angular2 webpack star
  • 在构建时设置.net core库程序集/文件/nuget包版本

    我正在寻找一种在构建时设置 net core 库的程序集版本 以及文件版本和 nuget 包版本 的方法 我的库是使用最新的 Visual Studio 2017 RC 编写的 因此不再需要projects json文件 并由 TeamCi
  • iOS UI 元素移植到 Android 上

    女士们 先生们 在我的工作中 在开发 Android 应用程序时 我经常满足客户的以下要求 使其看起来像 iPhone 应用程序 是的 我知道 最好的方法是为他提供规范的 Android 设计 其中包含所有这些模式 例如仪表板 使用菜单按钮
  • Firestore (9.6.6):连接 WebChannel 传输错误:

    在反应应用程序中 我在同一个文件中有两个几乎相同的函数来将文档添加到子集合中 一个每次都有效 另一个使用不同的子集合 大多数时候都会收到以下警告 注意 不是错误 并且不会添加数据 await addDoc collection db use
  • 使用 XLW 项目和 Visual Studio 2022 C++ 构建和调试 Excel 加载项

    我正在使用以下命令创建 Excel 加载项XLW https github com xlw xlwGitHub 上的 Visual Studio 2022 C 项目 我做了以下事情 创建一个解决方案 MyFunction 其中包含两个项目
  • 来自 的警报对话框被阻止

    当我使用 webview 并显示此错误时 Dart 不显示警报对话框 警报对话框被阻止 扩展名 webViewEvents 225 确认对话框被阻止 扩展名 webViewEvents 225 有谁知道如何绕过该问题或如何捕获错误 Than
  • 在 IE 10 中使用复合键创建索引时出现 DataError

    我正在使用 IE10 测试基于 indexedDB 的应用程序 我无法创建具有多个键的对象存储 例如 var objectStore theDb createObjectStore store1 keyPath key1 key2 当我尝试
  • 在Python中生成字典树中的所有叶到根路径

    我有一个 非标准 形式的字典树 如下所示 tree 0 A B C D E F 叶节点被定义为字典键值对 其中值是空字典 我想将所有叶到根路径提取为列表列表 如下所示 paths C B A 0 E D 0 F D 0 如果有帮助的话 也可
  • 如何使用 Firebase Analytics 定义受众群体?

    我想通过使用自定义属性分隔应用程序的受众来跟踪它们 每个用户都有一个权限列表 我希望在登录我的应用程序时能够使用此属性将它们分开 目前 所有用户都属于 所有用户 类别 而不是单独的类别 我使用 CEO 登录超过 10 次 并且等待了几天 不
  • Openssl 从 CSR 创建证书(无私钥 - 存储在另一个系统中)

    我需要创建一个基于CSR由第三方生成我无权访问私钥 生成的证书需要有keyUsage keyCertSign至少作为证书的一部分 C OpenSSL Win32 bin openssl exe req in C xampp htdocs c
  • FTP 对 PASV 命令的响应应该是什么

    我正在编写一个 FTP 服务器 但我不理解 PASV 命令 任何服务器都会发送如下响应 227 Entering Passive Mode 213 229 112 130 216 4 括号里的数字是什么意思 普通模式和被动模式有什么区别 是
  • 在 WooCommerce 中显示促销购物车商品格式的价格范围

    我尝试在购物车和结帐页面中显示每个产品的小计价格和销售价格 我已经设法使用以下代码将销售价格 24TL 添加到正常价格 33TL Show sale prices on the Cart and Checkout pages functio