Skip to content
Advertisement

Apply a welcome discount to non “on sale” items in WooCommerce

I use this code to set up a “welcome” discount for a registered user:

 add_action( 'woocommerce_cart_calculate_fees', 'personal_discount_based', 20, 1 );
 function personal_discount_based( $cart ) {
  if ( is_admin() && ! defined( 'DOING_AJAX' ) )
     return; 
     $user = get_user_data();
     $usId = $user->ID;
     update_user_meta( $usId, 'discount', 5 );
     $personalDisconte = $user->discount;
     $orderNumbers = wc_get_customer_order_count($usid);

     if ( ! $personalDisconte == true )
    return; 
    $percentage = $personalDisconte;
    $discount = $cart->get_subtotal() * $percentage / 100;
    if(!empty($orderNumbers)  && $orderNumbers == 1){
      update_user_meta( $usId, 'discount', 0 );
    }elseif( $orderNumbers > 1){
      $cart->add_fee( sprintf( __("Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
    }else{
       $cart->add_fee( sprintf( __("Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
     }

   }

And this code does its function. But now I am facing another problem. The discount applies to the entire order amount, of course. In this case, the order may already include products with a discount. And this option is not suitable for the seller.

It is generally possible that the discount generated by this code is not applied to the total amount of the order. But only to those products that are without discount.

For example, a user added three products to the cart:

Item one 50$ Item two 75$ Item three 90$ (old price 120$)

So, I have to apply a discount to the sum of 50 + 75 (these are regular price product) After 50 + 75 – 5% plus 90 the result will be the final sum.

At the moment, I do not know how to do this and am not sure if it is possible to do it at all. If someone can help, give advice please.

Advertisement

Answer

Your code is complicated for nothing if you are applying a welcome discount for new customers that doesn’t have purchased yet, so I have simplified and lighten your code.

Then to get the cart subtotal for non “On sale” items you need to loop through cart items to get it.

The revisited code:

add_action( 'woocommerce_cart_calculate_fees', 'welcome_discount_on_normal_items', 20, 1 );
function welcome_discount_on_normal_items( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $user_id = get_current_user_id();

    if( $user_id > 0 ) {
        $percentage   = 5; // Discount percentage
        $orders_count = (int) wc_get_customer_order_count($user_id);
        $subtotal     = 0; // Initializing

        // Loop through cart items
        foreach( $cart->get_cart() as $cart_item ) {
            // Add non on sale items to subtotal
            if( ! $cart_item['data']->is_on_sale() ) {
                $subtotal += $cart_item['line_subtotal'];
            }
        }

        // Discount percentage amount on non "on sale" items subtotal
        $discount = $subtotal * $percentage / 100;

        // For new customer only that haven't purchase yet
        if( $subtotal > 0 && $orders_count === 0 ) {
            $cart->add_fee( sprintf( __("Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
        }
    }
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement