php - 显示 WooCommerce 自定义产品属性和单个产品的所有条款

标签 php wordpress woocommerce custom-taxonomy taxonomy-terms

我使用自定义函数在 WooCommerce 单品页面上创建了六个自定义属性。它们包括:图标、标签和术语。

基于 Replace WooCommerce attribute labels by a custom image for each 答案代码,我使用了以下函数 (带有自定义 HTML 标记,不同于 WooCommerce product-attributes.php 模板中的标记):

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

    $attributes = $product->get_attributes();

    if ( ! $attributes ) return;

    $out = '<div class="custom-attributes">';

    foreach ( $attributes as $attribute ) {

        if ( $attribute->get_variation() ) continue;

        if ( $attribute->is_taxonomy() ) {
            
            $taxonomy = $attribute->get_name();
            $taxo_obj = $attribute->get_taxonomy_object();
            $name = $taxo_obj->attribute_name;
            $label = $taxo_obj->attribute_label;
            $label_name = wc_attribute_label( $taxonomy );

            $out .= '<div class="' . esc_attr( $taxonomy ) . ' single-attribute">';

            $out .= '<div><img class="attribute-image" src="'.get_stylesheet_directory_uri().'/woocommerce/attributes/'.$name.'.svg" alt="Attribute '.$label.'"/></div>';

            $out .= '<div class="attribute-label '.$label.'">'.$label_name.'</div>';
            
            $out .= '<div class="attribute-values">';

            $terms = wp_get_post_terms( $product->get_id(), $taxonomy, array('fields' => 'names') );

            foreach ( $terms as $term_name )
                $term_names['names'] = $term_name;

            $out .= implode(', ', $term_names);
            $out .= '</div></div>';

        } else {
            $value_string = implode( ', ', $attribute->get_options() );
            $out .= '<div class="' . sanitize_title($taxonomy) . ' ' . sanitize_title( $value_string ) . '">';
            $out .= '<div class="attribute-label">' . $taxonomy . ': </div> ';
            $out .= '<div class="attribute-value">' . esc_html( $value_string ) . '</div></div>';
        }
    }
    $out .= '</div>';

    echo $out;
}

该函数工作正常,但有一个问题:如果特定属性的项不止一个(例如颜色:红色、蓝色、绿色),该函数在屏幕上仅打印数组的最后一个值。

我阅读了文档并做了很多测试,并检查了CSS没有问题。我还在 StackOverflow 和网上阅读了几乎所有关于该主题的问题的答案,但我找不到答案。

有没有人可以帮助我了解错误在哪里?

最佳答案

问题出在您的代码中:

            $terms = wp_get_post_terms( $product->get_id(), $taxonomy, array('fields' => 'names') );

            foreach ( $terms as $term_name )
                $term_names['names'] = $term_name;

            $out .= implode(', ', $term_names);

你可以替换为这个:

            $terms = wp_get_post_terms( $product->get_id(), $taxonomy, array('fields' => 'names') );

            $term_names = []; // Initializing

            // Loop through each terms
            foreach ( $terms as $term_name ) {
                $term_names[] = $term_name;
            }

            $out .= implode(', ', $term_names);

或者用一行代码更好:

            $out .= $product->get_attribute( $taxonomy );

关于php - 显示 WooCommerce 自定义产品属性和单个产品的所有条款,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62518530/

相关文章:

php - 使用 95% 标签的图像无法正常工作

php - 我正在尝试过滤仅分配给主页类别的帖子(Laravel)

php - php哈希密码中的注册脚本

sorting - 如何在 Woocommerce 中按自定义分类法订购产品?

wordpress - Woocommerce for WP 中 wc_add_to_cart_message 钩子(Hook)的替代方案

php - Raspbian owncloud nginx [错误] 2667#0 : *4 connect() failed (111: Connection refused) while connecting to upstream

php - Redux WP 框架。一个简单的工作代码示例?

javascript - 联系表 7 - 提交时设置 Cookie

php - 结帐页面上重复的付款方式字段 [woocommerce]

php - 如何获取用户(客户)在 WooCommerce 中花费的总金额?