Home Forums WoodMart support forum Lable percentage for variable product

Lable percentage for variable product

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #149070

    tvojsplet
    Participant

    Hi, 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.
    #149094

    Hello,

    You can find this option in the Theme Settings > Shop > Product labels https://prnt.sc/pgg13x

    Best Regards

    #149095

    tvojsplet
    Participant

    I know. But it only show for simple product not for variable product. More specific for single variation.

    #149097

    tvojsplet
    Participant

    If 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>’;
    }

    #149122

    Hello,

    Unfortunately, the labels are not compatible with the plugin you provide.

    Best Regards

    #149192

    tvojsplet
    Participant

    It 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.
    #149234

    Hello,

    Please provide the page URL I will provide custom CSS to make it as other labels on the product grid.

    Best Regards

    #149254

    tvojsplet
    Participant

    I 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 );

    #149258

    That is fine. If you have any questions please feel free to contact us.

    Best Regards

Viewing 9 posts - 1 through 9 (of 9 total)

The topic ‘Lable percentage for variable product’ is closed to new replies.