2

Hi can you help me for limit multiple checkbox selections?

I have create a bundle product with 100 simple products, how I can limit the maximum selectable simple products to 10 or 20 or other value recorded as custom attribute into bundle product?

Please help me

Thank you in advise

2

Use javascript to limit the maximum number selected. Place the following into app/design/frontend/[yourpackage]/default/template/catalog/product/view.phtml. Create an attribute called max_checked_count as a number/integer type.

This assumes you're using jQuery 1.6+:

var maxCheckedCount = <?php echo $maxCheckedCount = $this->getMaxCheckedCount(); ?>;
var maxCheckedAlertMessage = "<?php echo $this->__('Only %d selections can be chosen at once.', $maxCheckedCount); ?>";

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

Here is a Fiddle of the above idea working - though the Fiddle doesn't have the PHP bits: http://jsfiddle.net/rREfg/1/

  • Hi Phil, you had a big idea, many many thanks !!! – Loris Jul 1 '13 at 10:10
  • But this implementation will be erased when magento will be updated to 1.8 version if I don't use [myPackage]? Do I create a [myPackage] correct? And if I need to modify Magento core, for simple things, how I need to do? Thanks in advice ! Loris – Loris Jul 1 '13 at 10:27
  • Correct - you'll need to create your own design package. You don't need to do anything other than create the package folder and copy in only the items you'll need to update - in this case the catalog/product/view.phtml. I would suggest reading this: magentocommerce.com/wiki/4_-themes_and_template_customization/0-_theming_in_magento/package – philwinkle Jul 1 '13 at 22:08
  • Hi Phil, please tell me where I have to insert your code into view.phtml (copied from core, right?) ? Because I don't know where put the code. And I'm not sure about JQuery is loaded into the page, how can I check is JQuery is loaded? Many thanks in advice. Loris – Loris Jul 2 '13 at 7:15
1

I could actually only make it work like this (I am no coder)

    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 echo $maxCheckedCount = $this->getProduct()->getMaxCheckedCount(); ?>+1; "getProduct()->" and the "+1" (as I see it if you enter the number 5 in Magento backend then the visitor is actually only allowed to check 4 (four) boxes not 5)

I placed the script in the bottom of this file: /app/design/frontend/default/YOURTEMPLATENAME/template/catalog/product/view.phtml

And remember now all product that have checkboxes and this attribute connected HAVE to have a number in that attribute - else the visitor will be allowed 0 (zero) click!

I do not know how to fix that with a php if statement (so better create a attribute set specific for bundle products) Magento 1.7

  • If you will avoid the problem with 0 allowed checked then remove the +1 from the script BUT remember then that if a visitor should be able to select 10 products then you actually have to write 11 (eleven) in Magento backend – fullmag Jul 31 '13 at 3:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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