Last active
February 2, 2022 18:14
-
-
Save rwkyyy/26adeb4b418e59199a6e7d94f8cbe202 to your computer and use it in GitHub Desktop.
WooCommerce conditional subtotal cart discounts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: EVD - Card Discount Rules | |
* Plugin URI: https://rwky.ro | |
* Description: Discount rules for cart | |
* Version: 1.0 | |
* Author: Eduard Vasile Doloc | |
* Author URI: http://rwky.ro | |
* Developer: Eduard Vasile Doloc | |
* Developer URI: http://rwky.ro | |
* Text Domain: evd_discounts | |
* Domain Path: /languages | |
* | |
* WC requires at least: 2.2 | |
* WC tested up to: 3.3.5 | |
* | |
* License: GNU General Public License v3.0 | |
* License URI: http://www.gnu.org/licenses/gpl-3.0.html | |
*/ | |
/** | |
* Check if WooCommerce is active - we don't want that, do we? | |
**/ | |
function evd_woo_is_absent() { | |
if (!defined('WC_VERSION')) { | |
?> | |
<div class="error notice"> | |
<p> | |
<?php _e( 'EVD Cart Discount Rules - <strong>WooCommerce is not active!</strong>', 'evd_discounts' ); ?> | |
</p> | |
</div> | |
<?php | |
} | |
} | |
add_action( 'admin_notices', 'woo_is_absent' ); | |
// Magic & logic | |
add_action('woocommerce_cart_calculate_fees', 'evd_woo_discounts'); | |
function evd_woo_discounts() { | |
global $woocommerce; | |
$excluded_amount = $discount_percent = 0; | |
$working_total = $woocommerce->cart->cart_contents_total; | |
# Only apply manual discount if no coupons are applied | |
if (!$woocommerce->cart->applied_coupons) { | |
# Find any items in cart that belong to the restricted categories | |
foreach ($woocommerce->cart->cart_contents as $item) { | |
$product_categories = get_the_terms($item['product_id'], 'product_cat'); | |
if (empty($product_categories) || is_wp_error($product_categories) || !$product_categories) { | |
if (is_wp_error($product_categories)) { | |
wp_die($product_categories->get_error_message()); | |
} | |
else { | |
$product_categories = new WP_Error('no_product_categories', "The product \"".$item->post_title."\" doesn't have any categories attached, thus no discounts can be calculated.", "Fatal Error"); | |
wp_die($product_categories); | |
} | |
} | |
foreach ($excluded_categories as $excluded_category) { | |
foreach ($product_categories as $category) { | |
if ($excluded_category == $category->term_id) { | |
$excluded_amount += $item['line_subtotal']; # Increase our discounted amount | |
$working_total -= $item['line_subtotal']; # Decrease our discounted amount | |
} | |
} | |
} | |
} | |
# Logic to determine WHICH discount to apply based on subtotal | |
if ($working_total >= 100 && $working_total < 149) { | |
$discount_percent = 10; | |
} | |
elseif ($working_total >= 150 && $working_total < 249) { | |
$discount_percent = 15; | |
} | |
elseif ($working_total >= 250 && $working_total < 349) { | |
$discount_percent = 20; | |
} | |
elseif ($working_total >= 350 && $working_total < 499) { | |
$discount_percent = 25; | |
} | |
elseif ($working_total >= 500) { | |
$discount_percent = 30; | |
} | |
else { | |
$discount_percent = 0; | |
} | |
# Make sure cart total is eligible for discount | |
if ($discount_percent > 0) { | |
$discount_amount = ( ( ($discount_percent/100) * $working_total ) * -1 ); | |
$woocommerce->cart->add_fee('Discount', $discount_amount); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment