WooCommerce变量产品:在HTML表格中显示一些变体值 [英] WooCommerce variable products: Display some variations values in an HTML table
问题描述
height
, width
, 常规价格
和销售价格
对于每个变化
强>可变产品。 例如,假设变量产品带有三个不同维度的变体,我需要将我的函数输出为HTML:
< table>
< thead>
< tr>
< th>高度< / th>
< th>宽度< / th>
< th>正常价格< / th>
销售价格< / 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>
我不知道如何为此,我可以将它添加到 woocommerce_after_single_product
动作中 content-single-product.php
。
如何做到这一点?
非常感谢。
更新 (于2018-03-27 - 限制为可变产品,避免错误)
以下是在 woocommerce_after_single_product $
$ b
add_action('woocommerce_after_single_product','custom_table_after_single_product'); code> 动作钩子
函数custom_table_after_single_product(){
global $ product;
//仅适用于变量产品
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''。 __('宽度','woocommerce')。'< / th>
th''。 __('正常价格','woocommerce')。'< / th>
th''。 __('销售价格','woocommerce')。'< / th>
< / tr>
< / thead>
< tbody>';
foreach($ available_variations as $ variation){
//获取WC_Product_Variation对象的实例
$ 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;
$ b 代码放在function.php文件中您的活动孩子主题(或主题)或任何插件文件。
所有代码都在Woocommerce 3+上测试过并且可以正常工作。您可以
In Woocommerce, I would like to create a function which outputs a simple HTML table with height
, width
, regular price
and sale price
for each variation
of a variable product.
For example, let's say that the variable product comes with three variations with different dimensions and I need to make my function output this 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>
I am not sure how to build a function for this so I can add it into woocommerce_after_single_product
action inside content-single-product.php
.
How this could be done?
Any help is much appreciated.
解决方案 Update (on 2018-03-27 - Restricted to variable products only, avoiding an error)
Here is the correct way to achieve hooking it in woocommerce_after_single_product
action hook:
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;
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
All code is tested on Woocommerce 3+ and works. You can view additional hooks here…
这篇关于WooCommerce变量产品:在HTML表格中显示一些变体值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!