在 Woocommerce 的单个产品页面上显示特定的自定义产品属性

2024-05-12

我找到了以下代码 https://isabelcastillo.com/woocommerce-product-attributes-functions在产品详细信息页面上显示所有自定义属性(具有我需要的特定条形设计)。代码的工作方式就像一个魅力,我有适当的 CSS 来显示我的自定义属性的水平条。

我的问题是我只想显示特定的命名 属性并且不知道如何更改循环来做到这一点......

function isa_woocommerce_all_pa(){

global $product;
$attributes = $product->get_attributes();

if ( ! $attributes ) {
    return;
}

$out = '<ul class="taste-attributes">';

foreach ( $attributes as $attribute ) {


    // skip variations
    if ( $attribute->get_variation() ) {
    continue;
    }
    $name = $attribute->get_name();
    if ( $attribute->is_taxonomy() ) {

        $terms = wp_get_post_terms( $product->get_id(), $name, 'all' );
        // get the taxonomy
        $tax = $terms[0]->taxonomy;
        // get the tax object
        $tax_object = get_taxonomy($tax);
        // get tax label
        if ( isset ( $tax_object->labels->singular_name ) ) {
            $tax_label = $tax_object->labels->singular_name;
        } elseif ( isset( $tax_object->label ) ) {
            $tax_label = $tax_object->label;
            // Trim label prefix since WC 3.0
            if ( 0 === strpos( $tax_label, 'Product ' ) ) {
               $tax_label = substr( $tax_label, 8 );
            }                
        }


        $out .= '<li class="' . esc_attr( $name ) . '">';
        $out .= '<p class="attribute-label">' . esc_html( $tax_label ) . ': </p> ';
        $tax_terms = array();
        foreach ( $terms as $term ) {
            $single_term = esc_html( $term->name );
            // Insert extra code here if you want to show terms as links.
            array_push( $tax_terms, $single_term );
        }
        $out .= '<span class="attribute-value">' . implode(', ', $tax_terms) . '</span><progress value="' . implode(', ', $tax_terms) .
        '" max="10"><div class="progress-bar"><span style="width:'
        . implode(', ', $tax_terms) . '0%">'
        . implode(', ', $tax_terms) . '</span></div></progress></li>';


        } else {
            $value_string = implode( ', ', $attribute->get_options() );
            $out .= '<li class="' . sanitize_title($name) . ' ' . sanitize_title( $value_string ) . '">';
            $out .= '<p class="attribute-label">' . $name . ': </p> ';
            $out .= '<progress value="' . esc_html( $value_string ) . '" max="10"></progress></li>';
        }
    }

    $out .= '</ul>';

    echo $out;
}
add_action('woocommerce_single_product_summary', 'isa_woocommerce_all_pa', 20);

在以下代码中,您将首先在数组中定义所需的产品属性 slugs,这些属性将显示在单个产品页面中:

add_action( 'woocommerce_single_product_summary', 'display_some_product_attributes', 25 );
function display_some_product_attributes(){
    // HERE define the desired product attributes to be displayed
    $defined_attributes = array('fyllighet', 'carrier', 'billing-e-number');

    global $product;
    $attributes = $product->get_attributes();

    if ( ! $attributes ) {
        return;
    }

    $out = '<ul class="taste-attributes">';

    foreach ( $attributes as $attribute ) {

        // Get the product attribute slug from the taxonomy
        $attribute_slug = str_replace( 'pa_', '', $attribute->get_name() );

        // skip all non desired product attributes
        if ( ! in_array($attribute_slug, $defined_attributes) ) {
            continue;
        }

        // skip variations
        if ( $attribute->get_variation() ) {
            continue;
        }

        $name = $attribute->get_name();

        if ( $attribute->is_taxonomy() ) {

            $terms = wp_get_post_terms( $product->get_id(), $name, 'all' );
            // get the taxonomy
            $tax = $terms[0]->taxonomy;
            // get the tax object
            $tax_object = get_taxonomy($tax);
            // get tax label
            if ( isset ( $tax_object->labels->singular_name ) ) {
                $tax_label = $tax_object->labels->singular_name;
            } elseif ( isset( $tax_object->label ) ) {
                $tax_label = $tax_object->label;
                // Trim label prefix since WC 3.0
                if ( 0 === strpos( $tax_label, 'Product ' ) ) {
                   $tax_label = substr( $tax_label, 8 );
                }                
            }

            $out .= '<li class="' . esc_attr( $name ) . '">';
            $out .= '<p class="attribute-label">' . esc_html( $tax_label ) . ': </p> ';
            $tax_terms = array();

            foreach ( $terms as $term ) {
                $single_term = esc_html( $term->name );
                // Insert extra code here if you want to show terms as links.
                array_push( $tax_terms, $single_term );
            }

            $out .= '<span class="attribute-value">' . implode(', ', $tax_terms) . '</span><progress value="' . implode(', ', $tax_terms) .
            '" max="10"><div class="progress-bar"><span style="width:'
            . implode(', ', $tax_terms) . '0%">'
            . implode(', ', $tax_terms) . '</span></div></progress></li>';

        } else {
            $value_string = implode( ', ', $attribute->get_options() );
            $out .= '<li class="' . sanitize_title($name) . ' ' . sanitize_title( $value_string ) . '">';
            $out .= '<p class="attribute-label">' . $name . ': </p> ';
            $out .= '<progress value="' . esc_html( $value_string ) . '" max="10"></progress></li>';
        }
    }

    $out .= '</ul>';

    echo $out;
}

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

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

