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;