WooCommerce 可变产品:在 HTML table 中显示一些变体值
WooCommerce variable products: Display some variations values in an HTML table
在 Woocommerce 中,我想创建一个函数,输出一个简单的 HTML table 和 height
、width
、regular price
和 sale price
对于 变量产品 的每个 variation
。
例如,假设变量产品具有三个不同维度的变体,我需要让我的函数输出这个 HTML:
<table>
<thead>
<tr>
<th>Height</th>
<th>Width</th>
<th>Regular price</th>
<th>Sale price</th>
</tr>
</thead>
<tbody>
<tr>
<td>180cm</td>
<td>100cm</td>
<td>224€</td>
<td>176€</td>
</tr>
<tr>
<td>210cm</td>
<td>125cm</td>
<td>248€</td>
<td>200€</td>
</tr>
<tr>
<td>240cm</td>
<td>145cm</td>
<td>288€</td>
<td>226€</td>
</tr>
</tbody>
我不确定如何为此构建一个函数,所以我可以将它添加到 content-single-product.php
woocommerce_after_single_product
操作中 content-single-product.php
.
如何做到这一点?
非常感谢任何帮助。
更新 (于 2018-03-27 - 仅限可变产品,避免错误)
以下是在 woocommerce_after_single_product
动作挂钩中实现挂钩的正确方法:
add_action( 'woocommerce_after_single_product', 'custom_table_after_single_product' );
function custom_table_after_single_product(){
global $product;
// Only for variable products
if( ! $product->is_type('variable')) return;
$available_variations = $product->get_available_variations();
if( count($available_variations) > 0 ){
$output = '<table>
<thead>
<tr>
<th>'. __( 'Height', 'woocommerce' ) .'</th>
<th>'. __( 'Width', 'woocommerce' ) .'</th>
<th>'. __( 'Regular price', 'woocommerce' ) .'</th>
<th>'. __( 'Sale price', 'woocommerce' ) .'</th>
</tr>
</thead>
<tbody>';
foreach( $available_variations as $variation ){
// Get an instance of the WC_Product_Variation object
$product_variation = wc_get_product($variation['variation_id']);
$sale_price = $product_variation->get_sale_price();
if( empty( $sale_price ) ) $sale_price = __( '<em>(empty)</em>', 'woocommerce' );
$output .= '
<tr>
<td>'. $product_variation->get_height() .'</td>
<td>'. $product_variation->get_width() .'</td>
<td>'. $product_variation->get_regular_price() .'</td>
<td>'. $sale_price .'</td>
</tr>';
}
$output .= '
</tbody>
</table>';
echo $output;
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
所有代码都在 Woocommerce 3+ 上测试过并且有效。你可以view additional hooks here…
在 Woocommerce 中,我想创建一个函数,输出一个简单的 HTML table 和 height
、width
、regular price
和 sale price
对于 变量产品 的每个 variation
。
例如,假设变量产品具有三个不同维度的变体,我需要让我的函数输出这个 HTML:
<table>
<thead>
<tr>
<th>Height</th>
<th>Width</th>
<th>Regular price</th>
<th>Sale price</th>
</tr>
</thead>
<tbody>
<tr>
<td>180cm</td>
<td>100cm</td>
<td>224€</td>
<td>176€</td>
</tr>
<tr>
<td>210cm</td>
<td>125cm</td>
<td>248€</td>
<td>200€</td>
</tr>
<tr>
<td>240cm</td>
<td>145cm</td>
<td>288€</td>
<td>226€</td>
</tr>
</tbody>
我不确定如何为此构建一个函数,所以我可以将它添加到 content-single-product.php
woocommerce_after_single_product
操作中 content-single-product.php
.
如何做到这一点?
非常感谢任何帮助。
更新 (于 2018-03-27 - 仅限可变产品,避免错误)
以下是在 woocommerce_after_single_product
动作挂钩中实现挂钩的正确方法:
add_action( 'woocommerce_after_single_product', 'custom_table_after_single_product' );
function custom_table_after_single_product(){
global $product;
// Only for variable products
if( ! $product->is_type('variable')) return;
$available_variations = $product->get_available_variations();
if( count($available_variations) > 0 ){
$output = '<table>
<thead>
<tr>
<th>'. __( 'Height', 'woocommerce' ) .'</th>
<th>'. __( 'Width', 'woocommerce' ) .'</th>
<th>'. __( 'Regular price', 'woocommerce' ) .'</th>
<th>'. __( 'Sale price', 'woocommerce' ) .'</th>
</tr>
</thead>
<tbody>';
foreach( $available_variations as $variation ){
// Get an instance of the WC_Product_Variation object
$product_variation = wc_get_product($variation['variation_id']);
$sale_price = $product_variation->get_sale_price();
if( empty( $sale_price ) ) $sale_price = __( '<em>(empty)</em>', 'woocommerce' );
$output .= '
<tr>
<td>'. $product_variation->get_height() .'</td>
<td>'. $product_variation->get_width() .'</td>
<td>'. $product_variation->get_regular_price() .'</td>
<td>'. $sale_price .'</td>
</tr>';
}
$output .= '
</tbody>
</table>';
echo $output;
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
所有代码都在 Woocommerce 3+ 上测试过并且有效。你可以view additional hooks here…