Cart The discount when applied to the cart does not display the correct amount in the order Total using Woocommerce in wordpress

I am creating a new woocommerce plugin that will give you a discount on discount on top of the cart page.

Now, when there are goods in the basket, the user clicks the "Apply discount" button, and when pressed, the action is launched from my custom plug-in, which will add a discount to the basket for this particular basket order.

So far this has worked perfectly, and also shows that the discount on the basket is applied. below is a screenshot

enter image description here

As you can see in the screenshot, the wrong number is displayed on the Order Total screen. Below is the code from my custom woocommerce plugin file.

The following action is called when the button is sent.

if(!empty($_POST['apply_discount_woo'])){
                    add_action('woocommerce_calculate_totals',array(&$this,'cart_order_total_action'));
                }

and 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.!!';
                    }
                }
            }

?

+3
2

Woocommerce. , , .

1- 2-

, , . , , - $5 , Woocommere. , , . woocommerce, Woocommerce.

i.e . , 5 . , Cart Total .

.

.

. , , - woocommerce.

, .

.

0

, 100%, , :

$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' );
+1

All Articles