/**
  * Increase applied coupon counts
  */
 public function increase_coupon_usage_counts()
 {
     if ('yes' == get_post_meta($this->id, '_recorded_coupon_usage_counts', true)) {
         return;
     }
     if (sizeof($this->get_used_coupons()) > 0) {
         foreach ($this->get_used_coupons() as $code) {
             if (!$code) {
                 continue;
             }
             $coupon = new WC_Coupon($code);
             $used_by = $this->get_user_id();
             if (!$used_by) {
                 $used_by = $this->billing_email;
             }
             $coupon->inc_usage_count($used_by);
         }
         update_post_meta($this->id, '_recorded_coupon_usage_counts', 'yes');
     }
 }
 /**
  * function to modify order_items of renewal order
  *
  * @param array $order_items
  * @param int $original_order_id
  * @param int $renewal_order_id
  * @param int $product_id
  * @param string $new_order_role
  * @return array $order_items
  */
 public function sc_subscriptions_renewal_order_items($order_items = null, $original_order_id = 0, $renewal_order_id = 0, $product_id = 0, $new_order_role = null)
 {
     $pay_from_credit_of_original_order = get_option('pay_from_smart_coupon_of_original_order', 'yes');
     if ($pay_from_credit_of_original_order != 'yes') {
         return;
     }
     if ($new_order_role != 'child') {
         return;
     }
     if (empty($renewal_order_id) || empty($original_order_id)) {
         return;
     }
     $original_order = $this->get_order($original_order_id);
     $renewal_order = $this->get_order($renewal_order_id);
     $coupon_used_in_original_order = $original_order->get_used_coupons();
     $coupon_used_in_renewal_order = $renewal_order->get_used_coupons();
     if (sizeof($coupon_used_in_original_order) > 0) {
         $smart_coupons_contribution = array();
         foreach ($coupon_used_in_original_order as $coupon_code) {
             $coupon = new WC_Coupon($coupon_code);
             if (!empty($coupon->discount_type) && $coupon->discount_type == 'smart_coupon' && !empty($coupon->amount) && !in_array($coupon_code, $coupon_used_in_renewal_order, true)) {
                 $renewal_order_total = $renewal_order->get_total();
                 $discount = min($renewal_order_total, $coupon->amount);
                 if ($discount > 0) {
                     $new_order_total = $renewal_order_total - $discount;
                     update_post_meta($renewal_order_id, '_order_total', $new_order_total);
                     update_post_meta($renewal_order_id, '_order_discount', $discount);
                     if ($new_order_total <= floatval(0)) {
                         update_post_meta($renewal_order_id, '_renewal_paid_by_smart_coupon', 'yes');
                     }
                     $renewal_order->add_coupon($coupon_code, $discount);
                     $smart_coupons_contribution[$coupon_code] = $discount;
                     $used_by = $renewal_order->get_user_id();
                     if (!$used_by) {
                         $used_by = $renewal_order->billing_email;
                     }
                     $coupon->inc_usage_count($used_by);
                 }
             }
         }
         if (!empty($smart_coupons_contribution)) {
             update_post_meta($renewal_order_id, 'smart_coupons_contribution', $smart_coupons_contribution);
         }
     }
     return $order_items;
 }
/**
 * Update used coupon amount for each coupon within an order.
 *
 * @since 2.7.0
 * @param int $order_id
 */
function wc_update_coupon_usage_counts($order_id)
{
    if (!($order = wc_get_order($order_id))) {
        return;
    }
    $has_recorded = $order->get_data_store()->get_recorded_coupon_usage_counts($order);
    if ($order->has_status('cancelled') && $has_recorded) {
        $action = 'reduce';
        $order->get_data_store()->set_recorded_coupon_usage_counts($order, false);
    } elseif (!$order->has_status('cancelled') && !$has_recorded) {
        $action = 'increase';
        $order->get_data_store()->set_recorded_coupon_usage_counts($order, true);
    } else {
        return;
    }
    if (sizeof($order->get_used_coupons()) > 0) {
        foreach ($order->get_used_coupons() as $code) {
            if (!$code) {
                continue;
            }
            $coupon = new WC_Coupon($code);
            if (!($used_by = $order->get_user_id())) {
                $used_by = $order->get_billing_email();
            }
            switch ($action) {
                case 'reduce':
                    $coupon->dcr_usage_count($used_by);
                    break;
                case 'increase':
                    $coupon->inc_usage_count($used_by);
                    break;
            }
        }
    }
}
 /**
  * Increase applied coupon counts
  *
  * @access public
  * @return void
  */
 public function increase_coupon_usage_counts()
 {
     global $woocommerce;
     if (get_post_meta($this->id, '_recorded_coupon_usage_counts', true) == 'yes') {
         return;
     }
     if (sizeof($this->get_used_coupons()) > 0) {
         foreach ($this->get_used_coupons() as $code) {
             if (!$code) {
                 continue;
             }
             $coupon = new WC_Coupon($code);
             $coupon->inc_usage_count();
         }
     }
     update_post_meta($this->id, '_recorded_coupon_usage_counts', 'yes');
 }
/**
 * Increase coupon usage count
 */
function woocommerce_increase_coupon_counts()
{
    global $woocommerce;
    if ($applied_coupons = $woocommerce->cart->get_applied_coupons()) {
        foreach ($applied_coupons as $code) {
            $coupon = new WC_Coupon($code);
            $coupon->inc_usage_count();
        }
    }
}
/**
 * Update used coupon amount for each coupon within an order.
 *
 * @since 2.7.0
 * @param int $order_id
 */
function wc_update_coupon_usage_counts($order_id)
{
    $order = wc_get_order($order_id);
    $has_recorded = get_post_meta($order_id, '_recorded_coupon_usage_counts', true);
    if (!$order) {
        return;
    }
    if ($order->has_status('cancelled') && 'yes' === $has_recorded) {
        $action = 'reduce';
        delete_post_meta($order_id, '_recorded_coupon_usage_counts');
    } elseif (!$order->has_status('cancelled') && 'yes' !== $has_recorded) {
        $action = 'increase';
        update_post_meta($order_id, '_recorded_coupon_usage_counts', 'yes');
    } else {
        return;
    }
    if (sizeof($order->get_used_coupons()) > 0) {
        foreach ($order->get_used_coupons() as $code) {
            if (!$code) {
                continue;
            }
            $coupon = new WC_Coupon($code);
            if (!($used_by = $order->get_user_id())) {
                $used_by = $order->get_billing_email();
            }
            switch ($action) {
                case 'reduce':
                    $coupon->dcr_usage_count($used_by);
                    break;
                case 'increase':
                    $coupon->inc_usage_count($used_by);
                    break;
            }
        }
    }
}