Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

wordpress - Add badges woocommerce when

I need to add, in functions.php, badges for

1)product on sale add badge "DISCOUNT"
2)when new (about 30days) add badge "NEW"
3)when the discount is more than 5%, add badge "SUPER DEAL"
4)when in a specific category add badge "OUTLET"

Thanks

UPDATE - Answering my questions!

Hi add the answer! Hope will be usefull!

    //show the last 30 days new products
add_action( 'woocommerce_before_shop_loop_item_title', function() {
    $postdate      = get_the_time( 'Y-m-d' ); // Post date
    $postdatestamp = strtotime( $postdate );  // Timestamped post date
    $newness       = 30;                      // Newness in days
    if ( ( time() - ( 60 * 60 * 24 * $newness ) ) < $postdatestamp ) {
        echo '<div class="novita">' . esc_html__( 'Novità!', 'total' ) . '</div>';
    }
}, 20 );

Add badges of SUPERDEAL when sale > 15%

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));
            }
        }
        $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)) . '%';
    }
    if($percentage<=5){
    return '<span class="onsale">' . esc_html__( '-', 'woocommerce' ) . ' ' . $percentage . '</span>';}
    if($percentage>5){
    return '<span class="onsale">' . esc_html__( 'SUPER OFFERTA -', 'woocommerce' ) . ' ' . $percentage . '</span>';}
    
        
}

Add badge when in a specific category

add_action( 'woocommerce_after_shop_loop_item_title', 'display_lottie_new_badge', 40 );
function display_lottie_new_badge() {
    global $product;

    if ( has_term ( array('certificati'), 'product_cat', $product->get_id() ) ) {
       echo '<div class="marginecertificato"><span class="certificato">' . esc_html__( 'CERTIFICATO!', 'woocommerce' ) . '</span></div>';}
    
}
question from:https://stackoverflow.com/questions/65829790/add-badges-woocommerce-when

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...