
Offering a welcome discount to new users is a great way to convert first-time visitors into loyal customers.
But instead of sending coupon codes or setting up complex rules, let’s make it fully automatic — no plugin needed!
/**
* Auto apply first-order discount for new customers
* and show message on checkout page
*/
add_action('woocommerce_cart_calculate_fees', function() {
if ( is_admin() && ! defined('DOING_AJAX') ) return;
$discount_amount = 100; // 💰 Change this to your desired discount amount
$user_id = get_current_user_id();
if ( $user_id ) {
// Check if user has any previous orders
$orders = wc_get_orders([
'customer_id' => $user_id,
'limit' => 1,
]);
// If no previous orders, apply discount
if ( empty($orders) ) {
WC()->cart->add_fee( __('First Order Discount', 'textdomain'), -$discount_amount );
// Store a flag to display message later
WC()->session->set( 'first_order_discount_applied', true );
} else {
WC()->session->__unset( 'first_order_discount_applied' );
}
}
});
/**
* Display message on checkout page if first order discount applied
*/
add_action('woocommerce_before_checkout_form', function() {
if ( WC()->session->get( 'first_order_discount_applied' ) ) {
echo '🎉 Congrats! You’re getting a special discount on your first order.';
}
});
/**
* Auto apply first-order percentage discount for new customers
*/
add_action('woocommerce_cart_calculate_fees', function() {
if ( is_admin() && ! defined('DOING_AJAX') ) return;
$discount_percent = 10; // 🧾 Change this to your desired percentage
$user_id = get_current_user_id();
if ( $user_id ) {
// Check if user has any previous orders
$orders = wc_get_orders([
'customer_id' => $user_id,
'limit' => 1,
]);
if ( empty($orders) ) {
$cart_total = WC()->cart->get_subtotal();
$discount = ($cart_total * $discount_percent) / 100;
WC()->cart->add_fee( sprintf(__('First Order %s%% Discount', 'textdomain'), $discount_percent), -$discount );
WC()->session->set( 'first_order_discount_applied', true );
} else {
WC()->session->__unset( 'first_order_discount_applied' );
}
}
});
/**
* Show message on checkout page
*/
add_action('woocommerce_before_checkout_form', function() {
if ( WC()->session->get( 'first_order_discount_applied' ) ) {
echo '🎉 Congrats! You’re getting a special discount on your first order.';
}
});
add_filter('woocommerce_shipping_free_shipping_is_available', function($is_available) {
$user_id = get_current_user_id();
$orders = wc_get_orders(['customer_id' => $user_id, 'limit' => 1]);
if (empty($orders)) return true; // Free shipping for first order
return $is_available;
});