• <legend id='hwUYg'><style id='hwUYg'><dir id='hwUYg'><q id='hwUYg'></q></dir></style></legend>
      1. <tfoot id='hwUYg'></tfoot>

        <small id='hwUYg'></small><noframes id='hwUYg'>

        <i id='hwUYg'><tr id='hwUYg'><dt id='hwUYg'><q id='hwUYg'><span id='hwUYg'><b id='hwUYg'><form id='hwUYg'><ins id='hwUYg'></ins><ul id='hwUYg'></ul><sub id='hwUYg'></sub></form><legend id='hwUYg'></legend><bdo id='hwUYg'><pre id='hwUYg'><center id='hwUYg'></center></pre></bdo></b><th id='hwUYg'></th></span></q></dt></tr></i><div id='hwUYg'><tfoot id='hwUYg'></tfoot><dl id='hwUYg'><fieldset id='hwUYg'></fieldset></dl></div>
          <bdo id='hwUYg'></bdo><ul id='hwUYg'></ul>

        在 Woocommerce 3 中获取产品属性

        时间:2024-04-13

        <i id='zTIiu'><tr id='zTIiu'><dt id='zTIiu'><q id='zTIiu'><span id='zTIiu'><b id='zTIiu'><form id='zTIiu'><ins id='zTIiu'></ins><ul id='zTIiu'></ul><sub id='zTIiu'></sub></form><legend id='zTIiu'></legend><bdo id='zTIiu'><pre id='zTIiu'><center id='zTIiu'></center></pre></bdo></b><th id='zTIiu'></th></span></q></dt></tr></i><div id='zTIiu'><tfoot id='zTIiu'></tfoot><dl id='zTIiu'><fieldset id='zTIiu'></fieldset></dl></div>
            • <bdo id='zTIiu'></bdo><ul id='zTIiu'></ul>
              <tfoot id='zTIiu'></tfoot>

                  <tbody id='zTIiu'></tbody>

                  <legend id='zTIiu'><style id='zTIiu'><dir id='zTIiu'><q id='zTIiu'></q></dir></style></legend>

                  <small id='zTIiu'></small><noframes id='zTIiu'>

                  本文介绍了在 Woocommerce 3 中获取产品属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  更新 WooCommerce 后属性不再显示.

                  Attributes no longer displayed after I have updated WooCommerce.

                  在模板 content-single-product.php 中,如果我使用 var_dump($attribute_names); 我会得到一个对象数组.

                  In the template content-single-product.php if I use var_dump($attribute_names); I get an array of objects.

                  foreach( $attribute_names as $attribute_name ) 中,数据的范围受到保护.

                  In foreach( $attribute_names as $attribute_name ) the scope of data is protected.

                  这是这个模板的代码:

                  $attributes = $product->get_attributes();
                  
                  <?php if($attributes) {echo "<p class='product-desc-title'>Параметры</p>";}  ?>          
                  <?php foreach ( $attributes as $attribute ) : ?>          
                  <?php
                      if ( $attribute['is_taxonomy'] ) {
                            global $post;
                            $attribute_names = $attribute;
                  
                            foreach ( $attribute_names as $attribute_name ) {
                              $taxonomy = get_taxonomy( $attribute_name );
                  
                                if ( $taxonomy && ! is_wp_error( $taxonomy ) ) {
                                    $terms = wp_get_post_terms( $post->ID, $attribute_name );
                  
                                    $terms_array = array();
                                    $attrID = $attribute['name'];
                                    $paPMat = 'pa_product-material';
                                    $paPColor = 'pa_product-color';
                  
                                    // При добавлении новых атрибутов для товаров добавить новый массив с названием атрибута и слагом с приставкой "pa_"
                                    $pAttributes_array = array(
                                        array(
                                            'label' => 'Материал фасадов',
                                            'slug' => 'pa_product-material',
                                        ),
                                        array(
                                            'label' => 'Цвет',
                                            'slug' => 'pa_product-color',
                                        ),
                                        array(
                                            'label' => 'Конфигурация',
                                            'slug' => 'pa_konfiguraciya',
                                        ),
                                        array(
                                            'label' => 'Материал корпуса',
                                            'slug' => 'pa_material-kuxni',
                                        ),
                                        array(
                                            'label' => 'Форма',
                                            'slug' => 'pa_forma',
                                        ),
                                        array(
                                            'label' => 'Тип дверей',
                                            'slug' => 'pa_tip-dverej',
                                        ),
                                        array(
                                            'label' => 'Створки',
                                            'slug' => 'pa_stvorki',
                                        ),
                                  array(
                                            'label' => 'Размеры',
                                            'slug' => 'pa_razmery',
                                        ),
                  
                                    );
                  
                                    foreach ($pAttributes_array as $key => $value) {
                                        if ( ! empty( $terms ) && $attrID ===  $value['slug'] ) {
                                          foreach ( $terms as $term ) {
                                               $archive_link = get_term_link( $term->slug, $attribute_name );
                                               $full_line = '<a href="' . $archive_link . '">'. $term->name . '</a>';
                                               array_push( $terms_array, $full_line );
                                          }
                                          echo '<p class="pa-string">'. $value['label'] .': '. implode( $terms_array, ',  ' ) . '</p>';
                                        }
                                    }
                                }
                            }                                                 
                        } else {
                            $values = array_map( 'trim', explode( '|', $attribute['value'] ) );
                            echo apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );
                        }
                  ?>
                  <?php endforeach; ?>
                  

                  推荐答案

                  更新: 压缩代码以进行测试:

                  Updated: Compacting your code for testing:

                  $attributes = $product->get_attributes();
                  foreach ( $attributes as $attribute ):
                      $attribute_names = $attribute;
                      // testing output
                      var_dump($attribute_name);
                  endforeach;
                  

                  var_dump($attribute_name); 原始输出为您提供有关 WC_Product_Attribute 对象,这意味着您必须使用 这个类.

                  The var_dump($attribute_name); raw output gives you indications about the objects which are WC_Product_Attribute objects, meaning that you have to use the available methods for this class.

                  有两种方法:

                  1) 您可以使用 get_data() 方法如下:

                  1) You can access properties in an unprotected array using the get_data() method this way:

                  $attributes = $product->get_attributes();
                  foreach ( $attributes as $attribute ):
                      $attribute_data = $attribute->get_data();
                      // testing pre-formatted output
                      echo '<pre>'; print_r($attribute_data); echo '</pre>'; 
                      // We stop the loop to get the first object only (for testing)
                      break; 
                  endforeach;
                  

                  这将为您提供如下原始输出:

                  That will gives you a raw output like:

                  Array (
                      [id] => 1
                      [name] => pa_color
                      [options] => Array (
                          [0] => 8
                          [1] => 9
                      )
                      [position] => 0
                      [visible] => 
                      [variation] => 1
                      [is_visible] => 0
                      [is_variation] => 1
                      [is_taxonomy] => 1
                      [value] => 
                  )
                  

                  然后你可以这样使用它:

                  And then you can use it this way:

                  $attributes = $product->get_attributes();
                  foreach ( $attributes as $attribute ):
                      $attribute_data = $attribute->get_data(); // Get the data in an array
                  
                      $attribute_name = $attribute_data['name']; // The taxonomy slug name
                      $attribute_terms = $attribute_data['options']; // The terms Ids
                  endforeach;
                  

                  2) 您可以使用 WC_Product_Attribute 方法如:

                  2) You can use the WC_Product_Attribute methods like:

                  $attributes = $product->get_attributes();
                  foreach ( $attributes as $attribute ):
                      $attribute_name = $attribute->get_taxonomy(); // The taxonomy slug name
                      $attribute_terms = $attribute->get_terms(); // The terms
                      $attribute_slugs = $vaattributeues->get_slugs(); // The term slugs
                  endforeach;
                  

                  这篇关于在 Woocommerce 3 中获取产品属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:PHP:DOMDocument - 带有冒号的属性? 下一篇:根据产品属性添加自定义 woocommerce 电子邮件

                  相关文章

                    <small id='mLR6f'></small><noframes id='mLR6f'>

                    <i id='mLR6f'><tr id='mLR6f'><dt id='mLR6f'><q id='mLR6f'><span id='mLR6f'><b id='mLR6f'><form id='mLR6f'><ins id='mLR6f'></ins><ul id='mLR6f'></ul><sub id='mLR6f'></sub></form><legend id='mLR6f'></legend><bdo id='mLR6f'><pre id='mLR6f'><center id='mLR6f'></center></pre></bdo></b><th id='mLR6f'></th></span></q></dt></tr></i><div id='mLR6f'><tfoot id='mLR6f'></tfoot><dl id='mLR6f'><fieldset id='mLR6f'></fieldset></dl></div>

                    • <bdo id='mLR6f'></bdo><ul id='mLR6f'></ul>

                    <legend id='mLR6f'><style id='mLR6f'><dir id='mLR6f'><q id='mLR6f'></q></dir></style></legend>

                    <tfoot id='mLR6f'></tfoot>