php - 在 Woocommerce 3 中获取产品属性

标签 php wordpress woocommerce attributes product

更新 WooCommerce 后属性不再显示。

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

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

这是这个模板的代码:

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

最佳答案

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

$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对象,这意味着您必须使用 this class 的可用方法.

有两种方法:

1) 您可以使用 get_data() 访问未 protected 数组中的属性方法是这样的:

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

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

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] => 
)

然后你可以这样使用它:

$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方法如:

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

关于php - 在 Woocommerce 3 中获取产品属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47542209/

相关文章:

php - 配置 : error: Please reinstall the BZip2 distribution

php - 如何获取 WordPress 帖子缩略图(特色图片)网址?

php - 如何以编程方式获取/设置产品价格?

php - WooCommerce 按 ID 限制产品

php - Laravel 5 - 调用未定义的方法 Illuminate\Database\Eloquent\Collection::Paginate()

php - Mysql - SQL 查询计数器每天、每月、每年和总计的 Web 访问量

php - 使用 PDO 在两个数据库之间传输 mysql 表

php - 在wordpress中按自定义字段排序

html - 菜单栏中的图像按钮,如何缩放到页面宽度

javascript - Woocommerce 感谢页面中的 Linkwise Affiliate 集成