doubutao6216 2017-07-20 19:57
浏览 11

如何在php中查询数组中的选定键

This is not a Woocommerce-specific question! This is a question about the loop in the included code.

In working with Woocommerce's product-attributes.php template, there is a loop that cycles through attributes, displaying them, along with their terms.

In the attached image you can see the table that is output. variable single product page

You can check out the code in question at the bottom of this post.

My problem is I only want the term of the current variable product selected to be displayed after its attribute label. As you can see in the image, I have a 'dimension' selected, yet all of the terms of an associated attribute are looped through and displayed in the table (ie, the product should only display a single weight (term)). How do I select the 1 term that is stored with the variable product's attribute? I feel like the array query in the following line $attribute_values = wc_get_product_terms( $product->get_id(), $attribute->get_name(), array( 'fields' => 'all' ) ); is key but I'm not sure how it works.

Research

I've searched seemingly everywhere on Woocommerce's forums, here on Stackoverflow, Google. I've been through the files and isolated the issue to this one template. I'm at wit's end. Can anyone help?

<?php foreach ( $attributes as $attribute ) : ?>
    <tr>
        <th><?php echo wc_attribute_label( $attribute->get_name() ); ?></th>
        <td><?php
            $values = array();

            if ( $attribute->is_taxonomy() ) {
                $attribute_taxonomy = $attribute->get_taxonomy_object();
                $attribute_values = wc_get_product_terms( $product->get_id(), $attribute->get_name(), array( 'fields' => 'all' ) );

                foreach ( $attribute_values as $attribute_value ) {
                    $value_name = esc_html( $attribute_value->name );

                    if ( $attribute_taxonomy->attribute_public ) {
                        $values[] = '<a href="' . esc_url( get_term_link( $attribute_value->term_id, $attribute->get_name() ) ) . '" rel="tag">' . $value_name . '</a>';
                    } else {
                        $values[] = $value_name;
                    }
                }
            } else {
                $values = $attribute->get_options();

                foreach ( $values as &$value ) {
                    $value = make_clickable( esc_html( $value ) );
                }
            }

            echo apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );
        ?></td>
    </tr>
<?php endforeach; ?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
    • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
    • ¥30 Unity接入微信SDK 无法开启摄像头
    • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
    • ¥20 cad图纸,chx-3六轴码垛机器人
    • ¥15 移动摄像头专网需要解vlan
    • ¥20 access多表提取相同字段数据并合并
    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算
    • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow