在 Woocommerce 商店页面中显示可变产品的默认变化价格

2024-05-10

I'm wondering how I can display the product price on the shop page. Right now my variable products are shown with their price range. Shope page

But these are both products that have been setup with default variable parameters: Default Parameters

When you click on a product, it's being shown as followed: Single Product

正如您所看到的,该特定选择的价格是€300,我想知道如何显示它€300在商店页面上而不是€150-€2.003


可以获取在可变产品中设置的默认变化价格并将其显示在商店和档案页面中:

add_filter( 'woocommerce_variable_price_html', 'custom_variable_displayed_price', 10, 2 );
function custom_variable_displayed_price( $price_html, $product ) {
    // Only for archives pages
    if ( ! ( is_shop() || is_product_category() || is_product_tag() ) )
        return $price_html;

    // Searching for the default variation
    $default_attributes = $product->get_default_attributes();
    // Loop through available variations
    foreach($product->get_available_variations() as $variation){
        $found = true; // Initializing
        // Loop through variation attributes
        foreach( $variation['attributes'] as $key => $value ){
            $taxonomy = str_replace( 'attribute_', '', $key );
            // Searching for a matching variation as default
            if( isset($default_attributes[$taxonomy]) && $default_attributes[$taxonomy] != $value ){
                $found = false;
                break;
            }
        }
        // When it's found we set it and we stop the main loop
        if( $found ) {
            $default_variaton = $variation;
            break;
        } // If not we continue
        else {
            continue;
        }
    }

    // If no default variation is found we exit.
    if( ! isset($default_variaton) )
        $price_html;

    // Formatting the price
    if ( $default_variaton['display_price'] !== $default_variaton['display_regular_price'] && $product->is_on_sale()) {
        $price_html = '<del>' . wc_price($default_variaton['display_regular_price']) . '</del> <ins>' . wc_price($default_variaton['display_price']) . '</ins>';
    } else {
        $price_html = wc_price($default_variaton['display_price']);
    }
    return $price_html;
}

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

相关回答:显示 Woocommerce 3 上的默认变化价格和节省金额 https://stackoverflow.com/questions/50234781/display-the-default-variation-price-and-savings-amount-on-woocommerce-3/50239940#50239940

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

在 Woocommerce 商店页面中显示可变产品的默认变化价格 的相关文章

  • 使用 ImageMagick (PHP) 将 2 个图像并排合并为 1 个图像

    我认为这是一件容易的事 我有 2 张图片 JPG 我希望它们合并成一张图片 其中 2 张图片并排 所以我有图片 A 和图片 B 我想要图片 AB 并排 两个图像具有相同的宽度和高度 在本例中 宽度 200px 高度 300px 但是第二个图
  • Readfile 从大文件中读取 0 字节?

    我正在尝试通过以下方式发送一个大文件readfile 但是 没有任何内容发送到浏览器 并且readfile 回报0 not false 我尝试发送的文件大小为 4GiB 并且可由 PHP 读取 我正在设置set time limit 0 以
  • 我可以让 swagger-php 在查询字符串上使用数组吗?

    我使用 Swagger php 当我定义查询字符串上的参数时 它可以是一个数组 但据我所知 它不支持这种查询字符串 https api domain tld v1 objects q 1 q 5 q 12 我相信这会被设定in the co
  • 正则表达式上的换行符

    我试图替换两个标签之间的所有内容 但我无法构建正确的表达式 这就是我所做的
  • PHP 无法打开流:是一个目录

    非常简单的 PHP 脚本 我在我亲自设置的 Ubuntu Web 服务器上的 EE 模板中运行 我知道这与权限有关 并且我已经将我尝试写入的目录的所有者更改为 Apache 用户 我得到的错误是 遇到 PHP 错误 严重性 警告 消息 fi
  • 如何关闭未关闭的 HTML 标签?

    每当我们从数据库或类似来源获取一些经过编辑的用户输入内容时 我们可能会检索仅包含开始标记但不包含结束标记的部分 这可能会妨碍网站当前的布局 有客户端或服务器端的方法来解决这个问题吗 找到了一个很好的答案 使用 PHP 5 并使用 DOMDo
  • 在我的 index.php 中加载 CSS 和 JS 等资源时出现错误 403

    我使用的是 Linux Elementary OS 并在 opt 中安装了 lampp My CSS and JS won t load When I inspect my page through browser The console
  • PHP 无法加载动态库“php_pdo_oci.dll”

    我在 Windows 8 上运行 Apache 2 4 7 和 PHP 5 5 9 我安装了 PHPUnit 并开始弹出此警告图像 警告 是的 我在 php ini 中启用了扩展加载以及 extension dir 以更正文件夹 并且该文件
  • 获取字符串中的最后一个整数

    我需要隔离包含多个整数的字符串中最新出现的整数 我怎样才能得到23代替1 for lastnum1 text 1 out of 23 lastnum1 this gt getEval eregi replace out of text 你可
  • 如何将我的 init 函数中的代码集中到所有控制器中?

    public function init this gt view gt user Zend Auth getInstance gt getIdentity this gt view gt siteName Zend Registry ge
  • WordPress 包含 SVG 文件错误

    我使用 PHP 和 WordPress 在本地主机上 我可以毫无问题地包含 SVG 文件 但在实时服务器上 我尝试包含一个 SVG 文件以便能够使用 CSS 对其进行样式设置 我收到此错误消息 Parse error syntax erro
  • 如何通过php获取网页的Open Graph协议?

    PHP 有一个简单的命令来获取网页的元标记 get meta tags 但这仅适用于具有名称属性的元标记 然而 开放图谱协议如今变得越来越流行 从网页获取 opg 值的最简单方法是什么 例如 我看到的基本方法是通过 cURL 获取页面并使用
  • 如何在 phalcon 框架中同时连接多个数据库在模型类中同时使用两个而不仅仅是一个

    在我的代码中我有两个数据库ABC and XYZ 我想在同一模型中使用两个数据库 而不是 phalcon 中的解决方案是什么 如何为此实现多个数据库连接 one
  • 从所有会话中注销

    我有一个注销选项 这是我的代码 session start session destroy setcookie key time 60 60 24 setcookie username time 60 60 24 我想添加另一个选项来注销所
  • 如何从 Laravel 执行存储过程

    我需要在表单提交数据后执行存储过程 我让存储过程按照我想要的方式工作 并且我的表单正常工作 我只是不知道从 laravel 5 执行 sp 的语句 它应该是这样的 执行 my stored procedure 但我似乎在网上找不到类似的东西
  • 反向引用在 PHP 中不起作用

    最近我一直在研究 更多的是在实践中说实话 正则表达式 我注意到他的力量 我提出的这个要求 link https stackoverflow com questions 30380397 take the text up to a speci
  • phpActiveRecord 日期时间格式不正确

    当尝试使用 phpActiveRecord 在表中创建记录时 出现以下错误 Invalid datetime format 1292 Incorrect datetime value 2013 06 20 11 59 08 PDT for
  • PHP递归遍历对象树[关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 如何清除 APC 缓存而不使 Apache 崩溃?

    如果 APC 存储大量条目 清除它们会导致 httpd 崩溃 如果 apc clear cache user 花费的时间超过 phps max execution time 调用 apc clear cache 的脚本 将在之前被 php
  • 如何使用 php 将 *.xlsb 转换为数组或 *.csv

    我正在尝试转换 xlsb文件到php array or csv文件 或至少 xls 我尝试使用PHPExcel 但看起来它无法识别该文件中的内容 我注意到 你可以重命名 xlsb文件到 zip文件 然后使用命令行解压缩unzip zip 之

随机推荐