Home / Admin / Boost Sales with Dynamic Pricing Strategy with Progressive Discounts in WooCommerce – No Plugin
Duplicate Snippet

Embed Snippet on Your Site

Boost Sales with Dynamic Pricing Strategy with Progressive Discounts in WooCommerce – No Plugin

Post Intro
woocommerce tutorial -How to Boost Sales and Create a Dynamic Pricing Strategy with Progressive Discounts in WooCommerce
Welcome to this tutorial where we will explore how to implement progressive discounts in your WooCommerce cart. By leveraging the power of progressive discounts, we can enhance our pricing strategy to boost sales and create a dynamic shopping experience for our customers.

In this tutorial, we will cover the step-by-step process of implementing progressive discounts using the provided code snippet. We will learn how to set up discount tiers based on quantity thresholds and apply the discounts automatically to the cart.

By the end of this tutorial, you will have the knowledge to optimize your WooCommerce store by offering progressive discounts, which can attract more customers, increase order values, and ultimately drive sales growth.

Code Preview
php
<?php
// Hook to apply progressive discount based on cart quantity
add_action('woocommerce_cart_calculate_fees', 'progressive_discount', 20, 1);
function progressive_discount($cart) {
    // Check if the action is not triggered in the admin and not during AJAX
    if (is_admin() && !defined('DOING_AJAX'))
        return;
    // Define progressive discount tiers
$discount_tiers = array(
    // First discount tier
    array(
        'quantity' => 10,              // The quantity at which this tier applies
        'discount_rate' => 0.02,       // The discount rate for this tier (2%)
        'label' => 'Progressive Discount'  // Label for the discount
    ),
    // Second discount tier
    array(
        'quantity' => 20,              // The quantity at which this tier applies
        'discount_rate' => 0.03,       // The discount rate for this tier (3%)
        'label' => 'Additional Progressive discount 1'  // Label for the discount
    ),
    // Third discount tier
    array(
        'quantity' => 50,              // The quantity at which this tier applies
        'discount_rate' => 0.04,       // The discount rate for this tier (4%)
        'label' => 'Additional Progressive Discount 2'  // Label for the discount
    ),
);
    // Initialize total and discounted quantities
    $total_quantity = 0;
    $discounted_quantity = 0;
    // Loop through cart items
    foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
        $product_id = $cart_item['product_id'];
        $quantity = $cart_item['quantity'];
        $enable_discount = get_post_meta($product_id, 'progressive_offer', true);
        // Check if progressive offer is enabled for the product
        if ($enable_discount === 'yes') {
            $total_quantity += $quantity;
            // Determine the applicable discount amount based on the tiers
            $discount_amount = 0;
            $previous_tier_quantity = 0;
            $current_discount_label = '';
            // Loop through discount tiers
            foreach ($discount_tiers as $discount_tier) {
                if ($total_quantity >= $discount_tier['quantity']) {
                    // Calculate the discount for this tier
                    $tier_discount_amount = ($total_quantity - max($previous_tier_quantity, $discount_tier['quantity'])) * $cart->subtotal * $discount_tier['discount_rate'] / $cart->cart_contents_count;
                    // Add the tier discount amount to the total discount amount
                    $discount_amount += $tier_discount_amount;
                    // Update the previous tier quantity
                    $previous_tier_quantity = $discount_tier['quantity'];
                    // Update the current discount label
                    $current_discount_label = $discount_tier['label'];
                } else {
                    break;
                }
            }
        }
    }
    // Apply the discount as a fee to the cart if the discount amount is greater than 0
    if ($discount_amount > 0) {
        $cart->add_fee($current_discount_label, -$discount_amount);
    }
}
// Hook to add the progressive offer checkbox to product options
add_action('woocommerce_product_options_general_product_data', 'add_progressive_offer_checkbox');
function add_progressive_offer_checkbox() {
    global $post;
    woocommerce_wp_checkbox(
        array(
            'id' => 'progressive_offer',
            'label' => __('Enable Progressive Offer', 'woocommerce'),
            'description' => __('Check this box if you want this product to be eligible for the progressive offer.', 'woocommerce'),
            'value' => get_post_meta($post->ID, 'progressive_offer', true),
        )
    );
}
// Hook to save the value of the progressive offer checkbox when product is saved
add_action('woocommerce_process_product_meta', 'save_progressive_offer_checkbox');
function save_progressive_offer_checkbox($post_id) {
    // Save the value of the progressive offer checkbox
    $progressive_offer = isset($_POST['progressive_offer']) ? 'yes' : 'no';
    update_post_meta($post_id, 'progressive_offer', $progressive_offer);
}
// Hook to display a notice for products eligible for the progressive offer
add_action('woocommerce_after_add_to_cart_button', 'add_progressive_offer_notice');
function add_progressive_offer_notice() {
    global $post;
    // Check if the progressive offer is enabled for this product
    $enable_discount = get_post_meta($post->ID, 'progressive_offer', true);
    if ($enable_discount === 'yes') {
        // Display a message indicating that this product is eligible for the progressive offer
        echo '<div class="woocommerce-message">' . __('This product is eligible for the progressive offer!', 'woocommerce') . '</div>';
        // Define progressive discount tiers
      $discount_tiers = array(
    // First discount tier
    array(
        'quantity' => 10,              // The quantity at which this tier applies
        'discount_rate' => 0.02,       // The discount rate for this tier (2%)
        'label' => 'Progressive Discount'  // Label for the discount
    ),
    // Second discount tier
    array(
        'quantity' => 20,              // The quantity at which this tier applies
        'discount_rate' => 0.03,       // The discount rate for this tier (3%)
        'label' => 'Additional Progressive discount 1'  // Label for the discount
    ),
    // Third discount tier
    array(
        'quantity' => 50,              // The quantity at which this tier applies
        'discount_rate' => 0.04,       // The discount rate for this tier (4%)
        'label' => 'Additional Progressive Discount 2'  // Label for the discount
    ),
);
// Hook to enqueue custom CSS for styling the progressive offer message
add_action('wp_enqueue_scripts', 'add_custom_css');
function add_custom_css() {
    // Add custom CSS to style the progressive offer message
    $custom_css = "
        .woocommerce-message {
            clear: both;
            margin-top: 1em;
            background-color: white;
        }
    ";
    wp_add_inline_style('woocommerce-general', $custom_css);
}

Comments

Add a Comment