【发布时间】:2020-12-22 04:26:54
【问题描述】:
我在这里遇到了一个奇怪的困境,我正在努力解决。基本上我在 Woocommerce 上使用方法来使用下面的 sn-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"
}
}
如您所见,颜色属性与上一篇文章中的其余属性位于最后一个索引处。
如何以编程方式仅从当前帖子中获取属性?这是完整的sn-p代码
<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>
【问题讨论】:
-
您需要为每个产品重新初始化
$singleArray和$completeList,在foreach ($productAttributesObject as $i => $value)循环之前添加$singleArray = array(); $completeList = array(); -
哈哈哈该死的,我不知道它这么简单。在这里,我拉着头发试图弄清楚这一点
-
我们都去过那里......
-
@Nick 你能把你的评论变成一个答案,这样我就可以把它标记为已解决:)
标签: php arrays wordpress object foreach