【问题标题】:Display WooCommerce Custom Product Attributes and all terms on single products显示 WooCommerce 自定义产品属性和单个产品的所有条款
【发布时间】:2020-10-12 13:21:51
【问题描述】:

我在 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 和在线阅读了几乎所有关于该主题的问题的答案,但我找不到答案。

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

【问题讨论】:

  • 更新。我以这种方式$out .= implode( ', ', $terms ); 编辑'$out .= implode( ', ', $term_names ); 行,现在它可以工作了(它跳过了foreach)。但我不确定我所做的是否正确。
  • 更新。我在下面从@LoicTheAztec 收到的答案清楚地解决了我的问题,也澄清了我的不确定性。

标签: php wordpress woocommerce custom-taxonomy taxonomy-terms


【解决方案1】:

问题来自您的代码中的以下内容:

            $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 );

【讨论】:

  • 我已经测试了您提出的两种解决方案,并确认它们都可以正常工作。特别是后者显然更便宜。您的回答清楚地回答了我的疑问,也最多解决了我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-20
  • 1970-01-01
  • 2019-06-26
  • 1970-01-01
相关资源
最近更新 更多