【发布时间】:2020-09-30 17:17:49
【问题描述】:
我已经制作了一个表格,我在可变产品页面上显示产品数据。这个想法非常简单,但是 - 我遇到了一个我无法弄清楚的错误。
我得到“Undefined variable: variation_id”,即使我有它作为参数并且我不知道如何解决它。
代码:
add_action( 'woocommerce_single_product_summary', 'variable_product_data_as_table', 5 );
function variable_product_data_as_table(){
global $product;
if (! $product->is_type('variable')) return;
$variation = wc_get_product($variation_id);
$available_variations = $product->get_available_variations();
if( count($available_variations) > 0 ) {
$table_display = '<hr><table class="variable-data">
<thead>
<tr>
<th style="text-align: center; font-weight: bold;">'. __( 'Length', 'woocommerce' ) .'</th>
<th style="text-align: center; font-weight: bold;">'. __( 'Width', 'woocommerce' ) .'</th>
<th style="text-align: center; font-weight: bold;">'. __( 'Height', 'woocommerce' ) .'</th>
<th style="text-align: center; font-weight: bold;">'. __( 'Stock', 'woocommerce' ) .'</th>
<th style="text-align: center; font-weight: bold;">'. __( 'Price', 'woocommerce' ) .'</th>
<th style="color:red; text-align: center;" font-weight: bold;>'. __( 'Sale', 'woocommerce' ) .'</th>
</tr>
</thead>
<tbody>';
foreach( $available_variations as $variation ){
$product_variation = wc_get_product($variation['variation_id']);
$sale_price = $product_variation->get_sale_price();
if( empty( $sale_price ) ) $sale_price = __( '', 'woocommerce' );
$table_display .= '
<tr>
<td style="text-align: center;">'. $product_variation->get_length() .'</td>
<td style="text-align: center;">'. $product_variation->get_width() .'</td>
<td style="text-align: center;">'. $product_variation->get_height() .'</td>
<td style="text-align: center;">'. $product_variation->get_stock_quantity() .'</td>
<td style="text-align: center;">'. $product_variation->get_regular_price() .'</td>
<td style="color: darkgreen; text-align: center; font-weight: bold; padding-left: 5px;">'. $sale_price .'</td>
</tr>';
}
$table_display .= '
</tbody>
</table>';
echo $table_display;
echo '<hr>';
}
}
【问题讨论】:
标签: php wordpress woocommerce html-table product