Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create custom discount for the cart in woocommerce

I'm creating a Plugin in WooCommerce and have a small issue with adding custom discounts to the CART / CHECKOUT page.

How can I apply custom discount to the cart without creating coupons? Say I want to give some discount of 5 dollars on the cart page. How can I do that?

Below is my code from the plugin file where I have used a coupon to apply discount, but I want to add another custom discount without the use of coupon.

Action Hook in the plugin file :

add_action('woocommerce_calculate_totals',array(&$this,'cart_order_total_action'));

and its function in the plugin file is :

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);
        $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;
            $coupon_code = 'wooreward_discount';
            if($user_points>=$points_set){
                // this following Code is optional and can be removed......as there is no need of if statement here
                if ( $woocommerce->cart->has_discount( $coupon_code ) ) {
                    /*$woocommerce->add_error( __('Coupon Code Already Applied.!!','woocommerce'));*/
                    return false;
                }else{
                    $woocommerce->cart->add_discount(sanitize_text_field($coupon_code));
                    $woocommerce->add_message( __('Taxco925 Reward Discount Applied.!!','woocommerce'));
                }
            }else{
                $woocommerce->add_error( __('Not Enough Taxco925 Points.!!','woocommerce'));
            }
        }else{
            $woocommerce->add_error( __('You have have not earned any Taxco925 Points yet.!!','woocommerce'));
        }
    }
}

As you can see this line $woocommerce->cart->add_discount(sanitize_text_field($coupon_code)); adds my discount to the cart. But it uses coupon in the background to do so . Is there any way I can add a custom discount without the use of coupon.

like image 411
Leroy Mikenzi Avatar asked Apr 08 '14 05:04

Leroy Mikenzi


People also ask

How do I add a discount to my WooCommerce cart page?

Discount based on WooCommerce Cart Order Total / SubtotalIn the filter section, Choose “All Products” as it is a store-wide discount. In the discount section, Select the Discount Type as “Percentage Discount” and enter the discount value(20%).

How do I display discounts on WooCommerce product and cart page?

To do this, simply go to the product page and scroll down to the “Pricing” section. Here, you'll see an option to “Enable sale price.” Check this box and enter the sale price in the ” Sale price” field. Be sure to click “Save changes” when you're done. You can also style the text of your discounts using HTML tags.

How do I add a discount percentage in WooCommerce?

Navigate to WooCommerce -> Woo Discount Rules -> Add New Rule. In the filter section, choose “All Products” as it is a storewide discount. In the discount section, Choose the Discount Type as “Percentage discount” and enter the discount value as 20%.


1 Answers

add_action('woocommerce_checkout_order_processed','custom_disount',10,1);
function custom_disount($order_id){
    $order = wc_get_order($order_id);
    $order_items = $order->get_items();
    foreach ($order_items as $order_item_key => $order_item) {
        $product = new WC_Product((int) $order_item['product_id']);
        $quantity = (int) $order_item['qty'];
        $discount=($product->regular_price*30)/100;  //30% disount.
        wc_update_order_item_meta($order_item_key,'_line_total',($product->regular_price*$quantity)-($discount*$quantity));
    }
}
like image 126
Ashish Khatri Avatar answered Oct 06 '22 00:10

Ashish Khatri