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

0

Gutenberg for Woocommerce product pages

Added: 1 year ago

Last Updated: 1 year ago

Want to use Gutenberg for Woocommerce? This snippet will make it happen (but might be useless in the future though)

WooCommerce

AI Verified

0

Add user payment method to admin email

Added: 1 year ago

Last Updated: 1 year ago

WooCommerce

AI Verified

2

WooCommerce Show Custom Content Under Checkout Fields

Added: 1 year ago

Last Updated: 2 months ago

Sometimes, you may wish to provide additional instructions to your customers when they are completing the WooCommerce Checkout form. This quick guide shows you how you can add any custom content you l...

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

Performance

AI Verified

34

Remove Unused Javascript

Added: 1 year ago

Last Updated: 1 week 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 months ago

Deactivate some WooCommerce Checkout Fields from showing

Elementor

AI Verified

6

CSS Grid Aid

Added: 9 months ago

Last Updated: 3 months ago

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