0

I am building the ecommerce magento website named "bookslab.in". I want to make the add to cart button freeze if the zipcode of the users they entered in the zipcode field is invalid & want to give the message "Zipcode is invalid, so you can't buy this product." I am providing you the view.phtml & cod.phtml. Please suggest the changes in the codes that i provided.

cod.phtml

<?php
/***************************************
 *** Cash On Delivery ***
 ***************************************
 */

$msgData = Mage::helper('netgo_cod')->getConfigData(); 
if($msgData['zip_status'] == 1){
?>
<div class="input-box">
    <div class="z-btn">
        <label>Zip Code : </label><input type="text" placeholder="Enter your pincode" value="" name="cod" class="product-custom-option required-entry" id="cod" size="29" style='color:black; font-size:12px'>
        <button type="button" onclick="checkCOD();" name="zip-check" title="Check" class="button" id="zip-check"><span><span>Check</span></span></button>
    </div>
    <div id="cod_msg"></div>
</div>

<script>
    function checkCOD(){
             var zipcode;
        var cod = $('cod').value;
        if(cod == ''){
            $('cod_msg').update('<span class="cod-error"><?php echo $msgData['emp_msg']; ?></span>');
            return
        }else{
            $('cod_msg').update('<img src="<?php echo Mage::getBaseUrl( Mage_Core_Model_Store::URL_TYPE_WEB, true ).'media/cod/img/ajax-loader-2.gif';?>">'); 
            new Ajax.Request('<?php echo Mage::getBaseUrl().'netgo/cod/check';?>', {
                method:'post',
                parameters: {zipcode: cod}, 
                onSuccess: function(transport) {
                    var response = transport.responseText || "no response text";
                    $('cod_msg').update(response);

                    if(response.search('Delivery of the book is not available on the above pincode')!=-1)
                                        {
                                          zipcode='checked';
                                        }
                                      else
                                      {
                                      zipcode='';
                                      }

                },
                onFailure: function() { alert('Something went wrong...'); }
            });
        }
    }
</script>
<?php } ?>

