AI Verified

Name

Reduce Cart Total based on Items

About

If you have 3 or more total quantity in your cart, then a $100 will be applied to the total.

Language

PHP

Rating

Voted: 0 by 0 user(s)

How to Setup Snippet

Add to Code Snippets.

Codevault

WebSquadron

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.4

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

History

Last modified:

13/04/2024

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

Found 0 vulnerabilities

Reduce Cart Total based on Items

 
                    
1add_action('woocommerce_before_calculate_totals', 'apply_discount_based_on_item_count', 10, 1);
2 
3function apply_discount_based_on_item_count($cart) {
4 // Only run this in the frontend and make sure we're not in the admin or doing ajax
5 if (is_admin() && !defined('DOING_AJAX')) return;
6 
7 // Check if the action has already been fired to prevent multiple discount applications
8 if (did_action('woocommerce_before_calculate_totals') >= 2) return;
9 
10 // Count the number of items in the cart
11 $item_count = $cart->get_cart_contents_count();
12 
13 // Apply a $100 discount if there are 3 or more items
14 if ($item_count >= 3) {
15 $discount = 100;
16 
17 // Add a negative fee to the cart to apply the discount
18 $cart->add_fee('Bulk Discount', -$discount, true);
19 }
20}

0

Related Snippets

Please see some snippets below related to this snippet..

WooCommerce

AI Verified

1

Woocommerce - Remove the additional information tab

Added: 1 year ago

Last Updated: 2 weeks ago

WooCommerce

AI Verified

0

WooCommerce Choose Charity For Fixed Donation On Checkout

Added: 1 year ago

Last Updated: 1 year ago

This guide shows you how can you can a fixed fee to the order based on a radio button selection on the checkout. Specific to this example, a fixed fee is added in the form of a donation based on a cha...

WooCommerce

AI Verified

0

WordPress Create Custom User Role

Added: 1 year ago

Last Updated: 1 year ago

This code snippet creates a custom role showing every possible default capability.

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

Performance

AI Verified

32

Remove Unused Javascript

Added: 1 year ago

Last Updated: 3 days ago

Remove Unused Javascript - and improve your Page Speed Insight Score

WooCommerce

Pro Verified

10

Deactivate some WooCommerce Checkout Fields

Added: 1 year ago

Last Updated: 2 weeks ago

Deactivate some WooCommerce Checkout Fields from showing

Elementor

AI Verified

6

CSS Grid Aid

Added: 8 months ago

Last Updated: 1 month ago

This can be used for any WordPress Builder to aid working with CSS Grids.