View Categories

WooCommerce:将价格显示为“原价–现在$$ –省$$”

< 1 min read

仿牌独立站出海专家

Part 1 – PHP Snippet: Display Prices as “Was, Now, Save” for Simple Products on Sale


add_filter( 'woocommerce_get_price_html', 'bbloomer_simple_product_price_format', 10, 2 );
 
function bbloomer_simple_product_price_format( $price, $product ) {
    
   if ( $product->is_on_sale() && $product->is_type('simple') ) {
      $price = sprintf( __( '<div class="was-now-save"><div class="was">WAS %1$s</div><div class="now">NOW %2$s</div><div class="save">SAVE %3$s</div></div>', 'woocommerce' ), wc_price ( $product->get_regular_price() ), wc_price( $product->get_sale_price() ), wc_price( $product->get_regular_price() - $product->get_sale_price() )  );      
   }
    
   return $price;
}

Part 2 – CSS Snippet: Display Prices as “Was, Now, Save” for Simple Products on Sale


.was, .now, .save {
width: 50%;
padding: 0.5em 1em;
text-align: center;
}
 
.was {
background: #636363;
color: white;
}
 
.now {
background: #acacac;
color: black;
}
 
.save {
background: #eee;
color: red;
font-size: 120%;
}