I have being creating a new woocommerce plugin that will provide an option to the apply discount on top of the cart page.
Now when there are products in the cart, the user will click on the Apply Discount button and when clicked and action is triggered from my custom plugin that will add cart discount to that particular Cart Order.
So far it works fine and also shows that cart discount applied. below is the screenshot
As you can see from the screenshot there the Order Total is showing Wrong figure calculated. Below is the code from my custom woocommerce plugin file.
Following action is called when the button is submitted
if(!empty($_POST['apply_discount_woo'])){
add_action('woocommerce_calculate_totals',array(&$this,'cart_order_total_action'));
}
and the function code:
public function cart_order_total_action(){
if ( is_user_logged_in() ){
global $woocommerce;
global $current_user;
global $wpdb;
$u_id = $current_user->ID;
$table_name = $wpdb->prefix."woocommerce_customer_reward_ms";
$thetable2 = $wpdb->prefix . "woocommerce_customer_reward_cart_ms";
$table_name3 = $wpdb->prefix."woocommerce_customer_reward_points_log_ms";
$data = $wpdb->get_row("SELECT * from $table_name where id=$u_id");
$data2 = $wpdb->get_row("SELECT * from $thetable2");
/* Order Id goes here */
$orders=array();//order ids
$args = array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $current_user->ID,
'post_type' => 'shop_order',
'post_status' => 'publish',
'tax_query'=>array(
array(
'taxonomy' =>'shop_order_status',
'field' => 'slug',
'terms' =>'on-hold'
)
)
);
$posts=get_posts($args);
//get the post ids as order ids
$orders=wp_list_pluck( $posts, 'ID' );
$order = $orders[0];
/* Order Id ends here */
if($data){
$user_points = $data->points;
$points_set = $data2->woo_pts_set;
print_r($woocommerce->cart->applied_coupons);
echo $woocommerce->cart->discount_cart;
echo $woocommerce->cart->get_cart_total();
/* the line below adds the cart discount to the Cart */
$woocommerce->cart->discount_cart = $data2->woo_discount_set;
}else{
echo 'You dont have any woo points yet.!!';
}
}
}
How can I update the cart total on Cart Discount added?