在 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 的单个产品页面上显示特定的自定义产品属性 的相关文章

  • 如何使用 phpStorm 从远程服务器删除文件

    所以我已经将远程服务器添加到 phpStrom 中 我可以在那里更改文件 但是 如果我删除文件 它们只会从我的计算机上消失 而不是在服务器上消失 如何使用 phpStorm 从远程服务器删除文件 To manually从远程位置删除文件 使
  • 如何从 PHP 的 sha256 加密迁移到 bcrypt?

    登录 rows sql gt fetch PDO FETCH ASSOC us id rows id us pass rows password us salt rows password salt status rows attempt
  • bash.sh 运行 cron 的权限被拒绝

    如何在这里使用 bash 脚本运行 cron 我做了如下操作 这里有错误 我想知道如何在 ubuntu 中做到这一点 我现在对它感到震惊 bash sh 文件 bin bash cd var www Controller usr bin p
  • 使用 PHP 将 HTML 片段包裹在 div 中(并从 HTML 标签生成目录)

    我原来的 HTML 看起来像这样 h1 Page Title h1 h2 Title of segment one h2 img src img jpg alt An image of segment one p Paragraph one
  • 如何使用WAMP登录phpMyAdmin,用户名和密码是什么?

    根 这个词是什么意思php我的管理员 http en wikipedia org wiki PhpMyAdmin 每当我写作时localhost phpmyadmin在地址栏上 我被要求输入用户名和密码 但我不知道它们是什么 我不记得何时何
  • 将变量从 PHP 发送到 Javascript

    我在两个单独的文件中有以下代码 其中一个是 javascript 另一个是 php JavaScript xmlhttp new XMLHttpRequest xmlhttp onreadystatechange function if t
  • Laravel 克隆查询字符串

    是否可以克隆一个查询字符串 以便我可以编写一次并在不影响其他结果的情况下进行长时间的更改 query DB table users gt where id 123 queryGet query queryPaginate query que
  • wkhtmltopdf 与 javascript-delay 不起作用

    使用下面的代码创建的 pdf 尚未完成 javascript 的渲染 因此 javascript 在一半的表格上执行 假设有 100 个表格 则有时完成 50 个表格 其他时候完成 52 54 个表格等 我正在使用旗帜 javascript
  • cURL 错误 (35):错误:14077458:SSL 例程:SSL23_GET_SERVER_HELLO:tlsv1 无法识别的名称

    我一直在使用以下代码块使用 cURL 从 HTTPS 网站收集数据 q https www example org for example ch curl init curl setopt ch CURLOPT URL q curl set
  • Symfony2 安全性 @Secure 注释不起作用

    我正在尝试使用注释来保护我的控制器 namespace Vinny StreamBundle Controller use Symfony Bundle FrameworkBundle Controller Controller use J
  • 干预/图像上传错误{{图像源不可读}}

    我正在尝试添加个人资料图片上传拉拉维尔 5 1 我用的是Intervention Image打包但当我尝试上传图像时出现此错误 AbstractDecoder php 第 302 行中的 NotReadableException 图像源不可
  • PHP 中 glob() 中的转义空格?

    我在 PHP 中有以下函数 除了名称中带有空格的文件外 该函数运行良好 Good picture jpg例如 这里是 function getphotolist currentalbum photos glob currentalbum J
  • 在 PHP 中读取“分块”POST 数据

    我试图在发送时使用 Transfer Encoding chunked 从请求中读取 POST 数据 但在收到所有数据之前无法启动脚本 是否可以让 PHP能够在分块请求通过时对其做出反应吗 将 PHP 5 3 8 与 Apache 结合使用
  • 开发 WordPress 管理链接重定向到实时站点

    我正在尝试对我拥有的 WordPress 网站进行新的更改 所以我复制了所有文件并导出到新的开发子域 为子域创建新数据库并从实时站点导入数据库 直播站点 http mysite com http mysite com 开发站点 http d
  • 如何设置 Zend Cache Storage 的过期时间?

    我想在 Zend 文件系统缓存中存储一 些 XML 并让它在 30 分钟后过期 如何设置缓存持续时间 过期时间 我使用 Zend 缓存作为组件 而不是在完整的 ZF2 应用程序的上下文中 cache Zend Cache StorageFa
  • 使 IPTC 数据可搜索

    我对 IPTC 元数据有疑问 是否可以通过 IPTC 元数据 关键字 搜索不在数据库中的图像并显示它们 我将如何执行此操作 我只需要一个基本的想法 我知道 PHP 有 iptcparse 函数 我已经编写了一个函数来获取画廊文件夹和所有子目
  • PDO 多查询“SQLSTATE[HY000]:一般错误”

    我仍在学习 PDO 所以我可能会错过一些东西 但基本上我正在尝试将一行插入表中 然后选择生成的 id 我不确定它是否喜欢一个 pdo 语句中的两个查询 这是我用来执行 SQL 的代码 public function ExecuteQuery
  • 从 https 切换到 http 时违反 RewriteRule

    我写了很多重写规则 in my htaccess文件 但是当我从https to http页面 它不遵守这些规则 NOTE 本地主机上一切正常 问题出在服务器上 UPDATE 这是我的website http www charityrumm
  • 如何获取PHP版本?

    有没有办法检查从该脚本中执行特定脚本的 PHP 版本 例如 下面的代码片段 version way to get version print version 将在一台机器上打印 5 3 0 在另一台机器上打印 5 3 1 version p
  • PHP 将日期与今天的日期进行比较

    我正在尝试采用以下格式的信用卡到期日期mm yy并查看该日期是否已过 以便我知道信用卡是否已过期 如果已经过期 则一类expired被插入到 tr 我的代码结果检查了 05 16 的样本日期 并且脚本显示该卡尚未过期 而显然该卡已经使用了一

随机推荐