Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

269
Visualizações
WooCommerce how to display "you save X%" on checkout page

I have made a function that displays on checkout the total savings based on product discounts but I would like it to show the percentage saved above the order total, and if possible show it inside of a box.

Code:

function wc_discount_total() {
   global $woocommerce;
    $discount_total = 0;
      
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values) {
          
    $_product = $values['data'];
  
        if ( $_product->is_on_sale() ) {
        $regular_price = $_product->get_regular_price();
        $sale_price = $_product->get_sale_price();
        $discount = ($regular_price - $sale_price) * $values['quantity'];
        $discount_total += $discount;
        }
    }        
    if ( $discount_total > 0 ) {
    echo '<tr class="cart-discount">
    <th>'. __( 'Your Savings', 'woocommerce' ) .'</th>
    <td data-title=" '. __( 'You Saved', 'woocommerce' ) .' ">'
    . wc_price( $discount_total + $woocommerce->cart->discount_cart ) .'</td>
    </tr>';
    }
}
add_action( 'woocommerce_cart_totals_after_order_total', 'wc_discount_total', 99);
add_action( 'woocommerce_review_order_after_order_total', 'wc_discount_total', 99);

How my current checkout looks like:

enter image description here

Zoomed:

enter image description here

How I would like it to look:

enter image description here

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

If there is a discount on that order, then you could add another table row tag and calculate the percentage. So it'd be something like this:

add_action('woocommerce_cart_totals_after_order_total', 'wc_discount_total', 99);

add_action('woocommerce_review_order_after_order_total', 'wc_discount_total', 99);

function wc_discount_total()
{
  global $woocommerce;
  $discount_total = 0;

  foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) {

    $_product = $values['data'];

    if ($_product->is_on_sale()) {
      $regular_price = $_product->get_regular_price();
      $sale_price = $_product->get_sale_price();
      $discount = ($regular_price - $sale_price) * $values['quantity'];
      $discount_total += $discount;
    }
  }
  if ($discount_total > 0) {
    echo '<tr class="cart-discount">
      <th>' . __('Your Savings', 'woocommerce') . '</th>
      <td data-title=" ' . __('You Saved', 'woocommerce') . ' ">'
      . wc_price($discount_total + $woocommerce->cart->discount_cart) . '</td>
      </tr>';

    $total = WC()->cart->cart_contents_total;
    $total_saved = wc_price($discount_total + $woocommerce->cart->discount_cart);
    $percentage_saved = round(($total_saved * 100) / $total);

    echo '<tr class="cart-percentage-discount">
      <th>' . __('Percentage Saved', 'woocommerce') . '</th>
      <td data-title=" ' . __('You Saved', 'woocommerce') . ' ">' . esc_html($percentage_saved . "%") . '</td>
      </tr>';
  }
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda