Variant quantity discounts

How do I create qty discounts for a product with 50 variants in bulk in admin.
I have 1 tee shirt that comes in 5 sizes and 10 colours, I added the qty discount to the main product but dont see anywhere I can get it to flow through to all the other colours and size variants?

Should be able to select this checkbox upon adding the product to the conditions

image

Thanks…but I think you are talking about promotions.
I am talking about product qty discounts tab in the product record to apply to all variants in the product at once.

You are correct, this is within promotions. Can’t you use this to achieve what you are trying to achieve?

We already have 1000,s of products with quantity discounts that we have set from the early days of cs cart.
They are all.percentage discounts of 10,25,50,100 and 1000 qty of products which was easily controlled by export import. But now with all the variants system (not the old product options system) it seems the qty discounts can’t be “applied to all”.

Oh, with those sorts of quantities, even if you didn’t have some preconfigured then using the promtions path would not work well as the promotions path doesn’t cater for ranges of product quantities.

I had a talk to ChatGPT and asked it for some code using the mychanges add-on (assuming it is activated) it recommended the following (test in development first)

  1. Go to the app/addons/my_changes/overrides/addons folder in your CS-Cart installation and create a new file called “fn.quantity_discounts.php” (if it doesn’t exist already).
  2. Open the fn.quantity_discounts.php file in a code editor and add the following code:

[code deleted, updated code in post below]

This code defines a new function called “fn_add_quantity_discount_to_product_variants” that will add quantity discounts to all variants of a specific product.

  1. Save the fn.quantity_discounts.php file and go to your CS-Cart admin panel.
  2. Navigate to Products > Products and edit the product you want to apply the discount to.
  3. Go to the “Quantity Discounts” tab and set up the discount tiers as desired.
  4. Click on the Save button to save the product.

That’s it! The quantity discounts will now be applied to all variants of the product. You can test the changes by adding different quantities of the product to your cart and checking if the correct discounts are applied. Note that the discounts set up through the “Quantity Discounts” tab will only apply to that specific product, and not to other products or categories.

Many thanks @chickentwisty Ill give it a try.

and for anyone else who may be looking in, what I was hoping for was that when you go to a variation group, and select varaiations, would be nice if in “edit selected” could contain “qty discounts” or similar

[code deleted code in post below]

Hmmm?
both codes still seem to only add discounts to the main product,
cach cleared manually, tried % dicount and £ discount?

@CS-Cart_team reply at all maybe

ChatGPT agreed third time lucky?

<?php

use Tygh\Registry;

if (!defined('BOOTSTRAP')) { die('Access denied'); }

function fn_add_quantity_discount_to_product_variants(&$variants_data) {
    if (isset($variants_data['variants'])) {
        foreach ($variants_data['variants'] as $variant_id => &$variant) {
            $product_id = $variant['product_id'];
            $product_data = fn_get_product_data($product_id, CART_LANGUAGE, 'full', false, false, true, false, true);

            if (isset($product_data['product_quantity_discounts'])) {
                $amount = $variant['amount'];
                $discount_found = false;

                foreach ($product_data['product_quantity_discounts'] as $discount) {
                    if ($amount >= $discount['from'] && $amount <= $discount['to']) {
                        $variant['quantity_discounts'][] = array(
                            'quantity' => $discount['from'].'-'.$discount['to'],
                            'price' => $variant['price'] * (1 - $discount['discount']/100),
                        );

                        $discount_found = true;
                        break;
                    }
                }

                if (!$discount_found) {
                    // Remove any existing quantity discounts from the variant
                    $variant['quantity_discounts'] = array();
                }
            }
        }
    }
}

appears to be working, the discount quantity is applied to each variant, unless what you are looking for is the discount not to be applied to each variant but instead the total quantity of all variants i.e. as if the quantity in the attached scenario was six?

image

thanks, yeah that looks better, but yes…we give discounts on say tee shirts, doesnt matter about the colour or size mixing but if they buy 10 (mixed colours & sizes) the total of variants then the discount should apply

I tried but didnt get a result, but Ill take another look

thanks

I have struggled to get it to work by showing the discount on each variant, this one gets the right cart total though by showing the individual quantity discount on each based on the quantity of each variant and then adding a cart discount like so …

image

and this code would apply (but instead delete the function override and put it in init.php under the mychanges folder)

use Tygh\Registry;

if (!defined('BOOTSTRAP')) { die('Access denied'); }

fn_register_hook('calculate_cart', function(&$cart, &$auth, $calculate_taxes, $force_notification, $cart_products) {
    foreach ($cart_products as $cart_product) {
        $product_id = $cart_product['product_id'];
        $product_data = fn_get_product_data($product_id, CART_LANGUAGE, 'full', false, false, true, false, true);
        $total_quantity = 0;

        if (isset($cart_product['product_options']['variants'])) {
            foreach ($cart_product['product_options']['variants'] as $variant) {
                $amount = $variant['amount'];
                $total_quantity += $amount;
            }
        } else {
            $total_quantity = $cart_product['amount'];
        }

        if (isset($product_data['product_quantity_discounts'])) {
            $discount_found = false;

            foreach ($product_data['product_quantity_discounts'] as $discount) {
                if ($total_quantity >= $discount['from'] && $total_quantity <= $discount['to']) {
                    $price_discount = $product_data['price'] * (1 - $discount['discount']/100);
                    $cart_product['price'] = $price_discount;
                    $cart_product['base_price'] = $price_discount;

                    $discount_found = true;
                    break;
                }
            }

            if (!$discount_found) {
                // Remove any existing quantity discounts from the variants
                if (isset($cart_product['product_options']['variants'])) {
                    foreach ($cart_product['product_options']['variants'] as &$variant) {
                        $variant['quantity_discounts'] = array();
                    }
                } else {
                    $cart_product['quantity_discounts'] = array();
                }
            }
        }

        // Update the cart product data
        $cart_products[$cart_product['cart_key']] = $cart_product;
    }

    // Update the cart data
    $cart['products'] = $cart_products;
    $cart['subtotal'] = fn_cart_get_subtotal($cart_products);
    $cart['total'] = fn_format_price($cart['subtotal'], $cart['secondary_currency'], $cart['primary_currency']);
});


I appreciate the help , but seems i get this error when adding the code in init.php under
/my_changes

init.php already exists with code from an old ez merchant mailer, but I removed that and created new, but also same problem.
My version is 4.15.2

i didn’t notice but i too am getting that, can’t figure out how to stop it.

What about Administration → Import data → Products (deprecated) → Quantity discounts ?

That is before my time, but potentially to what the OP was referring to.

sorry for delay, vacation.

Thanks, yes we have used this in the past quite a lot but when adding products on the fly, it would be useful to be able to add the discounts recurring through all varaiants.

It’s possible create a cron job, like standard Products import, to update quantity discounts automatically?

I am afraid, old import does not support cron tasks