0

I have a sample pack product that needs to require 5 different flavors chosen by the customer.

var maxCheckedCount = <?php echo $maxCheckedCount = $this->getProduct()->getMaxCheckedCount(); ?>+1;
var maxCheckedAlertMessage = "<?php echo $this->__('You have reached maximum allowed selections.'); ?>";

jQuery('input[type=checkbox]').click(function(){
    var n = jQuery('input:checked').length;
    if(n>=maxCheckedCount){
        jQuery(this).prop('checked',false);
        alert(maxCheckedAlertMessage);
    }
});

I added the above code to product/view.html to check get the maximum selectable options, and prevent any more. I am wondering how I can check that the minimum selectable options are selected before allowing the item to be added to cart.

I have created a min_checked_count attribute along with the max_checked_count attribute from above, now I just need to know where to check against it.

1 Answer 1

0

I answered this question initially from which you've taken the sample code here: how I can limit the maximum selectable simple products?.

The answer, really, is very easy. You will extend the click handler to check if the minimum number has been reached and then enable the Proceed to Checkout button:

var maxCheckedCount = <?php echo $maxCheckedCount = $this->getProduct()->getMaxCheckedCount(); ?>+1;
var maxCheckedAlertMessage = "<?php echo $this->__('You have reached maximum allowed selections.'); ?>";
var minRequired = 3; //this is your minimum value

//Disabling the checkout button on load
var $btnCheckout = jQuery('.btn-checkout')
$btnCheckout.attr('disabled','disabled');

jQuery('input[type=checkbox]').click(function(){
    var n = jQuery('input:checked').length;

    //check for minimal value, and enable the button
    if(n>=minRequired){
        $btnCheckout.removeAttr('disabled');
    }

    if(n>=maxCheckedCount){
        jQuery(this).prop('checked',false);
        alert(maxCheckedAlertMessage);
    }
});

Be aware that this only checks if the minimum has been reached when the checkbox is clicked. If for some reason you had the minimum number of checkboxes pre-checked on load you would have to modify the script to not disable the button by default.

4
  • 1
    I appreciate the speedy reply, and this worked perfectly! In case anyone else wants to use this, in my case I wanted to disable the Add to Cart button rather than the Checkout button. I simply replaced btn-checkout with btn-cart.
    – DaChronDon
    Commented Sep 3, 2013 at 16:58
  • Is there an easy way to make sure this only disables the button on products with a required amount of selectable options? As it stands, this disables the add to cart button on all products even if they have no checkboxes.
    – DaChronDon
    Commented Sep 3, 2013 at 17:12
  • I was able to check against the sku of the item using php var sku = "<?php echo Mage::getModel('catalog/product')->load($_product->getId())->getSku(); ?>"; if (sku == "SKU-HERE") { $btnCart.attr('disabled','disabled'); } But still open to better ideas
    – DaChronDon
    Commented Sep 3, 2013 at 17:15
  • I don't like sku-based inclusion lists. Rather add a body class to the page via an observer to check onload or check if there are checkboxes in a particular div before disabling.
    – philwinkle
    Commented Sep 3, 2013 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.