我希望在单个产品选项卡中显示产品属性,但作为具有此属性的产品的链接.

我找到了一个这样的代码:

function cw_woo_attribute(){
    global $product;
    $attributes = $product->get_attributes();
    if ( ! $attributes ) {
        return;
    }
    $display_result = '';
    foreach ( $attributes as $attribute ) {
        if ( $attribute->get_variation() ) {
            continue;
        }
        $name = $attribute->get_name();
        if ( $attribute->is_taxonomy() ) {
            $terms = wp_get_post_terms( $product->get_id(), $name, 'all' );
            $cwtax = $terms[0]->taxonomy;
            $cw_object_taxonomy = get_taxonomy($cwtax);
            if ( isset ($cw_object_taxonomy->labels->singular_name) ) {
                $tax_label = $cw_object_taxonomy->labels->singular_name;
            } elseif ( isset( $cw_object_taxonomy->label ) ) {
                $tax_label = $cw_object_taxonomy->label;
                if ( 0 === strpos( $tax_label, 'Product ' ) ) {
                    $tax_label = substr( $tax_label, 8 );
                }
            }
            $display_result .= $tax_label . ': ';
            $tax_terms = array();
            foreach ( $terms as $term ) {
                $single_term = esc_html( $term->name );
                array_push( $tax_terms, $single_term );
            }
            $display_result .= implode(', ', $tax_terms) .  '<br />';
        } else {
            $display_result .= $name . ': ';
            $display_result .= esc_html( implode( ', ', $attribute->get_options() ) ) . '<br />';
        }
    }
    echo $display_result;
}
add_action('woocommerce_single_product_summary', 'cw_woo_attribute', 25);

而且它起作用了!

但是,如何修改此代码以使各个属性的名称成为指向分配了该属性的所有产品的链接?

我之前使用的是Isabel Castillo的‘WooCommerce Show Attributes’,但它开始与某些东西冲突,并在添加到购物车按钮上显示错误.

推荐答案

请注意,您使用的代码可以高度简化.

如果可能,使用WordPress get_term_link()将允许您显示链接的术语名称.

try 以下操作:

add_action( 'woocommerce_single_product_summary', 'woocommerce_single_product_linked_attributes', 25 );
function woocommerce_single_product_linked_attributes(){
    global $product;

    $attributes  = $product->get_attributes();
    $html_output = '';

    if ( empty($attributes) ) {
        return;
    }

    foreach ( $attributes as $attribute ) {
        if ( $attribute->get_variation() ) {
            continue;
        }
        
        if ( $attribute->is_taxonomy() ) {
            $values    = array();
            $taxonomy  = $attribute->get_taxonomy();

            foreach ( $attribute->get_terms() as $term ) {
                $term_link = get_term_link($term, $taxonomy); // Get term link

                if ( ! is_wp_error( $term_link ) ) {
                    $values[] = '<a class="attr-link" href="'. esc_url( $term_link ) . '">'. esc_html( $term->name ) . '</a>';
                } else {
                    $values[] = esc_html( $term->name );
                }
            }
            $html_output .=  wc_attribute_label($taxonomy) . ': ' . implode(', ', $values) . '<br>';
        } else {
            $html_output .= $attribute->get_name() . ': ' . esc_html( implode(', ', $attribute->get_options()) ) . '<br>';
        }
    }
    echo $html_output;
}

代码放在活动子主题(或活动主题)的function.php文件中.测试和作品.

Php相关问答推荐

根据特定运输类别增加WooCommerce购物车总重量

WooCommerce HPOS管理订单列表中带有订单元数据的定制列

目标类[Set_Locale]不存在.拉威尔

WooCommerce在购买后多次重定向

列出所有WooCommerce处理订单中的产品数量,SKU和品牌

Laravel区分大小写搜索

使用与OpenSSL命令行兼容的AES-256-CTR为大型文件创建php代码OpenSSL加密

如何从WooCommerce checkout 帐单邮箱字段中删除星号?

基于自定义域的每个购物车项目的WooCommerce定制数量输入步骤

文本到语音:不考虑音高值

如果在 APIPlatform 3.1 中的同一资源上使用处理器,Messenger 将无法工作

在 php 中生成 MAC ISO/IEC 9797-1

Laravel 发送邮箱问题 Swift_TransportException

使用现有的htaccess的PHP路由脚本

仅在管理员新订单通知中显示WooCommerce订单商品自定义元数据

Woocommerce API 的 PHP Curl 附加参数

ACF 更新字段功能不更新 WordPress 中的任何数据

如何从 PHP 中的 .txt 文件中列出今天生日的人的姓名?

使用 PHP 正则表达式从仅包含具有现有顺序的顶级标签的 XML 文档中搜索和创建数组

如何将数字文本文件添加到 PHP 数学方程式中?