Custom WooCommerce Settings Page

Custom WooCommerce Settings Page

Getting your Trinity Audio player ready...
Step 3: Apply Bulk Discounts in WooCommerce

Now, you need to hook into WooCommerce’s price calculation process to apply the discount based on the settings.

add_action('woocommerce_cart_calculate_fees', 'apply_bulk_discount_based_on_quantity');

function apply_bulk_discount_based_on_quantity() {
    if (is_admin() && !defined('DOING_AJAX')) {
        return;
    }
    
    // Get the discount settings
    $bulk_quantity_threshold = get_option('wc_bulk_discount_threshold', 10);
    $bulk_discount_percentage = get_option('wc_bulk_discount_percentage', 10);
    
    // Check the cart quantity
    $cart_quantity = WC()->cart->get_cart_contents_count();
    
    // Apply the discount if the quantity exceeds the threshold
    if ($cart_quantity >= $bulk_quantity_threshold) {
        // Calculate discount
        $discount = (WC()->cart->get_cart_total() * ($bulk_discount_percentage / 100));
        
        // Apply discount
        WC()->cart->add_fee('Bulk Discount', -$discount);
    }
}

Similar Posts