get_discount_amount() public method

Get discount amount for a cart item.
public get_discount_amount ( float $discounting_amount, array | null $cart_item = null, boolean $single = false ) : float
$discounting_amount float Amount the coupon is being applied to
$cart_item array | null Cart item being discounted if applicable
$single boolean True if discounting a single qty item, false if its the line
return float Amount this coupon has discounted
 /**
  * Function to apply discounts to a product and get the discounted price (before tax is applied).
  *
  * @access public
  * @param mixed $values
  * @param mixed $price
  * @param bool $add_totals (default: false)
  * @return float price
  */
 public function get_discounted_price($values, $price, $add_totals = false)
 {
     if (!$price) {
         return $price;
     }
     if (!empty($this->applied_coupons)) {
         foreach ($this->applied_coupons as $code) {
             $coupon = new WC_Coupon($code);
             if ($coupon->apply_before_tax() && $coupon->is_valid()) {
                 if ($coupon->is_valid_for_product($values['data']) || $coupon->is_valid_for_cart()) {
                     $discount_amount = $coupon->get_discount_amount($price, $values, $single = true);
                     $price = max($price - $discount_amount, 0);
                     if ($add_totals) {
                         $this->discount_cart += $discount_amount * $values['quantity'];
                         $this->increase_coupon_discount_amount($code, $discount_amount * $values['quantity']);
                         $this->increase_coupon_applied_count($code, $values['quantity']);
                     }
                 }
             }
         }
     }
     return apply_filters('woocommerce_get_discounted_price', $price, $values, $this);
 }
