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

331
Visualizações
Coupon discount on product regular price (not sale price)

In WooCommerce, I have products with a regular price of X and a sales price of Y. I would like to add a coupon with a code for a $45.00 discount to be taken from the regular price X.

I would like the coupon to disregard the sale price so I get X-$45 NOT Y-$45. But when the coupon is not applied price Y is used.

I found the following which works for percentage discounts, but I can't seem to make it work for a fixed product discount price.

add_filter('woocommerce_coupon_get_discount_amount', 'woocommerce_coupon_get_discount_amount', 10, 5 );
function woocommerce_coupon_get_discount_amount( $discount, $discounting_amount, $cart_item, $single, $coupon ) {
    if ($coupon->type == 'percent_product' || $coupon->type == 'percent') {
        global $woocommerce;
        $cart_total = 0;

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

         $variable_product1= new WC_Product_Variation( $cart_item["variation_id"] );

         $cart_total += $variable_product1 ->regular_price * $cart_item['quantity'];
        } 
        $discount = round( ( $cart_total / 100 ) * $coupon->amount, $woocommerce->cart->dp );
        return $discount;
    }
    return $discount;
}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

This seems to check for a coupon and then change the cart price to regular price rather than discounted price (obtained from woocommerce dynamic pricing in this application) prior to applying the coupon. placed in functions.php

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 10, 1);
function add_custom_price( $cart_object) {

    global $woocommerce;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $coupon = False;

    if ($coupons = WC()->cart->get_applied_coupons()  == False ) 
      $coupon = False;
    else {
        foreach ( WC()->cart->get_applied_coupons() as $code ) {
          $coupons1 = new WC_Coupon( $code );
          if ($coupons1->type == 'percent_product' || $coupons1->type == 'percent')
            $coupon = True;
        }
    }

    if ($coupon == True)
        foreach ( $cart_object->get_cart() as $cart_item ) 
        {
            $price = $cart_item['data']->regular_price;
            $cart_item['data']->set_price( $price );
        }
}
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