在Woocommerce中,foreach循环可以工作但在日志中会产生错误

时间:2018-05-22 10:51:55

标签: php wordpress foreach woocommerce product

我正在使用以下代码创建一个很好的表,在单个产品页面上的产品摘要下,但它导致日志错误,这似乎会减慢网站的速度。谁能告诉我我做错了什么?

错误如下:

  

[22-May-2018 10:39:51 UTC] PHP警告:/www1 / 84b / www.dizzydev9.co.uk/web/wp-content/themes/IHS/为foreach()提供的参数无效第750行的functions.php

相关代码:

add_action('woocommerce_after_single_product_summary', function() {

global $product;
$mols700ma_values = get_the_terms( $product->id, 'pa_mol-s700ma');
$mols300ma_values = get_the_terms( $product->id, 'pa_mol-s300ma');
$fluxtotal_values = get_the_terms( $product->id, 'pa_total-radiometric-flux-mw');
$din5031300_values = get_the_terms( $product->id, 'pa_din5031-10-360-780nm');
$din5031700_values = get_the_terms( $product->id, 'pa_din5031-10-400-700nm');
$mccree300_values = get_the_terms( $product->id, 'pa_adjusted-mccree-360-780nm');
$mccree700_values = get_the_terms( $product->id, 'pa_adjusted-mccree-400-700');


echo '<table id="parametrics"><tr>
<th class="rotate"><div><span>240-790nm Total radiometric flux (mW)</span> 
</div></th>
<th class="rotate"><div><span>PAR 400-700nm PPF µmol/s</span></div></th>
<th class="rotate"><div><span>PAR 360-780nm BPF µmol/s</span></div></th>
<th class="rotate"><div><span>PAR (adjusted DIN5031-10) 400-700nm BPF 
µmol/s</span></div></th>
<th class="rotate"><div><span>PAR (adjusted DIN5031-10) 360-780nm BPF 
µmol/s</span></div></th>
<th class="rotate"><div><span>PAR (adjusted McCree) 400-700nm BPF 
µmol/s</span></div></th>
<th class="rotate"><div><span>PAR (adjusted McCree) 360-780nm BPF 
µmol/s</span></div></th>
</tr>';
  echo '<tr><td>' ;
foreach ( $fluxtotal_values as $fluxtotal_value ) {
    echo $fluxtotal_value->name;
}
echo'</td><td>';    
foreach ( $mols700ma_values as $mols700ma_value ) {
    echo $mols700ma_value->name;
}
echo'</td><td>';
foreach ( $mols300ma_values as $mols300ma_value ) {
    echo $mols300ma_value->name;
}
echo'</td><td>';
foreach ( $din5031700_values as $din5031700_value ) {
    echo $din5031700_value->name;
}
echo'</td><td>';
foreach ( $din5031300_values as $din5031300_value ) {
    echo $din5031300_value->name;
}
echo'</td><td>';
foreach ( $mccree700_values as $mccree700_value ) {
    echo $mccree700_value->name;
}
echo'</td><td>';        
foreach ( $mccree300_values as $mccree300_value ) {
    echo $mccree300_value->name;
}
echo'</td></tr></table>';
}, 1);

1 个答案:

答案 0 :(得分:0)

我终于找到了你的问题错误的来源:你需要在foreach循环之前测试每个术语数组是否为空,以避免错误。

我决定以一种非常紧凑的方式完全重新审视您的代码。试试这个:

add_action('woocommerce_after_single_product_summary', 'product_attributes_custom_table', 1 );
function product_attributes_custom_table() {
    global $product;

    $product_attributes = array();

    // Set product attribute taxonomy and labels pairs in an array
    $keys_labels = array(
        'pa_total-radiometric-flux-mw'  => __('240-790nm Total radiometric flux (mW)'),
        'pa_mol-s700ma'                 => __('PAR 400-700nm PPF µmol/s'),
        'pa_mol-s300ma'                 => __('PAR 360-780nm BPF µmol/s'),
        'pa_din5031-10-400-700nm'       => __('PAR (adjusted DIN5031-10) 400-700nm BPF µmol/s'),
        'pa_din5031-10-360-780nm'       => __('PAR (adjusted DIN5031-10) 360-780nm BPF µmol/s'),
        'pa_adjusted-mccree-400-700'    => __('PAR (adjusted McCree) 400-700nm BPF µmol/s'),
        'pa_adjusted-mccree-360-780nm'  => __('PAR (adjusted McCree) 360-780nm BPF µmol/s'),
    );

    // Loop through the array
    foreach( $keys_labels as $key => $label ){
        if( taxonomy_exists($key) ){
            $terms = get_the_terms( $product->get_id(), $key );
            if($terms){
                // Change $terms in a coma separated list of term names
                $term_names = join(', ', wp_list_pluck($terms, 'name'));
                // Set label and the coresponding term names in the array
                $product_attributes[$label] = $term_names;
            }
        }
    }

    echo '<table id="parametrics"><tr>';

    // First loop for the labels
    foreach($product_attributes as $label => $term_names ){
        echo '<th class="rotate"><div><span>'.$label.'</span></div></th>';
    }

    echo '</tr><tr>';

    // Second loop for the term names
    foreach($product_attributes as $label => $term_names ){
        echo '<td>'.$term_names.'</td>';
    }

    echo'</td></tr></table>';
}

代码放在活动子主题(或活动主题)的function.php文件中。经过测试和工作。