Change the Label text for specific Product badges

This topic has 4 replies, 2 voices, and was last updated 1 months, 1 weeks ago ago by Luca Rossi

  • Avatar: dybwpath
    dybwpath
    Participant
    October 5, 2024 at 20:45

    I managed to change the Label text for the Product Badge in the Customize > Woocommerce > Shop Elements > Product Badges > Label text

    But I want for a specific product to have a specific text.

    I tried to achieve this with code, but I did not managed to target the label text.

    I used this code, but it didn’t change anything:

    // Change the sales badge text based on the product
    add_filter( ‘woocommerce_sale_flash’, ‘custom_sale_badge_text’, 10, 3 );

    function custom_sale_badge_text( $html, $post, $product ) {
    // Check the product ID
    if ( $product->get_id() === 123 ) { // Replace 123 with the desired product ID
    $html = ‘Special Discount!‘; // Change the badge text
    } elseif ( $product->get_id() === 456 ) { // Another product
    $html = ‘Limited Time Offer!‘; // Different text for another product
    }
    // Add more conditions as needed
    return $html;
    }

    = = =

    I want to signal a problem.

    The Label text it is changed in archive page, but it is not changed on Product page.

    On Product page is the default “Sale” text.

    Please, contact administrator
    for this information.
    3 Answers
    Avatar: Justin
    Luca Rossi
    Support staff
    October 7, 2024 at 07:14

    Hi @dybwpath,

    Please try adding this custom code under functions.php file locates in your child theme:

    
    add_filter( 'et_option_product_sale_label_text_et-desktop', '_custom_sale_badge_text' );
    function _custom_sale_badge_text($html){
        global $product;
    	// Check the product ID
    	if ( $product->get_id() == 123 ) { // Replace 123 with the desired product ID
    		$html = 'Special Discount!'; // Change the badge text
    	} elseif ( $product->get_id() == 456 ) { // Another product
    		$html = 'Limited Time Offer!'; // Different text for another product
    	}
    	// Add more conditions as needed
    	return $html;
    }
    

    Don’t forget to replace the 123, 456.

    Hope it helps!

    Avatar: dybwpath
    dybwpath
    Participant
    October 10, 2024 at 16:55

    Thank you.

    The code changes the Label text on the product page.

    But it does not change the shop page or in other places in site.

    I need it changed in shop or other places as well.

    Avatar: Justin
    Luca Rossi
    Support staff
    October 11, 2024 at 08:55

    Dear @dybwpath,

    We hope this message finds you well.

    Please add the following code to the functions.php file located in your child theme:

    
    function etheme_woocommerce_sale_flash( $span, $post, $product ) {
        $element_options                   = array();
        $element_options['single_product'] = false;
        $element_options['single_product'] = apply_filters( 'etheme_sale_label_single', $element_options['single_product'] );
        $element_options['in_percentage']  = etheme_get_option( 'sale_percentage', 0 );
        $element_options['in_percentage']  = apply_filters( 'etheme_sale_label_percentage', $element_options['in_percentage'] );
        
        $element_options['is_customize_preview'] = get_query_var('et_is_customize_preview', false);
        
        $element_options['sale_icon']       = etheme_get_option( 'sale_icon', 1 );
        $element_options['sale_label_text'] = etheme_get_option( 'sale_icon_text', 'Sale' );
        $element_options['show_label']      = $element_options['sale_icon'] || $element_options['is_customize_preview'];
        
        $element_options['wrapper_class'] = '';
        
        if ( $element_options['single_product'] ) {
            $element_options['sale_label_type']     = apply_filters( 'etheme_sale_label_type', etheme_get_option( 'product_sale_label_type_et-desktop', 'square' ));
            $element_options['show_label']          = $element_options['sale_label_type'] != 'none' || $element_options['is_customize_preview'];
            $element_options['sale_label_position'] = apply_filters( 'etheme_sale_label_position', etheme_get_option( 'product_sale_label_position_et-desktop', 'right' ));
            $element_options['sale_label_text']     = etheme_get_option( 'product_sale_label_text_et-desktop', 'Sale' );
            $element_options['sale_label_text']     = ( $element_options['in_percentage'] ) ? etheme_sale_label_percentage_text( $product, $element_options['sale_label_text'], true ) : $element_options['sale_label_text'];
            
            $element_options['class'] = 'type-' . $element_options['sale_label_type'];
            $element_options['class'] .= ' ' . $element_options['sale_label_position'];
            $element_options['class'] .= ' single-sale';
        } else {
            $element_options['sale_label_type']     = $element_options['sale_icon'] ? 'square' : 'none';
            $element_options['sale_label_position'] = 'left';
            $element_options['sale_label_text']     = ( $element_options['in_percentage'] ) ? etheme_sale_label_percentage_text( $product, $element_options['sale_label_text'] ) : $element_options['sale_label_text'];
            
            $element_options['class'] = 'type-' . $element_options['sale_label_type'];
            $element_options['class'] .= ' ' . $element_options['sale_label_position'];
        }
        
        if ( $element_options['sale_label_type'] == 'none' && $element_options['is_customize_preview'] ) {
            $element_options['wrapper_class'] .= ' dt-hide mob-hide';
        }
        
        if ( strpos( $element_options['sale_label_text'], '%' ) != false ) {
            $element_options['class'] .= ' with-percentage';
        }
        
        ob_start();
        
        if ( $element_options['show_label'] ) {
    
            if($product->get_id() == 791){
                $element_options['sale_label_text'] = 'Special Discount!';
            } elseif ( $product->get_id() == 792 ){
                $element_options['sale_label_text'] = 'Limited Time Offer!';
            }
            echo '' . $element_options['sale_label_text'] . '';
        }
        
        unset( $element_options );
        
        return ob_get_clean();
    }
    

    Please remember to replace the product IDs 791 and 792 with the appropriate IDs for your products.

    Should you have any further questions or need additional assistance, feel free to reach out.

    Best regards,
    8Theme’s Team

  • Viewing 4 results - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.Log in/Sign up

8theme customization service
We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.