Home » Wordpress » Cart Discount when applied to the cart doesn't show the correct amount in Order Total using Woocommerce in wordpress

Cart Discount when applied to the cart doesn't show the correct amount in Order Total using Woocommerce in wordpress

Posted by: admin November 30, 2017 Leave a comment

Questions:

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

enter image description here

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?

Answers:

Maybe it’s not 100% how you would like to achieve your goal, but you may generate a coupon on the fly:

$coupon_code = 'UNIQUECODE'; // Code
$amount = '10'; // Amount
$discount_type = 'fixed_cart'; // Type: fixed_cart, percent, fixed_product, percent_product

$coupon = array(
    'post_title'   => $coupon_code,
    'post_content' => '',
    'post_status'  => 'publish',
    'post_author'  => 1,
    'post_type'    => 'shop_coupon'
);

$new_coupon_id = wp_insert_post( $coupon );

// Add meta
update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
update_post_meta( $new_coupon_id, 'individual_use', 'no' );
update_post_meta( $new_coupon_id, 'product_ids', '' );
update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
update_post_meta( $new_coupon_id, 'usage_limit', '' );
update_post_meta( $new_coupon_id, 'expiry_date', '' );
update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
update_post_meta( $new_coupon_id, 'free_shipping', 'no' );

Questions:
Answers:

After hours of research and debugging some Woocommerce Documentation. I found out that there are two types of discount that can be applied to the cart

1- Cart Discount
2- Order Discount

And correct me if I’m wrong, but discount is always applied on use of coupons. What I was trying here is to give a custom Discount of say $5 to the cart and this discount was not treated as an actual discount by Woocommere. Also this custom discount will not reflect the actual cart total on the cart page as well as the checkout page.
In order to do that you will ultimately have to alter your woocommerce class files, which I dont recommend when you are creating an independent custom Woocommerce Plugin.

But I figured out an alternate method of applying discount i.e with help of coupons. I created a custom coupon that will give a $5 discount to the cart. And that was it, Cart total recognised the dicount and showed me the correct total also the discount applied.

Also the checkout page was populated with the correct total.

And after processing the order it showed me the correct discount applied.

Just wanted to share this with you all. As I felt this was the solution that worked like charm and without altering any of the woocommerce core files.

I hope this is useful to you all as well.

Thanks.