Do you want to offer free gifts for high-value customers? Yes, here is this customization that will offer BOGO gifts only to specific customers based on their purchase history or total amount spent in your shop. This code automatically adds a free product to the WooCommerce cart for a logged-in user if their previous history of total amount spent exceeds $10000.
add_action('woocommerce_before_calculate_totals', 'ts_add_free_product_to_cart'); function ts_add_free_product_to_cart($cart) { // Check if the user is logged in $user_id = get_current_user_id(); if ($user_id > 0) { // Get the total amount spent by the customer $total_amount_spent = wc_get_customer_total_spent($user_id); // Check if the total amount spent is greater than $10,000 if ($total_amount_spent > 10000) { // Define the free product ID $free_product_id = 470; // Replace with the actual ID of your free product // Check if the free product is not already in the cart $free_product_in_cart = false; foreach ($cart->get_cart() as $cart_item) { if ($cart_item['product_id'] == $free_product_id) { $free_product_in_cart = true; break; } } // If the free product is not in the cart, add it if (!$free_product_in_cart) { $cart->add_to_cart($free_product_id); } } } }
Output
When a customer logs in, the code refers to the previous history of the total amount spent by the customer. If the total money spent by the customer goes above a certain limit and when they add something to the cart, a free product is added automatically.
Alternatively, you can add free products based on user role of the customers. Check here on how to apply bogo buy one get one for registered user in WooCommerce.