尝试仅从当前产品信息中获取属性(Woocommerce)

时间:2020-09-03 00:10:57

标签: php arrays wordpress object foreach

在这里,我想找出一个奇怪的难题。基本上,我使用Woocommerce上的方法,使用下面的代码片段从每个产品信息中获取产品属性。<​​/ p>

$productAttributesObject = $product->get_attributes('')

然后将这些属性存储到数组中

foreach ($productAttributesObject  as $i => $value) {
    $singleArray[] = $value->get_options();
}

以某种方式,看来我创建的foreach基本上不会从产品发布中清除值,只是将它们堆叠在一起。为了清楚解释,我有两个产品帖子

一种产品包含名为Fruits的属性,其中包含以下术语:

100% Orange Juice | Apple | Pear | Pineapple | Passionfruit | Red Dragonfruit | Baobab Powder | Grapeseed Extract

其他产品包含名为Color的属性,其中包含以下术语:

Red | Green

我希望每个循环都会将属性附加到数组中

期望

产品1属性

array(4) {
  [0]=>
  array(3) {
    [0]=>
    string(6) "400 ml"
    [1]=>
    string(2) "1L"
    [2]=>
    string(2) "2L"
  }
  [1]=>
  array(8) {
    [0]=>
    string(17) "100% Orange Juice"
    [1]=>
    string(5) "Apple"
    [2]=>
    string(4) "Pear"
    [3]=>
    string(9) "Pineapple"
    [4]=>
    string(12) "Passionfruit"
    [5]=>
    string(15) "Red Dragonfruit"
    [6]=>
    string(13) "Baobab Powder"
    [7]=>
    string(17) "Grapeseed Extract"
  }
  [2]=>
  array(1) {
    [0]=>
    string(3) "Yes"
  }
  [3]=>
  array(1) {
    [0]=>
    string(3) "Yes"
  }
}

产品2属性

array(1) {
  [4]=>
  array(1) {
    [0]=>
    string(3) "Red"
  }
}

我得到的是数组似乎彼此堆叠,并且从以前的产品中获取属性,而不是仅从当前产品中获取属性

现实

产品1属性

array(4) {
  [0]=>
  array(3) {
    [0]=>
    string(6) "400 ml"
    [1]=>
    string(2) "1L"
    [2]=>
    string(2) "2L"
  }
  [1]=>
  array(8) {
    [0]=>
    string(17) "100% Orange Juice"
    [1]=>
    string(5) "Apple"
    [2]=>
    string(4) "Pear"
    [3]=>
    string(9) "Pineapple"
    [4]=>
    string(12) "Passionfruit"
    [5]=>
    string(15) "Red Dragonfruit"
    [6]=>
    string(13) "Baobab Powder"
    [7]=>
    string(17) "Grapeseed Extract"
  }
  [2]=>
  array(1) {
    [0]=>
    string(3) "Yes"
  }
  [3]=>
  array(1) {
    [0]=>
    string(3) "Yes"
  }
}

产品2属性

array(5) {
  [0]=>
  array(3) {
    [0]=>
    string(6) "400 ml"
    [1]=>
    string(2) "1L"
    [2]=>
    string(2) "2L"
  }
  [1]=>
  array(8) {
    [0]=>
    string(17) "100% Orange Juice"
    [1]=>
    string(5) "Apple"
    [2]=>
    string(4) "Pear"
    [3]=>
    string(9) "Pineapple"
    [4]=>
    string(12) "Passionfruit"
    [5]=>
    string(15) "Red Dragonfruit"
    [6]=>
    string(13) "Baobab Powder"
    [7]=>
    string(17) "Grapeseed Extract"
  }
  [2]=>
  array(1) {
    [0]=>
    string(3) "Yes"
  }
  [3]=>
  array(1) {
    [0]=>
    string(3) "Yes"
  }
  [4]=>
  array(1) {
    [0]=>
    string(3) "Red"
  }
}

如您所见,属性颜色位于上一个索引的最后一个索引中,其余属性来自上一篇文章。

如何仅以编程方式仅从当前帖子中获取属性?这是完整的代码段

<ul class="product-list">
              
              <?php 

        $args = array(
          'post_type' => 'product',
          'posts_per_page' => 2
        );

        $the_query = new WP_Query( $args );

      ?>
      
      <?php if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?>
      
      <?php global $product ?>
      
                <li class="product-post"
                
                    <?php 
        
          $productAttributesObject = $product->get_attributes('');
                
        foreach ($productAttributesObject  as $i => $value) {
           $singleArray[] = $value->get_options();
        }
      
        foreach($singleArray as $val) { 
            foreach ($val as $v) { 
                $completeList[] = $v; 
            }
        }
        
        $arrayLowerCase = array_map('strtolower', $completeList);
        $arrayConvert = str_replace(' ', '-', $arrayLowerCase);
        $attributeTerms = implode(' ', $arrayConvert);
      
                  ?>
                  
                  data-type="<?php echo $attributeTerms ?>"
                
                 >

                  
                    <div class="product-image-container">
                      
                      <?php the_post_thumbnail('full', array('class' => 'product-image')) ?>
                        <div class="product-coaster"></div>
                    </div>
    
                    <h2 class="product-name"><?php the_title() ?></h2>
                    <?php the_excerpt() ?>
                </li>
                
                <?php endwhile; else: ?>
                
                <p>no post</p>
                
        <?php endif ?>

        <?php wp_reset_query(); ?>

</ul>

1 个答案:

答案 0 :(得分:1)

您需要为每个产品重新初始化$singleArray$completeList。为此,请添加

$singleArray = array();
$completeList = array();

在此行之前:

foreach ($productAttributesObject  as $i => $value) {