view.phtml

 <?php  
    ?>
    <?php $_helper = $this->helper('catalog/output'); ?>
    <?php $_product = $this->getProduct(); ?>
    <script type="text/javascript">
        var optionsPrice = new Product.OptionsPrice(<?php echo $this->getJsonConfig() ?>);
    </script>
    <div id="messages_product_view"><?php echo $this->getMessagesBlock()->toHtml() ?></div>
    <div class="product-view">
        <div class="product-essential">
            <form action="<?php echo $this->getSubmitUrl($_product, array('_secure' => $this->_isSecure())) ?>" method="post" id="product_addtocart_form"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
                <?php echo $this->getBlockHtml('formkey') ?>
                <div class="no-display">
                    <input type="hidden" name="product" value="<?php echo $_product->getId() ?>" />
                    <input type="hidden" name="related_product" id="related-products-field" value="" />
                </div>

                <div class="product-img-box">
                    <div class="product-name">
                        <h1><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></h1>
                    </div>
                    <?php echo $this->getChildHtml('media') ?>
                </div>

                <div class="product-shop">
                    <div class="product-name">
                        <span class="h1"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></span>
                    </div>

                    <div class="price-info">
                        <?php echo $this->getPriceHtml($_product); ?>
                        <?php echo $this->getChildHtml('bundle_prices') ?>
                        <?php echo $this->getTierPriceHtml() ?>
                    </div>

                    <div class="extra-info">
                        <?php echo $this->getReviewsSummaryHtml($_product, 'default', false)?>
                        <?php echo $this->getChildHtml('product_type_availability'); ?>
                    </div>

                    <?php echo $this->getChildHtml('alert_urls') ?>

                    <?php if ($_product->getShortDescription()):?>
                        <div class="short-description">
                            <div class="std"><?php echo $_helper->productAttribute($_product, nl2br($_product->getShortDescription()), 'short_description') ?></div>
                        </div>
                    <?php endif;?>




                    <?php echo $this->getChildHtml('other');?>

                    <?php if ($_product->isSaleable() && $this->hasOptions()):?>
                        <?php echo $this->getChildChildHtml('container1', '', true, true) ?>
                    <?php endif;?>

                </div>

                <div class="add-to-cart-wrapper">
                    <?php echo $this->getChildHtml('product_type_data') ?>
                    <?php echo $this->getChildHtml('extrahint') ?>

                    <?php if (!$this->hasOptions()):?>
                        <div class="add-to-box">
                            <?php if($_product->isSaleable()): ?>
                                <?php echo $this->getChildHtml('addtocart') ?>
                                <?php if( $this->helper('wishlist')->isAllow() || $_compareUrl=$this->helper('catalog/product_compare')->getAddUrl($_product)): ?>
                                    <span class="or"><?php echo $this->__('OR') ?></span>
                                <?php endif; ?>
                            <?php endif; ?>
                            <?php echo $this->getChildHtml('addto') ?>
                            <?php echo $this->getChildHtml('sharing') ?>
                        </div>
                        <?php echo $this->getChildHtml('extra_buttons') ?>
                    <?php elseif (!$_product->isSaleable()): ?>
                        <div class="add-to-box">
                            <?php echo $this->getChildHtml('addto') ?>
                            <?php echo $this->getChildHtml('sharing') ?>
                        </div>
                    <?php endif; ?>
                </div>

                <?php echo $this->getChildHtml('related_products') ?>

                <div class="clearer"></div>
                <?php if ($_product->isSaleable() && $this->hasOptions()):?>
                    <?php echo $this->getChildChildHtml('container2', '', true, true) ?>
                <?php endif;?>
            </form>
            <script type="text/javascript">
            //<![CDATA[
                var productAddToCartForm = new VarienForm('product_addtocart_form');
                productAddToCartForm.submit = function(button, url) {
                    if (this.validator.validate()) {
                        var form = this.form;
                        var oldUrl = form.action;

                        if (url) {
                           form.action = url;
                        }
                        var e = null;
                        try {
                            this.form.submit();
                        } catch (e) {
                        }
                        this.form.action = oldUrl;
                        if (e) {
                            throw e;
                        }

                        if (button && button != 'undefined') {
                            button.disabled = true;
                        }
                    }
                }.bind(productAddToCartForm);

                productAddToCartForm.submitLight = function(button, url){
                    if(this.validator) {
                        var nv = Validation.methods;
                        delete Validation.methods['required-entry'];
                        delete Validation.methods['validate-one-required'];
                        delete Validation.methods['validate-one-required-by-name'];
                        // Remove custom datetime validators
                        for (var methodName in Validation.methods) {
                            if (methodName.match(/^validate-datetime-.*/i)) {
                                delete Validation.methods[methodName];
                            }
                        }

                        if (this.validator.validate()) {
                            if (url) {
                                this.form.action = url;
                            }
                            this.form.submit();
                        }
                        Object.extend(Validation.methods, nv);
                    }
                }.bind(productAddToCartForm);
            //]]>
            </script>
        </div>

        <div class="product-collateral toggle-content tabs">
            <?php if ($detailedInfoGroup = $this->getChildGroup('detailed_info', 'getChildHtml')):?>
                <dl id="collateral-tabs" class="collateral-tabs">
                    <?php foreach ($detailedInfoGroup as $alias => $html):?>
                        <dt class="tab"><span><?php echo $this->escapeHtml($this->getChildData($alias, 'title')) ?></span></dt>
                        <dd class="tab-container">
                            <div class="tab-content"><?php echo $html ?></div>
                        </dd>
                    <?php endforeach;?>
                </dl>
            <?php endif; ?>
        </div>

        <?php echo $this->getChildHtml('upsell_products') ?>
        <?php echo $this->getChildHtml('product_additional_data') ?>

    </div>

I am also attaching the screenshot of my webpage:

enter image description here

  • Add disable attribute on add to cart button if zip code is not valid. – Vaibhav Ahalpara Oct 25 '16 at 8:42
  • how to add & where to add – Salman Dalal Oct 25 '16 at 8:45
  • you use ajax to check zip code is valid or not right ? if it's not valid then use javascript document.getElementById("addtocart").disabled = true; – Vaibhav Ahalpara Oct 25 '16 at 8:48
  • Okzz i will check & know you is it working or not? – Salman Dalal Oct 25 '16 at 8:52
  • it will works :) and you can also set message using same method – Vaibhav Ahalpara Oct 25 '16 at 8:53
0

You can disable the button using jQuery

if(response.search('Delivery of the book is not available on the above pincode')!=-1){
      zipcode='checked';
      jQuery('.btn-cart').attr('disabled', 'disabled'); // add this code
    } else { 
        zipcode='';
        jQuery('.btn-cart').removeAttr('disabled');
    }

This will work good but it will be client side to disable button. I also suggest you to validate this from server side

| improve this answer | |
  • He is not using jQuery and if we can solve this problem with javascript then why load jQuery library. – Vaibhav Ahalpara Oct 25 '16 at 8:56
  • and yes I agree that server-side validation is required . – Vaibhav Ahalpara Oct 25 '16 at 8:58
  • The Magento version he is using has jquery already loaded. So he wont have any problems with it – Priyank Oct 25 '16 at 8:58
  • Yeah user3046689 u suggested me right. But one problem is coming. – Salman Dalal Oct 25 '16 at 16:10
  • If user add the correct zipcode after the wrong zipcode then page does not getting refresh & the button of ADD TO CART is still freezed. How to refresh the page of product. – Salman Dalal Oct 25 '16 at 16:11
0

For Temporary you can disable that product with these code

<script>
function checkCOD(){
         var zipcode;
    var cod = $('cod').value;
    if(cod == ''){
        $('cod_msg').update('<span class="cod-error"><?php echo $msgData['emp_msg']; ?></span>');
        return
    }else{
        $('cod_msg').update('<img src="<?php echo Mage::getBaseUrl( Mage_Core_Model_Store::URL_TYPE_WEB, true ).'media/cod/img/ajax-loader-2.gif';?>">'); 
        new Ajax.Request('<?php echo Mage::getBaseUrl().'netgo/cod/check';?>', {
            method:'post',
            parameters: {zipcode: cod}, 
            onSuccess: function(transport) {
                var response = transport.responseText || "no response text";
                $('cod_msg').update(response);

                if(response.search('Delivery of the book is not available on the above pincode')!=-1)
                                    {
                                      zipcode='checked';
                                      jQuery('.btn-cart').prop("disabled", false);  
                                    }
                                  else
                                  {
                                  zipcode='';
                                    jQuery('.btn-cart').attr('disabled', 'disabled');
                                  }

            },
            onFailure: function() { alert('Something went wrong...'); }
        });
    }
}

For server side validation you can set this product is selble no and with your condition for that you can use this observer. catalog_product_is_salable_after and restrict user to addtocart that product by issalable to no.

| improve this answer | |
  • Yeah user3046689 u suggested me right. But one problem is coming. – Salman Dalal Oct 26 '16 at 6:35
  • If user add the correct zipcode after the wrong zipcode then page does not getting refresh & the button of ADD TO CART is still freezed. How to refresh the page of product. – Salman Dalal Oct 26 '16 at 6:36
  • check my updated answer – Murtuza Zabuawala Oct 26 '16 at 6:39
  • When you updated this Murtuza – Salman Dalal Oct 26 '16 at 6:55
  • 1
    @zus you can check all items in cart checkout_cart_save_before via this event and you can remove specific item from cart as per your logic – Murtuza Zabuawala Dec 29 '17 at 6:39

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.