4

In cart page when postal code is entered and pressed get quote button then it submits one form and shows the rate of that postal code after that update button appears and clicking on that updates the total.

So basically there are two forms and after that it affects the total.

Is there any way that i want to do this (Submit both forms) on single event like on blur. In short i want to do it through ajax without refreshing page.

Shipping and estimate on cart page

Shipping.phtml code is

<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * @category   design_default
 * @package    Mage
 * @copyright  Copyright (c) 2004-2007 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
?>
<style>
.custom-xip{position: relative; top: -78px; width: 130px; margin-left: 140px;}
.custom-xip > label{position: relative; left: -60px; top: 30px;}
#my_shipping_estimate h3, #my_shipping_estimate #destination, #my_shipping_estimate #country, #my_shipping_estimate #get_quote{ display:none;}
@media(max-width: 470px){.img-media-res{margin-left: 0 !important;} .custom-xip{margin-left: 113px !important;}}
</style>
<div class="shipping-estimate box" id="my_shipping_estimate">
<h3><?php echo $this->__('Estimate Shipping and Tax') ?></h3>
<form action="<?php echo $this->getUrl('checkout/cart/estimatePost') ?>" method="post" id="shipping-zip-form">
    <p id="destination"><?php echo $this->__('Enter your  destination to get a shipping estimate.') ?></p>
    <p id="country"><label for="country"><?php echo $this->__('Country') ?></label><br /><span><?php echo Mage::getBlockSingleton('directory/data')->getCountryHtmlSelect($this->getEstimateCountryId()) ?></span></p>
    <?php //if($this->getStateActive()): ?>
    <div class="form-group">
        <!--
        <label for="region_id"><?php echo $this->__('State/Province') ?></label><br />
        <select id="region_id" name="region_id" title="<?php echo $this->__('State/Province') ?>" class="validate-select form-control" style="display:none">
            <option value=""><?php echo $this->__('Please select region, state or province') ?></option>
        </select>
        -->
        <script type="text/javascript">
            $('region_id').setAttribute('defaultValue',  "<?php echo $this->getEstimateRegionId() ?>");
        </script>
        <input type="text" id="region" name="region" value="<?php echo $this->htmlEscape($this->getEstimateRegion()) ?>"  title="<?php echo $this->__('State/Province') ?>" class="form-control input-text" style="display:none" />
    </div>
    <?php //endif; ?>
    <?php if($this->getCityActive()): ?>
    <p><label for="city"><?php echo $this->__('City') ?></label><br />
        <input class="form-control input-text" id="city" type="text" name="estimate_city" value="<?php echo $this->htmlEscape($this->getEstimateCity()) ?>" />
    </p>
    <?php endif; ?>
    <img class="img-media-res" style="margin-top: 44px; margin-left: 28px;" src="<?php echo $this->getSkinUrl('images/freight_bad.png');?>" id="imageFlag">
    <div class="form-group custom-xip">
    <label style="color: #000; font-weight: normal !important;"><?php echo $this->__('Enter your postcode to calculate freight: ') ?></label>
    <input  class="input-text form-control" type="text" id="postcode" style="width: 70px; margin-left: 78px; margin-top: -3px; height: 24px;" name="estimate_postcode" value="<?php echo $this->htmlEscape($this->getEstimatePostcode()) ?>"/>
    </div>
    <p><button id="get_quote" type="button" onclick="coShippingMethodForm.submit(); return false;" class="form-button-alt btn btn-danger"><span><?php echo $this->__('Get a Quote') ?></span></button></p>
</form>
<script type="text/javascript">
    new RegionUpdater('country', 'region', 'region_id', <?php echo $this->helper('directory')->getRegionJson() ?>);
