I am using woocommerce on my WordPress site. There is a problem with Coupon usage count, please help me fix it. I saw already many questions in the stack-overflow for my problem, but I did not get a single answer to my need.
The image below will show you that there is a coupon on my site that has a limit that you can only use it 10000 times.
But it shows that the coupon has not been used (it displays 0 usage). You can see that too. But the image below shows me that there are 4 orders in my site that used that coupon.
Also, I am using custom code to apply the coupon for the order as follows.
$args = array('customer_id' => $user_id);
$order = wc_create_order($args);
if($order->id){
$order_id = $order->id;
WC()->cart->add_to_cart($box_type,1,$variationID);
$order->add_product($varProduct, 1, $variationsArray);
$order->set_address( $saddress, 'shipping' );
$order->set_address( $baddress, 'billing' );
$order_items = $order->get_items();
if(isset($_SESSION['wc_coupon_code']) && $_SESSION['wc_coupon_code']){
$code = $_SESSION['wc_coupon_code'];
$coupon = new WC_Coupon($code);
if($coupon->is_valid()){
$id = $order->add_coupon($code,$coupon->coupon_amount);
if($id){
WC()->cart->add_discount( sanitize_text_field($code) );
}
}
}
WC()->cart->calculate_totals();
$order->calculate_totals();
do_action('woocommerce_before_calculate_totals');
do_action('woocommerce_calculated_total',$order->get_total(),WC()->cart);
do_action('woocommerce_checkout_order_processed',$order_id,$_POST);
if($response && empty($response['errors'])){
if(strtolower($response['status']) == 'ok'){
$order->update_status("Completed",$response['statusDetail'], TRUE);
$order->payment_complete();
}
}
}
So that someone can guide me where am I doing wrong ? And what should I do for the applied coupon so that it counts as usage?