在 WooCommerce 中获取可变产品的所有变体属性和价格
Get all variations attributes with prices for a variable product in WooCommerce
有必要以尊重的方式展示属性变化"Regular Price"。然而,尽管尝试显示价格没有成功。请查看下面显示变化罚款的代码。请帮忙显示价格。
//Getting product attributes
$product_attributes = $product->get_attributes();
if(!empty($product_attributes)){
//Getting product attributes slugs
$product_attribute_slugs = array_keys($product_attributes);
$count_slug = 0;
echo '<div class="product_attributes">';
foreach ($product_attribute_slugs as $product_attribute_slug){
$count_slug++;
// Removing "pa_" from attribute slug and adding a cap to first letter
$attribute_name = ucfirst( str_replace('pa_', '', $product_attribute_slug) );
//echo $attribute_name . ' (';
$attribute_values = get_terms($product_attribute_slug);
$count_value = 0;
//print_r(array_values($available_variations));
foreach($attribute_values as $attribute_value){
$count_value++;
$attribute_name_value = $attribute_value->name; // name value
$attribute_slug_value = $attribute_value->slug; // slug value
$attribute_slug_value = $attribute_value->term_id; // ID value
echo $attribute_name_value;
}
}
echo '</div>';
//print_r(array_values($attribute_values));
}
您可以通过这种方式获取可变产品中所有产品变体所需的所有数据:
if($product->is_type('variable')){
foreach($product->get_available_variations() as $variation ){
// Variation ID
$variation_id = $variation['variation_id'];
echo '<div class="product-variation variation-id-'.$variation_id.'">
<strong>Variation id</strong>: '.$variation_id.'<br>';
// Attributes
$attributes = array();
foreach( $variation['attributes'] as $key => $value ){
$taxonomy = str_replace('attribute_', '', $key );
$taxonomy_label = get_taxonomy( $taxonomy )->labels->singular_name;
$term_name = get_term_by( 'slug', $value, $taxonomy )->name;
$attributes[] = $taxonomy_label.': '.$term_name;
}
echo '<span class="variation-attributes">
<strong>Attributes</strong>: '.implode( ' | ', $attributes ).'</span><br>';
// Prices
$active_price = floatval($variation['display_price']); // Active price
$regular_price = floatval($variation['display_regular_price']); // Regular Price
if( $active_price != $regular_price ){
$sale_price = $active_price; // Sale Price
}
echo '<span class="variation-prices">
<strong>Price</strong>: '.$variation['price_html'].'</span><br>
</div>';
}
}
此代码已经过测试并且有效。
有必要以尊重的方式展示属性变化"Regular Price"。然而,尽管尝试显示价格没有成功。请查看下面显示变化罚款的代码。请帮忙显示价格。
//Getting product attributes
$product_attributes = $product->get_attributes();
if(!empty($product_attributes)){
//Getting product attributes slugs
$product_attribute_slugs = array_keys($product_attributes);
$count_slug = 0;
echo '<div class="product_attributes">';
foreach ($product_attribute_slugs as $product_attribute_slug){
$count_slug++;
// Removing "pa_" from attribute slug and adding a cap to first letter
$attribute_name = ucfirst( str_replace('pa_', '', $product_attribute_slug) );
//echo $attribute_name . ' (';
$attribute_values = get_terms($product_attribute_slug);
$count_value = 0;
//print_r(array_values($available_variations));
foreach($attribute_values as $attribute_value){
$count_value++;
$attribute_name_value = $attribute_value->name; // name value
$attribute_slug_value = $attribute_value->slug; // slug value
$attribute_slug_value = $attribute_value->term_id; // ID value
echo $attribute_name_value;
}
}
echo '</div>';
//print_r(array_values($attribute_values));
}
您可以通过这种方式获取可变产品中所有产品变体所需的所有数据:
if($product->is_type('variable')){
foreach($product->get_available_variations() as $variation ){
// Variation ID
$variation_id = $variation['variation_id'];
echo '<div class="product-variation variation-id-'.$variation_id.'">
<strong>Variation id</strong>: '.$variation_id.'<br>';
// Attributes
$attributes = array();
foreach( $variation['attributes'] as $key => $value ){
$taxonomy = str_replace('attribute_', '', $key );
$taxonomy_label = get_taxonomy( $taxonomy )->labels->singular_name;
$term_name = get_term_by( 'slug', $value, $taxonomy )->name;
$attributes[] = $taxonomy_label.': '.$term_name;
}
echo '<span class="variation-attributes">
<strong>Attributes</strong>: '.implode( ' | ', $attributes ).'</span><br>';
// Prices
$active_price = floatval($variation['display_price']); // Active price
$regular_price = floatval($variation['display_regular_price']); // Regular Price
if( $active_price != $regular_price ){
$sale_price = $active_price; // Sale Price
}
echo '<span class="variation-prices">
<strong>Price</strong>: '.$variation['price_html'].'</span><br>
</div>';
}
}
此代码已经过测试并且有效。