</script>
<?php if (($_shippingRateGroups = $this->getEstimateRates())): ?>
<div class="divider"></div>
<form id="co-shipping-method-form" action="<?php echo $this->getUrl('checkout/cart/estimateUpdatePost') ?>">
    <dl class="shipment-methods">
        <?php foreach ($_shippingRateGroups as $code => $_rates): ?>
            <dt><?php //echo $this->getCarrierName($code) ?></dt>
            <dd>
                <ul class="list-unstyled">
                <?php foreach ($_rates as $_rate): ?>
                    <li style="visibility:hidden" class="<?php if ($_rate->getErrorMessage()) echo 'error-msg';?>">
                       <?php if ($_rate->getErrorMessage()): ?>
                            <?php echo $_rate->getErrorMessage() ?>
                       <?php else: ?>
                           <input name="estimate_method" type="radio" value="<?php echo $this->htmlEscape($_rate->getCode()) ?>" id="s_method_<?php echo $_rate->getCode() ?>"<?php if($_rate->getCode()===$this->getAddressShippingMethod()) echo ' checked="checked"' ?> />
                            <input  name="estimate_method" type="radio" value="<?php echo $this->escapeHtml($_rate->getCode()) ?>" id="s_method_tablerate_bestway" class="radio" checked="checked">
                            <label for="s_method_<?php echo $_rate->getCode() ?>"><?php echo $_rate->getMethodDescription() ?> - <strong><?php echo $_rate->getPrice() ?></strong></label>
                       <?php endif ?>
                    </li>
                <?php endforeach; ?>
                </ul>
            </dd>
        <?php endforeach; ?>
    </dl>
    <fieldset ><button style="" id="button_total" type="submit" class="form-button-alt btn btn-danger" name="do" value="<?php echo $this->__('Update Total') ?>"><span><?php echo $this->__('Update Total') ?></span></button></fieldset>
</form>
<?php endif; ?>

<script type="text/javascript">
    var coShippingMethodForm = new VarienForm('shipping-zip-form');
    Validation.addAllThese(
    [
           ['validate-postcode', '<?php echo $this->__('Please enter a valid zip code. For example 90602 or 90602-1234.') ?>', function(v) {
                if( $('shipping-postcode').value.match(/(^[A-z0-9]{2,10}([\s]{0,2}|[\-]{0,2})[A-z0-9]{2,10}$)/ )) {
                    return true;
                } else {
                    return false;
                }
           }]
    ]
    );
0

Before starting,we need to what are the functionality of two forms.

  • First form is given result list of available shipping methods basic of your select country and other address details.
  • Another update total basic of selected method.

You could not merge two form into one form because of whenever an address have multiple shipping methods then you need to selected one of those.For this case it should be wrong process.

An idea ,consist of two form.But all two form content will be update via ajax.

Lets go ESTIMATE SHIPPING AND TAX form.

for doing all using ajax for this form.You need to change something

  • Change Get Quote button on click code

from

onclick="coShippingMethodForm.submit()";

to

onclick="coShippingMethodForm.submit(); return false;"

Then on coShippingMethodForm.submit=function() call a prototype ajax for getting available shipping methods list:

new Ajax.Request(this.form.action, {
          method:this.form.method,
          parameters: this.form.serialize(),
          onLoading:function(){    },
          onSuccess: function(transport) {
          jQuery("#avaliable-Shipping-method").html(transport.responseText);

            },
          onFailure: function() { }
          });

Now you need to override Checkout Cartcontroller.php for getting available shipping methods using ajax and response the list shipping from estimatePostAction() .

and create a div#avaliable-Shipping-method for update your formusing list of avlaible shipping methods

  • thanks for Your quick response.In my case i have only one shipping method and i will only use that one specific shipping method in entire store..i am beginner in magento please annotate me in detail. if possible give me step by step solution, I will be very grateful.. – Yousaf Ali Oct 7 '15 at 9:19
  • please show the code of shipping.phtml – Amit Bera Oct 7 '15 at 13:57
  • I have updated code above please Check @Amit Bera – Yousaf Ali Oct 8 '15 at 4:27

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.