0

hi guys i wanna show how much customers save with current deal. I entered regular and sale price for all products on my store and it show the percentage of discount on prdocut catalog.Woocommerce cart shows only the total and subtotal.

Subtotal:10$
Total:10$

so i wrote this function to show what i want. Although it works on my localhost, I can't get any results on live hosting.

 function discount_module_in() {
    global $woocommerce;

    $items = $woocommerce->cart->get_cart();

        foreach($items as $item => $values) { 
            $price=get_post_meta($values['product_id'] , '_regular_price', true);
            $sale=get_post_meta($values['product_id'] , '_sale_price', true);
            $amount=$price-$sale;
            $discount=(1-$sale/$price)*100;
            if (intval($discount)==70) {
                $amount70+=$amount;
            } 
            if (intval($discount)==50) {
                $amount50+=$amount;
            }
            if (intval($discount)==30) {
                $amount30+=$amount;

            }

        }
        if ($amount70>0) {
            echo "70% OFF: -".$amount70.get_woocommerce_currency_symbol()."<br>";
        }
        if ($amount50>0) {
            echo "50% OFF: -".$amount50.get_woocommerce_currency_symbol()."<br>";
        }
        if ($amount30>0) {
            echo "30% OFF: -".$amount30.get_woocommerce_currency_symbol()."<br>";
        }


}   
add_action( 'woocommerce_after_cart_totals', 'discount_module_in');

I also check if its theme issue or not but i just paste the code inside of fuction.php, to be ensure i try simple function and it worked. It's not a woocommerce hook issue either.

  function discount_module_in() {

        echo "test"
  }
  add_action( 'woocommerce_after_cart_totals', 'discount_module_in');

i also change my code with this. it eorken on local host but still not working on live host.

function deneme(){

    global $woocommerce;


    $cart = WC()->cart->get_cart();

    foreach( $cart as $cart_item ){

    $product = wc_get_product( $cart_item['product_id'] );


    $price=$product->get_regular_price();
    $sale=$product->get_sale_price();

    $amount=$price-$sale;
    $discount=(1-$sale/$price)*100;
        if (intval($discount)==70) {
            $amount70+=$amount;
        } 
        if (intval($discount)==50) {
                    $amount50+=$amount;
        }
        if (intval($discount)==30) {
        $amount30+=$amount;
        }

    }
        if ($amount70>0) {
            echo "70% OFF: -".$amount70.get_woocommerce_currency_symbol()."<br>";
        }
        if ($amount50>0) {
            echo "50% OFF: -".$amount50.get_woocommerce_currency_symbol()."<br>";
        }
        if ($amount30>0) {
            echo "30% OFF: -".$amount30.get_woocommerce_currency_symbol()."<br>";
        }    
}
add_action( 'woocommerce_proceed_to_checkout', 'deneme');

after debuggin it gives me this error

Warning: A non-numeric value encountered in /home/u693223509/domains/inuovo.com/public_html/wp-content/themes/inuovo/functions.php on line 34

Warning: A non-numeric value encountered in /home/u693223509/domains/inuovo.com/public_html/wp-content/themes/inuovo/functions.php on line 34

Warning: A non-numeric value encountered in /home/u693223509/domains/inuovo.com/public_html/wp-content/themes/inuovo/functions.php on line 35

Warning: A non-numeric value encountered in /home/u693223509/domains/inuovo.com/public_html/wp-content/themes/inuovo/functions.php on line 35

Warning: Division by zero in /home/u693223509/domains/inuovo.com/public_html/wp-content/themes/inuovo/functions.php on line 35

here are line 34 35

    $amount=$price-$sale;
    $discount=(1-$sale/$price)*100;
0

i found the solution im sorry its my bad my shop based on variable produts, after i call variation price it worked!

  • Hi, welcome to WordPress Stack Exchange. We generally prefer answers to be fairly definitive. Could I suggest you show the code that worked and maybe explain how you worked it out so that others can follow along who have a similar issue? – Matthew Brown aka Lord Matt Sep 1 at 23:37
0

Here is the solution; sorry for delay :) i hope this will help who looking for same issue;

    function deneme (){

    global $woocommerce;


    $cart = WC()->cart->get_cart();

    foreach( $cart as $cart_item ){

    $product = wc_get_product( $cart_item['product_id'] );
    // Now you have access to (see above)...

    $sale     =  $product->get_variation_sale_price( 'min', true );
    $price  =  $product->get_variation_regular_price( 'max', true );
    $amount=$price-$sale;
    $total+=$price*$cart_item['quantity'];
    $discount=(1-$sale/$price)*100;
        if (intval($discount)==70) {
            $amount70+=$amount*$cart_item['quantity'];
        } 
        if (intval($discount)==50) {
            $amount50+=$amount*$cart_item['quantity'];
        }
        if (intval($discount)==30) {
        $amount30+=$amount*$cart_item['quantity'];
        }

    }
        echo "<span style='float:right'>ORDER TOTAL: ".get_woocommerce_currency_symbol().$total."</span><br>";
        if ($amount70>0) {
            echo "<span style='float:right'>70% OFF: -".get_woocommerce_currency_symbol().$amount70."</span><br>";
        }
        if ($amount50>0) {
            echo "<span style='float:right'>50% OFF: -".get_woocommerce_currency_symbol().$amount50."</span><br>";
        }
        if ($amount30>0) {
            echo "<span style='float:right'>30% OFF: -".get_woocommerce_currency_symbol().$amount30."</span><br>";
        }  
}

add_action( 'woocommerce_cart_totals_after_order_total', 'deneme');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.