View Single Post
  #16  
Old 11-12-2015, 07:34 AM
Cyril D's Avatar
Cyril D Cyril D is offline
Ecwid Team
 
Join Date: Feb 2015
Location: Ecwid HQ
Posts: 530
Default

Hello,

Thanks for contacting us!

As was already mentioned in this thread, while there's no such built-in functionality in Ecwid, you can specify the minimum and maximum quantity for a particular product by means of some JS codes and Ecwid JS API.

I've updated the previous JS code and added the ability to specify the min and max values for the product quantity—use the $minValue and $maxValue variables for this. Please, mind that I'm not a professional programmer and all the codes are provided as is.

Code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
if (typeof(Ecwid) == "object") {
    Ecwid.OnPageLoaded.add(function(page) {
        var $productIds = [47856077, 47856078, 47856079];
        if (page.type == "PRODUCT")
            for (var i = 0; i < $productIds.length; i++) {
                if ($productIds[i] == page.productId) {

                    $minValue = 5;
                    $maxValue = 10;
                    $oldValue = $minValue;
                    $quantityFieldClassname = "input.ecwid-productBrowser-details-qtyTextField";

                    $($quantityFieldClassname).val($minValue);

                    $($quantityFieldClassname).on("input", function() {
                        $currentQuantityFieldValue = $(this).val();

                        if ($currentQuantityFieldValue < $minValue || $currentQuantityFieldValue > $maxValue)
                        {
                            $(this).val($oldValue);
                            return false;
                        } else
                            $oldValue = $(this).val();
                    });
                }
            }
    });
}
</script>
Also, pay attention to the red magic numbers in brackets—they are IDs of products to which you want to apply this script. How to get these IDs? We have an article on this, please, take a look.

In case you need to apply this code to all products, you can use a simplified version of the above code:

Code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
    if (typeof(Ecwid) == "object") {
        Ecwid.OnPageLoaded.add(function(page) {
            if (page.type == "PRODUCT") {

                $minValue = 5;
                $maxValue = 10;
                $oldValue = $minValue;
                $quantityFieldClassname = "input.ecwid-productBrowser-details-qtyTextField";

                $($quantityFieldClassname).val($minValue);

                $($quantityFieldClassname).on("input", function() {
                    $currentQuantityFieldValue = $(this).val();

                    if ($currentQuantityFieldValue < $minValue || $currentQuantityFieldValue > $maxValue) {
                        $(this).val($oldValue);
                        return false;
                    } else
                        $oldValue = $(this).val();
                });

            }
        });
    }
</script>
To make these codes work, simply add it just below the Ecwid integration code aka the Product Browser.

By the way, now users on paid plans are able to add custom JS codes to their Starter Sites—thanks to smartMart who'd created an app that allows adding custom JS/CSS codes to all Ecwid storefronts right in the control panel. The app called "Custom JS/CSS Injector" and it's in beta currently, but it works just fine—I've tried it myself. You can drop smarMart a message via DM or email and them for a beta access.

Hope this helps!
__________________
Cyril D.
Ecwid Customer Care Team

More tips and hints on Ecwid use in our Help Center

⬇ Please click Thanks if my reply helped you.

Last edited by Cyril D; 11-12-2015 at 09:57 PM.