function load_cart_contents()
{
    global $woocommerce;
    $cart = $woocommerce->cart->cart_contents;
    if (sizeof($cart > 0)) {
        foreach ($cart as $cart_item_key => $cart_item) {
            $woocommerce->cart->calculate_totals();
            $product_id = $cart_item['product_id'];
            $product = $cart_item['data'];
            $coupons = $woocommerce->cart->applied_coupons;
            $price = $product->get_price();
            $quantity = $cart_item['quantity'];
            $subtotal = $price * $quantity;
            if ($coupons) {
                foreach ($coupons as $key => $coupon) {
                    $coupon = new WC_Coupon($coupon);
                    if ($coupon->is_valid_for_product($product)) {
                        $discount = $coupon->get_discount_amount($price, $cart_item);
                        $subtotal = $price * $quantity - $discount;
                    }
                }
            }
            ?>
<tr class="selectable ins_order_item" id="<?php 
            echo esc_attr($cart_item_key);
            ?>
">
  <td class="ins_product_id"><?php 
            echo esc_html($product_id);
            ?>
</td>
  <td class="ins_product_title"><?php 
            echo $product->get_title();
            ?>
</td>
  <td class="ins_product_price"><?php 
            echo wc_price($price);
            ?>
</td>
  <td class="ins_product_quantity"><?php 
            echo $quantity;
            ?>
</td>
  <td class="ins_product_subtotal"><?php 
            echo wc_price($subtotal);
            ?>
</td>
  <?php 
            $coupons = $woocommerce->cart->applied_coupons;
            if ($coupons) {
                foreach ($coupons as $coupon) {
                    $coupon = new WC_Coupon($coupon);
                    if ($coupon->is_valid_for_product($cart_item['data'])) {
                        ?>
<tr class="ins_item_discounts">
  <td style="text-align:center;"><?php 
                        echo __('Discount', 'instore');
                        ?>
</td>
  <td class="disc_code" style="text-align:center;"><?php 
                        echo esc_html($coupon->code);
                        ?>
</td>
  <td class="regular_price" style="text-align:center;"> Reg price <?php 
                        echo wc_price($product->get_price());
                        ?>
</td>
  <td class="disc_amount" style="text-align:right;">- <?php 
                        echo wc_price($discount);
                        ?>
</td>
</tr>
<?php 
                    }
                }
            }
            ?>
</tr>
<?php 
        }
    }
}
 public function ins_ajax_get_discount()
 {
     global $woocommerce, $current_user;
     $total_discount = 0.0;
     if (isset($_POST['discount'])) {
         if (is_array($_POST['discount'])) {
             $discount = $_POST['discount'];
             $disc_code = $discount['disc_reason'] . time();
             $coupon_code = add_filter('woocommerce_get_shop_coupon_data', array($this, 'ins_get_discount_code'), $disc_code);
         } else {
             $coupon_code = $_POST['discount'];
         }
         $coupon = new WC_Coupon($coupon_code);
         $cart_items = $woocommerce->cart->cart_contents;
         if ($coupon->is_valid()) {
             $total_discount += $coupon->get_discount_amount($woocommerce->cart->total);
             $post = get_post($coupon->id);
             $coupon->amount = $coupon->type == 'percent' ? $coupon->amount . '%' : wc_price($coupon->amount);
             $coupon->minimum_amount = isset($_POST['call']) && $_POST['call'] == 'edit' ? $coupon->minimum_amount : wc_price($coupon->minimum_amount);
             $coupon->expiry_date = is_long($coupon->expiry_date) ? date('m/d/Y', $coupon->expiry_date) : $coupon->expiry_date;
             $coupon->usage_count = empty($coupon->usage_count) ? 0 : $coupon->usage_count;
             $this->json_return = array('success' => true, 'call' => 'update_item_display', 'coupon' => $coupon, 'discount' => $total_discount, 'description' => isset($post) ? $post->post_excerpt : $discount['disc_reason'] . ' discount created on ' . date('m/d/Y h:m:i a') . ' by ' . $current_user->user_login);
             if (isset($_POST['call'])) {
                 $this->json_return['action'] = $_POST['call'];
             }
         } else {
             $this->json_return = array('success' => false, 'coupon_error' => $coupon->error_message);
         }
     }
     echo json_encode($this->json_return);
     die;
 }
 /**
  * Does the coupon have a value? (autocoupon should not be applied if it has no value)
  * @param  WC_Coupon $coupon The coupon data
  * @return bool True if it has a value (discount, free shipping, whatever) otherwise false)
  **/
 function coupon_has_a_value($coupon)
 {
     $has_a_value = false;
     if ($coupon->enable_free_shipping()) {
         $has_a_value = true;
     } else {
         //Test whether discount > 0
         //See WooCommerce: class-wc-cart.php function get_discounted_price
         global $woocommerce;
         foreach ($woocommerce->cart->get_cart() as $cart_item) {
             if ($coupon->is_valid_for_cart() || $coupon->is_valid_for_product($cart_item['data'], $cart_item)) {
                 if ($coupon->get_discount_amount($cart_item['data']->price, $cart_item) > 0) {
                     $has_a_value = true;
                     break;
                 }
             }
         }
     }
     return apply_filters('wjecf_coupon_has_a_value', $has_a_value, $coupon);
 }
 /**
  *
  * Gets custom price on the order page
  *
  * @param WC_Product $product
  * @param int $item_id
  * @param str $start - Start date
  * @param str $end - End date
  * @param array $order_item
  * @param array $coupons
  * @return array $item_prices - Item prices (subtotal, total, tax subtotal and tax total)
  *
  **/
 public function easy_booking_get_booking_price($product, $item_id, $start, $end, $order_item, $coupons)
 {
     if (!$product) {
         return false;
     }
     $calc_mode = $this->options['easy_booking_calc_mode'];
     // Calculation mode (Days or Nights)
     // Get booking duration
     $start_diff = strtotime($start);
     $end_diff = strtotime($end);
     $diff = absint($start_diff - $end_diff) * 1000;
     $days = $diff / 86400000;
     if ($days === 0) {
         $days = 1;
     }
     // If calculation mode is set to "Days", add one day
     if ($calc_mode === 'days' && $start != $end) {
         $duration = absint($days + 1);
     } elseif ($calc_mode === 'days' && $start === $end) {
         $duration = absint($days);
     } else {
         $duration = absint($days);
     }
     if ($product->is_taxable()) {
         $price = $product->get_price_excluding_tax();
         // Product price excluding tax
     } else {
         $price = $product->get_price();
         // Product price
     }
     // Price for x days
     $new_price = apply_filters('easy_booking_get_order_item_price', $price * $duration, $product, $item_id, $duration);
     if ($product->is_taxable()) {
         $item_tax_class = $order_item['tax_class'];
         $product_taxes = $this->easy_booking_get_product_taxes($new_price, $item_tax_class);
         // Product taxes without potential discounts
         foreach ($product_taxes as $_tax_id => $_tax_value) {
             $tax_subtotal[$_tax_id] = $_tax_value;
         }
         $tax_amount = WC_Tax::get_tax_total($product_taxes);
     }
     if ($coupons) {
         foreach ($coupons as $code) {
             $coupon = new WC_Coupon($code);
             if ($coupon->is_valid_for_product($product)) {
                 $coupon_amount = $coupon->get_discount_amount($new_price, $order_item, true);
                 // Discounted amount for item price
                 $total = $new_price - $coupon_amount;
                 // New price with discount
                 if (!empty($product_taxes)) {
                     foreach ($product_taxes as $_tax_id => $_tax_value) {
                         $tax_discount[$_tax_id] = $coupon->get_discount_amount($_tax_value, $order_item, true);
                         // Discounted amount for item taxes
                         $tax_total[$_tax_id] = $_tax_value - $tax_discount[$_tax_id];
                         //  Product taxes with discount
                     }
                 }
             } else {
                 if (!empty($product_taxes)) {
                     foreach ($product_taxes as $_tax_id => $_tax_value) {
                         $tax_total[$_tax_id] = $_tax_value;
                         // No valid coupon - Product taxes unchanged
                     }
                 }
                 $total = $new_price;
                 // No valid coupon - Product  price unchanged
             }
         }
     } else {
         if (!empty($product_taxes)) {
             foreach ($product_taxes as $_tax_id => $_tax_value) {
                 $tax_total[$_tax_id] = $_tax_value;
                 // No coupon - Product taxes unchanged
             }
         }
         $total = $new_price;
         // No coupon - Product  price unchanged
     }
     $new_price = $new_price * $order_item['quantity'];
     // Multiply subtotal by item quantity
     $total = $total * $order_item['quantity'];
     // Multiply total by item quantity
     if (!empty($product_taxes)) {
         foreach ($tax_subtotal as $tax_subtotal_id => $tax_subtotal_amount) {
             $tax_subtotal[$tax_subtotal_id] = $tax_subtotal_amount * $order_item['quantity'];
         }
         // Multiply tax subtotal by item quantity
         foreach ($tax_total as $tax_total_id => $tax_total_amount) {
             $tax_total[$tax_total_id] = $tax_total_amount * $order_item['quantity'];
         }
         // Multiply tax total by item quantity
         // Format taxes
         $line_taxes = array_map('wc_format_decimal', $tax_total);
         $line_subtotal_taxes = array_map('wc_format_decimal', $tax_subtotal);
         $item_prices['tax_subtotal'] = $line_subtotal_taxes;
         $item_prices['tax_total'] = $line_taxes;
     }
     $item_prices['subtotal'] = floatval(wc_format_decimal($new_price, 0));
     $item_prices['total'] = floatval(wc_format_decimal($total, 0));
     return $item_prices;
 }