Home / Forums / WoodMart support forum / Lable percentage for variable product
Home › Forums › WoodMart support forum › Lable percentage for variable product
Lable percentage for variable product
- This topic has 8 replies, 2 voices, and was last updated 6 years, 1 month ago by
Elise Noromit.
-
AuthorPosts
-
October 8, 2019 at 11:05 am #149070
tvojspletParticipantHi, how can i enable to show percentage discout for variable product. I talk to plugin author and they said that maybe in theme is only percenatge enabled for _post_product and not variable.
We use WooCommerce Show Single Variations witch show variation on shop page.Attachments:
You must be logged in to view attached files.October 8, 2019 at 12:43 pm #149094
Elise NoromitMemberHello,
You can find this option in the Theme Settings > Shop > Product labels https://prnt.sc/pgg13x
Best Regards
October 8, 2019 at 12:44 pm #149095
tvojspletParticipantI know. But it only show for simple product not for variable product. More specific for single variation.
October 8, 2019 at 12:47 pm #149097
tvojspletParticipantIf i add this code it shows percentage on all product but not in your lable.
add_filter( ‘woocommerce_sale_flash’, ‘add_percentage_to_sale_badge’, 20, 3 );
function add_percentage_to_sale_badge( $html, $post, $product ) {
if( $product->is_type(‘variable’)){
$percentages = array();// Get all variation prices
$prices = $product->get_variation_prices();// Loop through variation prices
foreach( $prices[‘price’] as $key => $price ){
// Only on sale variations
if( $prices[‘regular_price’][$key] !== $price ){
// Calculate and set in the array the percentage for each variation on sale
$percentages[] = round(100 – ($prices[‘sale_price’][$key] / $prices[‘regular_price’][$key] * 100));
}
}
// We keep the highest value
$percentage = max($percentages) . ‘%’;
} else {
$regular_price = (float) $product->get_regular_price();
$sale_price = (float) $product->get_sale_price();$percentage = round(100 – ($sale_price / $regular_price * 100)) . ‘%’;
}
return ‘<span class=”onsale”>’ . esc_html__( ‘SALE’, ‘woocommerce’ ) . ‘ ‘ . $percentage . ‘</span>’;
}October 8, 2019 at 3:00 pm #149122
Elise NoromitMemberHello,
Unfortunately, the labels are not compatible with the plugin you provide.
Best Regards
October 9, 2019 at 6:52 am #149192
tvojspletParticipantIt is not compactibility issue is the issue in code where it doesn’t take varable price for percentage. If i add code aboww with parameter $prices = $product->get_variation_prices(); it shows percentage discount but only outside box.
Attachments:
You must be logged in to view attached files.October 9, 2019 at 9:33 am #149234
Elise NoromitMemberHello,
Please provide the page URL I will provide custom CSS to make it as other labels on the product grid.
Best Regards
October 9, 2019 at 10:45 am #149254
tvojspletParticipantI have solved problem. In your function you didn’t have option to get variable price. I have changet your code from:
if( ! function_exists( ‘woodmart_product_label’ ) ) {
function woodmart_product_label() {
global $product;$output = array();
$product_attributes = woodmart_get_product_attributes_label();
$percentage_label = woodmart_get_opt( ‘percentage_label’ );if ( $product->is_on_sale() ) {
$percentage = ”;
if ( $product->get_type() == ‘variable’ && $percentage_label ) {
$available_variations = $product->get_variation_prices();
$max_percentage = 0;foreach( $available_variations[‘regular_price’] as $key => $regular_price ) {
$sale_price = $available_variations[‘sale_price’][$key];if ( $sale_price < $regular_price ) {
$percentage = round( ( ( $regular_price – $sale_price ) / $regular_price ) * 100 );if ( $percentage > $max_percentage ) {
$max_percentage = $percentage;
}
}
}$percentage = $max_percentage;
} elseif ( ( $product->get_type() == ‘simple’ || $product->get_type() == ‘external’ ) && $percentage_label ) {
$percentage = round( ( ( $product->get_regular_price() – $product->get_sale_price() ) / $product->get_regular_price() ) * 100 );
}if ( $percentage ) {
$output[] = ‘<span class=”onsale product-label”>-‘ . $percentage . ‘%’ . ‘</span>’;
}else{
$output[] = ‘<span class=”onsale product-label”>’ . esc_html__( ‘Sale’, ‘woodmart’ ) . ‘</span>’;
}
}if( !$product->is_in_stock() ){
$output[] = ‘<span class=”out-of-stock product-label”>’ . esc_html__( ‘Sold out’, ‘woodmart’ ) . ‘</span>’;
}if ( $product->is_featured() && woodmart_get_opt( ‘hot_label’ ) ) {
$output[] = ‘<span class=”featured product-label”>’ . esc_html__( ‘Hot’, ‘woodmart’ ) . ‘</span>’;
}if ( get_post_meta( get_the_ID(), ‘_woodmart_new_label’, true ) && woodmart_get_opt( ‘new_label’ ) ) {
$output[] = ‘<span class=”new product-label”>’ . esc_html__( ‘New’, ‘woodmart’ ) . ‘</span>’;
}if ( $product_attributes ) {
foreach ( $product_attributes as $attribute ) {
$output[] = $attribute;
}
}if ( $output ) {
echo ‘<div class=”product-labels labels-‘ . woodmart_get_opt( ‘label_shape’ ) . ‘”>’ . implode( ”, $output ) . ‘</div>’;
}
}
}
add_filter( ‘woocommerce_sale_flash’, ‘woodmart_product_label’, 10 );TO:
if( ! function_exists( ‘woodmart_product_label’ ) ) {
function woodmart_product_label() {
global $product;$output = array();
$product_attributes = woodmart_get_product_attributes_label();
$percentage_label = woodmart_get_opt( ‘percentage_label’ );if ( $product->is_on_sale() ) {
if($product->is_type( ‘variable’ ) )
{
$regular_price = $product->get_variation_regular_price();
$sale_price = $product->get_variation_price();
} else {
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
}$percentage = round( ( ( $regular_price – $sale_price ) / $regular_price ) * 100 );
if ( $product->get_type() == ‘variable’ && $percentage_label ) {
$available_variations = $product->get_variation_prices();
$max_percentage = 0;foreach( $available_variations[‘regular_price’] as $key => $regular_price ) {
$sale_price = $available_variations[‘sale_price’][$key];if ( $sale_price < $regular_price ) {
$percentage = round( ( ( $regular_price – $sale_price ) / $regular_price ) * 100 );if ( $percentage > $max_percentage ) {
$max_percentage = $percentage;
}
}
}$percentage = $max_percentage;
} elseif ( ( $product->get_type() == ‘simple’ || $product->get_type() == ‘external’ ) && $percentage_label ) {
$percentage = round( ( ( $product->get_regular_price() – $product->get_sale_price() ) / $product->get_regular_price() ) * 100 );
}if ( $percentage ) {
$output[] = ‘<span class=”onsale product-label”>-‘ . $percentage . ‘%’ . ‘</span>’;
}else{
$output[] = ‘<span class=”onsale product-label”>-‘ . $percentage . ‘%’ . ‘</span>’;
}
}if( !$product->is_in_stock() ){
$output[] = ‘<span class=”out-of-stock product-label”>’ . esc_html__( ‘Sold out’, ‘woodmart’ ) . ‘</span>’;
}if ( $product->is_featured() && woodmart_get_opt( ‘hot_label’ ) ) {
$output[] = ‘<span class=”featured product-label”>’ . esc_html__( ‘Hot’, ‘woodmart’ ) . ‘</span>’;
}if ( get_post_meta( get_the_ID(), ‘_woodmart_new_label’, true ) && woodmart_get_opt( ‘new_label’ ) ) {
$output[] = ‘<span class=”new product-label”>’ . esc_html__( ‘New’, ‘woodmart’ ) . ‘</span>’;
}if ( $product_attributes ) {
foreach ( $product_attributes as $attribute ) {
$output[] = $attribute;
}
}if ( $output ) {
echo ‘<div class=”product-labels labels-‘ . woodmart_get_opt( ‘label_shape’ ) . ‘”>’ . implode( ”, $output ) . ‘</div>’;
}
}
}
add_filter( ‘woocommerce_sale_flash’, ‘woodmart_product_label’, 10 );October 9, 2019 at 11:13 am #149258
Elise NoromitMemberThat is fine. If you have any questions please feel free to contact us.
Best Regards
-
AuthorPosts
The topic ‘Lable percentage for variable product’ is closed to new replies.
- You must be logged in to create new topics. Login / Register