在 Woocommerce 的单个产品页面上显示特定的自定义产品属性 的相关文章

  • 使用 PDO 准备语句使用搜索字段中的多个关键字进行 LIKE 查询

    网站用户使用搜索表单来查询产品数据库 输入的关键字在数据库中搜索产品的标题 public function startSearch keywords keywords preg split s keywords totalKeywords
  • Laravel 验证:存在附加列条件 - 自定义验证规则

    在 Laravel 中指定存在验证规则时 是否有一种方法可以引用另一个字段 我希望能够说输入 a 必须存在于表 a 中 输入 b 必须存在于表 b 中 并且表 b 中列 x 的值必须等于输入 a 最好通过例子来解释 public rules
  • 智能位置表单字段

    我的用户注册表单上有一个文本字段location 我本质上希望这个字段能够根据 Google 地图 或同等地图 进行验证 只允许有效位置通过 最好采用类似的格式滑铁卢 伦敦 or 伦敦 英国 要求 除了位置名称之外 我还想返回该位置中心的坐
  • 使用 PHP/COM/ADSI/LDAP 更改 AD 密码

    我已经被这个问题困扰了好几天了 我尝试了各种解决方案均无济于事 请帮忙 Problem 我们有两个域控制器 它们不属于我们的管理范围 我们能够通过端口 389 上的 LDAP 进行连接 但无法通过端口 636 安全连接 我们正在开发一个系统
  • 基本表创建 fpdf

    我找不到使用 fpdf 制作表格并从 mysql 数据库获取数据的合适教程 我只是想知道如何创建一个 我在网上尝试示例时遇到了很多错误 例如 我有 名字 中间名 姓氏 年龄 和 电子邮件 列 如何使用 fpdf 创建表格并回显数据库中的条目
  • 访问 public_html 级别之外/以下的文件

    如何通过 url 访问文件 home uzair etc index php 即使我运行域 something com 它显示了 home uzair public html index php 这个文件 任何人请帮助我如何访问放置在 ho
  • 如何计算加权平均值?

    我的语言是PHP 但是算法应该是相当通用的 我有一个关联数组 比方说 评级和评级次数 ratings array 1 gt 1 2 gt 3 3 gt 6 4 gt 3 5 gt 3 这相当于 1 2 2 2 3 3 3 3 3 3 4 4
  • 如何读取 XML 文件并从中获取值以在 PHP 编码的 HTML 页面中显示

    我有一个 XML 文件 其中有一些重复的标签 其中包含不同的值 我需要获取这些值并显示在我的网页中 请帮助我得到这个 如果您使用 PHP5 可以查看 SimpleXML 您可以在这里找到介绍教程 http www w3schools com
  • 加载时覆盖 WordPress 插件翻译文件

    我正在使用带有插件的法语版 WordPress活动日历 http wordpress org plugins the events calendar 该插件附带了捆绑的法语翻译 但有一些错误 我想修复它们 但替换原始文件是一个坏主意 因为它
  • PHP 相等变量

    我想知道是否有任何方法可以检查大量变量是否相等 如果我只有几个变量 我可以这样做 if a b a c b c 但是 如果我有 20 个变量 则需要一些时间来编写所有组合 还有其他方法吗 if count array unique arra
  • PHP mkdir() 和 fopen() 不起作用 - 权限问题? umask问题?

    以下 PHP 脚本无法创建目录 它也将无法创建文件 当目录已经存在时 ini set error reporting E ALL define ABSPATH SERVER DOCUMENT ROOT echo ABSPATH br br
  • 使用php将文本文件转换为xml?

    data txt ha15rs 250 home2 gif 2 ha36gs 150 home3 gif 1 ha27se 300 home4 gif 4 ha4678 200 home5 gif 5 我想使用 php 使用 simplex
  • PHP 日志文件颜色

    我正在编写一个 PHP 日志文件类 但我想为写入文件的行添加颜色 我遇到的问题是颜色也会改变终端的颜色 我想要实现的是仅更改写入日志文件的行的颜色 class logClass extends Singleton private funct
  • Yii 查询时对相关模型的限制

    我遇到了极限问题 我正在使用的代码如下 model PostCategory model record model gt with array posts gt array order gt posts createTime DESC li
  • laravel 5.4 在请求验证之前修改数据[关闭]

    Closed 这个问题是无法重现或由拼写错误引起 help closed questions 目前不接受答案 我有我的自定义请求 它扩展了 Backpack CrudController 现在我想重写 ValidatesWhenResolv
  • Laravel 8、Sanctum、Fortify /logout 在 Postman 中抛出“CSRF 令牌不匹配”

    我安装了 L8 Sanctum 和 Fortify 进行身份验证 我以前可以 login 使用了Pre request Script设置X XSRF TOKEN 我什至得到了 api user成功地 但当我这样做时 logout 我在 Po
  • 高效插入和更新时检查唯一性

    我的员工表中有 2 列 每列值必须是唯一的 staff code staff name staff id staff code staff name 1 MGT Management 2 IT IT staff 当向表中插入或更新项目时 我
  • 如何使用 PHP 获取列中的所有值?

    我一直在到处寻找这个问题 但仍然找不到解决方案 如何从 mySQL 列中获取所有值并将它们存储在数组中 例如 表名称 客户 列名称 ID 名称 行数 5 我想获取此表中所有 5 个名称的数组 我该如何去做呢 我正在使用 PHP 我试图 SE
  • 无法与站点通信以检查致命错误

    无法与站点通信以检查致命错误 因此 PHP 更改已恢复 您需要通过其他方式上传 PHP 文件更改 例如使用 SFTP 有什么解决办法 我正在 WordPress 中编辑头文件 遇到这个问题 尝试这个 我有同样的问题并决定调查一下 更改 wp
  • 如何显示 PHP 对象

    我有这样的代码 dataRecord1 client gt GetRecord token table filter echo pre print r dataRecord1 echo pre foreach dataRecord1 gt

随机推荐