How to get shipping cost based user entries?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







1















I have PHP function for getting shipping cost form magento based on productId, productQty, countryId, postcode. Here my productId, productQty are static and countryId, postcode is dynamic based on user entry,



if i try with predefined value,



$results = getShippingEstimate('14419','1',"IN","642001"); // Predefined Value


Return my shipping cost as per based on magento using "IN","642001" (Country &Zipcode)



//if i try with PHP function,

if(isset($_POST['zip_postal_code']) && isset($_POST['country']))
{
$zip_postal_code = $_POST['zip_postal_code'];
$country = $_POST['country'];

}


$results = getShippingEstimate('14419','1',"$zip_postal_code","$country");


Not working.



NOTE : Here my shipping cost field as Auto response,



HTML FORM :



<html>
<head>
<title> Upgrade Cost</title>

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

</head>
<body>
<!-- Ajax data -->
<h2>KoluPadi Upgrade Form</h2>
<form action='ajax_data.php' method='POST' >

<table border="0" cellspacing="1" cellpadding="2">
<tr>
<th> First Name : </th>
<td> <input type="text" name="first_name"/> </td>
</tr>
<tr>
<th> Last Name : </th>
<td> <input type="text" name="last_name"/> </td>
</tr>

<tr>
<th> Address : </th>
<td> <input type="text" name="address"/> </td>
</tr>

<tr>
<th> City : </th>
<td> <input type="text" name="city"/> </td>
</tr>

<tr>
<th> State/Province : </th>
<td> <input type="text" name="state_province"/> </td>
</tr>

<tr>
<th> Zip/Postal Code : </th>
<td> <input type="text" name="zip_postal_code"/> </td>
</tr>

<tr>
<th> Country : </th>
<td>
<select name="country" style="width: 173px;">
<option value="AFG">Afghanistan</option>
<option value="ALA">Åland Islands</option>
<option value="ALB">Albania</option>
<option value="DZA">Algeria</option>
<option value="ASM">American Samoa</option>
<option value="AND">Andorra</option>
<option value="AGO">Angola</option>
<option value="AIA">Anguilla</option>
<option value="ATA">Antarctica</option>
<option value="ATG">Antigua and Barbuda</option>
<option value="ARG">Argentina</option>
<option value="ARM">Armenia</option>
<option value="ABW">Aruba</option>
<option value="AUS">Australia</option>
<option value="AUT">Austria</option>
<option value="AZE">Azerbaijan</option>
<option value="BHS">Bahamas</option>
<option value="BHR">Bahrain</option>
<option value="BGD">Bangladesh</option>
<option value="BRB">Barbados</option>
<option value="BLR">Belarus</option>
<option value="BEL">Belgium</option>
<option value="BLZ">Belize</option>
<option value="BEN">Benin</option>
<option value="BMU">Bermuda</option>
<option value="BTN">Bhutan</option>
<option value="BOL">Bolivia, Plurinational State of</option>
<option value="BES">Bonaire, Sint Eustatius and Saba</option>
<option value="BIH">Bosnia and Herzegovina</option>
<option value="BWA">Botswana</option>
<option value="BVT">Bouvet Island</option>
<option value="BRA">Brazil</option>
<option value="IOT">British Indian Ocean Territory</option>
<option value="BRN">Brunei Darussalam</option>
<option value="BGR">Bulgaria</option>
<option value="BFA">Burkina Faso</option>
<option value="BDI">Burundi</option>
<option value="KHM">Cambodia</option>
<option value="CMR">Cameroon</option>
<option value="CAN">Canada</option>
<option value="CPV">Cape Verde</option>
<option value="CYM">Cayman Islands</option>
<option value="CAF">Central African Republic</option>
<option value="TCD">Chad</option>
<option value="CHL">Chile</option>
<option value="CHN">China</option>
<option value="CXR">Christmas Island</option>
<option value="CCK">Cocos (Keeling) Islands</option>
<option value="COL">Colombia</option>
<option value="COM">Comoros</option>
<option value="COG">Congo</option>
<option value="COD">Congo, the Democratic Republic of the</option>
<option value="COK">Cook Islands</option>
<option value="CRI">Costa Rica</option>
<option value="CIV">Côte d'Ivoire</option>
<option value="HRV">Croatia</option>
<option value="CUB">Cuba</option>
<option value="CUW">Curaçao</option>
<option value="CYP">Cyprus</option>
<option value="CZE">Czech Republic</option>
<option value="DNK">Denmark</option>
<option value="DJI">Djibouti</option>
<option value="DMA">Dominica</option>
<option value="DOM">Dominican Republic</option>
<option value="ECU">Ecuador</option>
<option value="EGY">Egypt</option>
<option value="SLV">El Salvador</option>
<option value="GNQ">Equatorial Guinea</option>
<option value="ERI">Eritrea</option>
<option value="EST">Estonia</option>
<option value="ETH">Ethiopia</option>
<option value="FLK">Falkland Islands (Malvinas)</option>
<option value="FRO">Faroe Islands</option>
<option value="FJI">Fiji</option>
<option value="FIN">Finland</option>
<option value="FRA">France</option>
<option value="GUF">French Guiana</option>
<option value="PYF">French Polynesia</option>
<option value="ATF">French Southern Territories</option>
<option value="GAB">Gabon</option>
<option value="GMB">Gambia</option>
<option value="GEO">Georgia</option>
<option value="DEU">Germany</option>
<option value="GHA">Ghana</option>
<option value="GIB">Gibraltar</option>
<option value="GRC">Greece</option>
<option value="GRL">Greenland</option>
<option value="GRD">Grenada</option>
<option value="GLP">Guadeloupe</option>
<option value="GUM">Guam</option>
<option value="GTM">Guatemala</option>
<option value="GGY">Guernsey</option>
<option value="GIN">Guinea</option>
<option value="GNB">Guinea-Bissau</option>
<option value="GUY">Guyana</option>
<option value="HTI">Haiti</option>
<option value="HMD">Heard Island and McDonald Islands</option>
<option value="VAT">Holy See (Vatican City State)</option>
<option value="HND">Honduras</option>
<option value="HKG">Hong Kong</option>
<option value="HUN">Hungary</option>
<option value="ISL">Iceland</option>
<option value="IND">India</option>
<option value="IDN">Indonesia</option>
<option value="IRN">Iran, Islamic Republic of</option>
<option value="IRQ">Iraq</option>
<option value="IRL">Ireland</option>
<option value="IMN">Isle of Man</option>
<option value="ISR">Israel</option>
<option value="ITA">Italy</option>
<option value="JAM">Jamaica</option>
<option value="JPN">Japan</option>
<option value="JEY">Jersey</option>
<option value="JOR">Jordan</option>
<option value="KAZ">Kazakhstan</option>
<option value="KEN">Kenya</option>
<option value="KIR">Kiribati</option>
<option value="PRK">Korea, Democratic People's Republic of</option>
<option value="KOR">Korea, Republic of</option>
<option value="KWT">Kuwait</option>
<option value="KGZ">Kyrgyzstan</option>
<option value="LAO">Lao People's Democratic Republic</option>
<option value="LVA">Latvia</option>
<option value="LBN">Lebanon</option>
<option value="LSO">Lesotho</option>
<option value="LBR">Liberia</option>
<option value="LBY">Libya</option>
<option value="LIE">Liechtenstein</option>
<option value="LTU">Lithuania</option>
<option value="LUX">Luxembourg</option>
<option value="MAC">Macao</option>
<option value="MKD">Macedonia, the former Yugoslav Republic of</option>
<option value="MDG">Madagascar</option>
<option value="MWI">Malawi</option>
<option value="MYS">Malaysia</option>
<option value="MDV">Maldives</option>
<option value="MLI">Mali</option>
<option value="MLT">Malta</option>
<option value="MHL">Marshall Islands</option>
<option value="MTQ">Martinique</option>
<option value="MRT">Mauritania</option>
<option value="MUS">Mauritius</option>
<option value="MYT">Mayotte</option>
<option value="MEX">Mexico</option>
<option value="FSM">Micronesia, Federated States of</option>
<option value="MDA">Moldova, Republic of</option>
<option value="MCO">Monaco</option>
<option value="MNG">Mongolia</option>
<option value="MNE">Montenegro</option>
<option value="MSR">Montserrat</option>
<option value="MAR">Morocco</option>
<option value="MOZ">Mozambique</option>
<option value="MMR">Myanmar</option>
<option value="NAM">Namibia</option>
<option value="NRU">Nauru</option>
<option value="NPL">Nepal</option>
<option value="NLD">Netherlands</option>
<option value="NCL">New Caledonia</option>
<option value="NZL">New Zealand</option>
<option value="NIC">Nicaragua</option>
<option value="NER">Niger</option>
<option value="NGA">Nigeria</option>
<option value="NIU">Niue</option>
<option value="NFK">Norfolk Island</option>
<option value="MNP">Northern Mariana Islands</option>
<option value="NOR">Norway</option>
<option value="OMN">Oman</option>
<option value="PAK">Pakistan</option>
<option value="PLW">Palau</option>
<option value="PSE">Palestinian Territory, Occupied</option>
<option value="PAN">Panama</option>
<option value="PNG">Papua New Guinea</option>
<option value="PRY">Paraguay</option>
<option value="PER">Peru</option>
<option value="PHL">Philippines</option>
<option value="PCN">Pitcairn</option>
<option value="POL">Poland</option>
<option value="PRT">Portugal</option>
<option value="PRI">Puerto Rico</option>
<option value="QAT">Qatar</option>
<option value="REU">Réunion</option>
<option value="ROU">Romania</option>
<option value="RUS">Russian Federation</option>
<option value="RWA">Rwanda</option>
<option value="BLM">Saint Barthélemy</option>
<option value="SHN">Saint Helena, Ascension and Tristan da Cunha</option>
<option value="KNA">Saint Kitts and Nevis</option>
<option value="LCA">Saint Lucia</option>
<option value="MAF">Saint Martin (French part)</option>
<option value="SPM">Saint Pierre and Miquelon</option>
<option value="VCT">Saint Vincent and the Grenadines</option>
<option value="WSM">Samoa</option>
<option value="SMR">San Marino</option>
<option value="STP">Sao Tome and Principe</option>
<option value="SAU">Saudi Arabia</option>
<option value="SEN">Senegal</option>
<option value="SRB">Serbia</option>
<option value="SYC">Seychelles</option>
<option value="SLE">Sierra Leone</option>
<option value="SGP">Singapore</option>
<option value="SXM">Sint Maarten (Dutch part)</option>
<option value="SVK">Slovakia</option>
<option value="SVN">Slovenia</option>
<option value="SLB">Solomon Islands</option>
<option value="SOM">Somalia</option>
<option value="ZAF">South Africa</option>
<option value="SGS">South Georgia and the South Sandwich Islands</option>
<option value="SSD">South Sudan</option>
<option value="ESP">Spain</option>
<option value="LKA">Sri Lanka</option>
<option value="SDN">Sudan</option>
<option value="SUR">Suriname</option>
<option value="SJM">Svalbard and Jan Mayen</option>
<option value="SWZ">Swaziland</option>
<option value="SWE">Sweden</option>
<option value="CHE">Switzerland</option>
<option value="SYR">Syrian Arab Republic</option>
<option value="TWN">Taiwan, Province of China</option>
<option value="TJK">Tajikistan</option>
<option value="TZA">Tanzania, United Republic of</option>
<option value="THA">Thailand</option>
<option value="TLS">Timor-Leste</option>
<option value="TGO">Togo</option>
<option value="TKL">Tokelau</option>
<option value="TON">Tonga</option>
<option value="TTO">Trinidad and Tobago</option>
<option value="TUN">Tunisia</option>
<option value="TUR">Turkey</option>
<option value="TKM">Turkmenistan</option>
<option value="TCA">Turks and Caicos Islands</option>
<option value="TUV">Tuvalu</option>
<option value="UGA">Uganda</option>
<option value="UKR">Ukraine</option>
<option value="ARE">United Arab Emirates</option>
<option value="GBR">United Kingdom</option>
<option value="USA">United States</option>
<option value="UMI">United States Minor Outlying Islands</option>
<option value="URY">Uruguay</option>
<option value="UZB">Uzbekistan</option>
<option value="VUT">Vanuatu</option>
<option value="VEN">Venezuela, Bolivarian Republic of</option>
<option value="VNM">Viet Nam</option>
<option value="VGB">Virgin Islands, British</option>
<option value="VIR">Virgin Islands, U.S.</option>
<option value="WLF">Wallis and Futuna</option>
<option value="ESH">Western Sahara</option>
<option value="YEM">Yemen</option>
<option value="ZMB">Zambia</option>
<option value="ZWE">Zimbabwe</option>
</select>

</td>
</tr>

<tr>
<th> Email Id : </th>
<td> <input type="text" name="email_id"/></td>
</tr>

<tr>
<th> Mobile Number : </th>
<td><input type="text" name="mobile_number"/></td>
</tr>

<tr>
<th>I have : </th>
<td>
<select id="old" name="i_have">
<option value = "select_option">Select Option</option>
<option value = "three_compact">3 Compact</option>
<option value = "three_regular">3 Regular</option>
<option value = "three_triple">3 Triple</option>
<option value = "five_compact">5 Compact</option>
<option value = "five_regular">5 Regular</option>
<option value = "five_triple">5 Triple</option>
<option value = "seven_compact">7 Compact</option>
<option value = "seven_regular">7 Regular</option>
<option value = "seven_triple">7 Triple</option>
<option value = "nine_compact">9 Compact</option>
<option value = "nine_regular">9 Regular</option>
<option value = "nine_triple">9 Triple</option>
</select>
</td>
</tr>

<tr>
<th>I want : </th>
<td>
<select id="new" name="i_want">
<option value = "select_option">Select Option</option>
<option value = "three_compact">3 Compact</option>
<option value = "three_regular">3 Regular</option>
<option value = "three_triple">3 Triple</option>
<option value = "five_compact">5 Compact</option>
<option value = "five_regular">5 Regular</option>
<option value = "five_triple">5 Triple</option>
<option value = "seven_compact">7 Compact</option>
<option value = "seven_regular">7 Regular</option>
<option value = "seven_triple">7 Triple</option>
<option value = "nine_compact">9 Compact</option>
<option value = "nine_regular">9 Regular</option>
<option value = "nine_triple">9 Triple</option>
</select>
</td>
</tr>

<tr>
<th>Price (Rs) : </th>
<td id="result"></td>
<input type="hidden" name="price" id="price"/>
</tr>

<tr>
<th>Shipping Weight : </th>
<td id="show"></td>
<input type="hidden" name="shipping_weight" id="shipping_weight"/>
</tr>
<tr>
<th>Shipping Cost (Rs) : </th>
<td id="findata"></td>
<input type="hidden" name="shipping_cost" id="shipping_cost"/>
</tr>
<tr>
<th> Grand Total: </th>
<td> <input type="text" name="grand_total"/></td>
</tr>

</table> <br/>

<!-- <b><div id="result" name="price"></div></b> -->

<input type="Submit" value="Pay" name="pay"/>

</form>
</body>

<!-- <button id="btn_check_value">Check for value</button> -->

<!-- Ajax Script -->

<!-- Ajax Price -->

<script>
$(document).ready(function(){
$('#new').on('change',function(){

var old_val = $("#old option:selected").val();
var new_val = $("#new option:selected").val();

$.ajax({
type: "POST",
url: "ajax_data.php",
dataType: "text",
data: { old: old_val, new: new_val},
success: function(data)
{
// Check the output of ajax call on firebug console
// console.log(data);
$('#result').html(data);
$('#price').val(data);

}
});

});
});

</script>

<!-- Ajax shipping weight -->

<script>
$(document).ready(function(){
$('#new').on('change',function(){

var old_val = $("#old option:selected").val();
var new_val = $("#new option:selected").val();

$.ajax({
type: "POST",
url: "ajax_ship_weight.php",
dataType: "text",
data: { old: old_val, new: new_val},
success: function(data)
{
// Check the output of ajax call on firebug console
//console.log(data);
$('#show').html(data);
$('#shipping_weight').val(data);

}
});

});
});
</script>

<!-- Ajax shipping cost -->

<script>
$(document).ready(function(){
$('#new').on('change',function(){

var old_val = $("#old option:selected").val();
var new_val = $("#new option:selected").val();

$.ajax({
type: "POST",
// url: "ajax_ship_cost_data.php",
url: "sp_cost.php",
dataType: "text",
data: { old: old_val, new: new_val},
success: function(data)
{
// Check the output of ajax call on firebug console
//console.log(data);
$('#findata').html(data);
$('#shipping_cost').val(data);

}
});

});
});
</script>
</html>


PHP :



<?php
ob_start();
require_once('./../app/Mage.php');
umask(0);
ini_set('display_errors',true);
ini_set('memory_limit', '1024M');
Mage::app()->loadArea('frontend');

function getShippingEstimate($productId,$productQty,$countryId,$postcode ) {

$quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore('english')->getId());
$_product = Mage::getModel('catalog/product')->load($productId);

$_product->getStockItem()->setUseConfigManageStock(false);
$_product->getStockItem()->setManageStock(false);

$quote->addProduct($_product, $productQty);
$quote->getShippingAddress()->setCountryId($countryId)->setPostcode($postcode);
$quote->getShippingAddress()->collectTotals();
$quote->getShippingAddress()->setCollectShippingRates(true);
$quote->getShippingAddress()->collectShippingRates();

$_rates = $quote->getShippingAddress()->getShippingRatesCollection();

$shippingRates = array();
foreach ($_rates as $_rate):
if($_rate->getPrice() > 0) {
$shippingRates = array("Title" => $_rate->getMethodTitle(), "Price" => $_rate->getPrice());
}
endforeach;

return $shippingRates;

}

###### Post #######
// if(isset($_POST['zip_postal_code']) && isset($_POST['country']))
// {
// $zip_postal_code = $_POST['zip_postal_code'];
// $country = $_POST['country'];

// }


//$results = getShippingEstimate('14419','1',"$zip_postal_code","$country");

###### Post #######

$results = getShippingEstimate('14419','1',"IN","642001"); // Predefined Value

$count = -1;
echo "<select>";
foreach ($results as $result):
$count++;
?>
<option value="<?php echo $count; ?>"> <?php echo $result["Title"]." - Rs ".$result["Price"]?>
</option>

<?php
endforeach;
echo "</select>";
?>


Expected output :



Get input value Zip-code and Country from form and passing values to php function using Ajax and return my shipping cost.



Workout output:



using predefined value,



$results = getShippingEstimate('14419','1',"IN","642001"); // Predefined Value


enter image description here



Try with $_POST,



 if(isset($_POST['zip_postal_code']) && isset($_POST['country']))
{
$zip_postal_code = $_POST['zip_postal_code'];
$country = $_POST['country'];

}


$results = getShippingEstimate('14419','1',"$zip_postal_code","$country");


enter image description here



Code help thanks to Stack Overflow









share































    1















    I have PHP function for getting shipping cost form magento based on productId, productQty, countryId, postcode. Here my productId, productQty are static and countryId, postcode is dynamic based on user entry,



    if i try with predefined value,



    $results = getShippingEstimate('14419','1',"IN","642001"); // Predefined Value


    Return my shipping cost as per based on magento using "IN","642001" (Country &Zipcode)



    //if i try with PHP function,

    if(isset($_POST['zip_postal_code']) && isset($_POST['country']))
    {
    $zip_postal_code = $_POST['zip_postal_code'];
    $country = $_POST['country'];

    }


    $results = getShippingEstimate('14419','1',"$zip_postal_code","$country");


    Not working.



    NOTE : Here my shipping cost field as Auto response,



    HTML FORM :



    <html>
    <head>
    <title> Upgrade Cost</title>

    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

    </head>
    <body>
    <!-- Ajax data -->
    <h2>KoluPadi Upgrade Form</h2>
    <form action='ajax_data.php' method='POST' >

    <table border="0" cellspacing="1" cellpadding="2">
    <tr>
    <th> First Name : </th>
    <td> <input type="text" name="first_name"/> </td>
    </tr>
    <tr>
    <th> Last Name : </th>
    <td> <input type="text" name="last_name"/> </td>
    </tr>

    <tr>
    <th> Address : </th>
    <td> <input type="text" name="address"/> </td>
    </tr>

    <tr>
    <th> City : </th>
    <td> <input type="text" name="city"/> </td>
    </tr>

    <tr>
    <th> State/Province : </th>
    <td> <input type="text" name="state_province"/> </td>
    </tr>

    <tr>
    <th> Zip/Postal Code : </th>
    <td> <input type="text" name="zip_postal_code"/> </td>
    </tr>

    <tr>
    <th> Country : </th>
    <td>
    <select name="country" style="width: 173px;">
    <option value="AFG">Afghanistan</option>
    <option value="ALA">Åland Islands</option>
    <option value="ALB">Albania</option>
    <option value="DZA">Algeria</option>
    <option value="ASM">American Samoa</option>
    <option value="AND">Andorra</option>
    <option value="AGO">Angola</option>
    <option value="AIA">Anguilla</option>
    <option value="ATA">Antarctica</option>
    <option value="ATG">Antigua and Barbuda</option>
    <option value="ARG">Argentina</option>
    <option value="ARM">Armenia</option>
    <option value="ABW">Aruba</option>
    <option value="AUS">Australia</option>
    <option value="AUT">Austria</option>
    <option value="AZE">Azerbaijan</option>
    <option value="BHS">Bahamas</option>
    <option value="BHR">Bahrain</option>
    <option value="BGD">Bangladesh</option>
    <option value="BRB">Barbados</option>
    <option value="BLR">Belarus</option>
    <option value="BEL">Belgium</option>
    <option value="BLZ">Belize</option>
    <option value="BEN">Benin</option>
    <option value="BMU">Bermuda</option>
    <option value="BTN">Bhutan</option>
    <option value="BOL">Bolivia, Plurinational State of</option>
    <option value="BES">Bonaire, Sint Eustatius and Saba</option>
    <option value="BIH">Bosnia and Herzegovina</option>
    <option value="BWA">Botswana</option>
    <option value="BVT">Bouvet Island</option>
    <option value="BRA">Brazil</option>
    <option value="IOT">British Indian Ocean Territory</option>
    <option value="BRN">Brunei Darussalam</option>
    <option value="BGR">Bulgaria</option>
    <option value="BFA">Burkina Faso</option>
    <option value="BDI">Burundi</option>
    <option value="KHM">Cambodia</option>
    <option value="CMR">Cameroon</option>
    <option value="CAN">Canada</option>
    <option value="CPV">Cape Verde</option>
    <option value="CYM">Cayman Islands</option>
    <option value="CAF">Central African Republic</option>
    <option value="TCD">Chad</option>
    <option value="CHL">Chile</option>
    <option value="CHN">China</option>
    <option value="CXR">Christmas Island</option>
    <option value="CCK">Cocos (Keeling) Islands</option>
    <option value="COL">Colombia</option>
    <option value="COM">Comoros</option>
    <option value="COG">Congo</option>
    <option value="COD">Congo, the Democratic Republic of the</option>
    <option value="COK">Cook Islands</option>
    <option value="CRI">Costa Rica</option>
    <option value="CIV">Côte d'Ivoire</option>
    <option value="HRV">Croatia</option>
    <option value="CUB">Cuba</option>
    <option value="CUW">Curaçao</option>
    <option value="CYP">Cyprus</option>
    <option value="CZE">Czech Republic</option>
    <option value="DNK">Denmark</option>
    <option value="DJI">Djibouti</option>
    <option value="DMA">Dominica</option>
    <option value="DOM">Dominican Republic</option>
    <option value="ECU">Ecuador</option>
    <option value="EGY">Egypt</option>
    <option value="SLV">El Salvador</option>
    <option value="GNQ">Equatorial Guinea</option>
    <option value="ERI">Eritrea</option>
    <option value="EST">Estonia</option>
    <option value="ETH">Ethiopia</option>
    <option value="FLK">Falkland Islands (Malvinas)</option>
    <option value="FRO">Faroe Islands</option>
    <option value="FJI">Fiji</option>
    <option value="FIN">Finland</option>
    <option value="FRA">France</option>
    <option value="GUF">French Guiana</option>
    <option value="PYF">French Polynesia</option>
    <option value="ATF">French Southern Territories</option>
    <option value="GAB">Gabon</option>
    <option value="GMB">Gambia</option>
    <option value="GEO">Georgia</option>
    <option value="DEU">Germany</option>
    <option value="GHA">Ghana</option>
    <option value="GIB">Gibraltar</option>
    <option value="GRC">Greece</option>
    <option value="GRL">Greenland</option>
    <option value="GRD">Grenada</option>
    <option value="GLP">Guadeloupe</option>
    <option value="GUM">Guam</option>
    <option value="GTM">Guatemala</option>
    <option value="GGY">Guernsey</option>
    <option value="GIN">Guinea</option>
    <option value="GNB">Guinea-Bissau</option>
    <option value="GUY">Guyana</option>
    <option value="HTI">Haiti</option>
    <option value="HMD">Heard Island and McDonald Islands</option>
    <option value="VAT">Holy See (Vatican City State)</option>
    <option value="HND">Honduras</option>
    <option value="HKG">Hong Kong</option>
    <option value="HUN">Hungary</option>
    <option value="ISL">Iceland</option>
    <option value="IND">India</option>
    <option value="IDN">Indonesia</option>
    <option value="IRN">Iran, Islamic Republic of</option>
    <option value="IRQ">Iraq</option>
    <option value="IRL">Ireland</option>
    <option value="IMN">Isle of Man</option>
    <option value="ISR">Israel</option>
    <option value="ITA">Italy</option>
    <option value="JAM">Jamaica</option>
    <option value="JPN">Japan</option>
    <option value="JEY">Jersey</option>
    <option value="JOR">Jordan</option>
    <option value="KAZ">Kazakhstan</option>
    <option value="KEN">Kenya</option>
    <option value="KIR">Kiribati</option>
    <option value="PRK">Korea, Democratic People's Republic of</option>
    <option value="KOR">Korea, Republic of</option>
    <option value="KWT">Kuwait</option>
    <option value="KGZ">Kyrgyzstan</option>
    <option value="LAO">Lao People's Democratic Republic</option>
    <option value="LVA">Latvia</option>
    <option value="LBN">Lebanon</option>
    <option value="LSO">Lesotho</option>
    <option value="LBR">Liberia</option>
    <option value="LBY">Libya</option>
    <option value="LIE">Liechtenstein</option>
    <option value="LTU">Lithuania</option>
    <option value="LUX">Luxembourg</option>
    <option value="MAC">Macao</option>
    <option value="MKD">Macedonia, the former Yugoslav Republic of</option>
    <option value="MDG">Madagascar</option>
    <option value="MWI">Malawi</option>
    <option value="MYS">Malaysia</option>
    <option value="MDV">Maldives</option>
    <option value="MLI">Mali</option>
    <option value="MLT">Malta</option>
    <option value="MHL">Marshall Islands</option>
    <option value="MTQ">Martinique</option>
    <option value="MRT">Mauritania</option>
    <option value="MUS">Mauritius</option>
    <option value="MYT">Mayotte</option>
    <option value="MEX">Mexico</option>
    <option value="FSM">Micronesia, Federated States of</option>
    <option value="MDA">Moldova, Republic of</option>
    <option value="MCO">Monaco</option>
    <option value="MNG">Mongolia</option>
    <option value="MNE">Montenegro</option>
    <option value="MSR">Montserrat</option>
    <option value="MAR">Morocco</option>
    <option value="MOZ">Mozambique</option>
    <option value="MMR">Myanmar</option>
    <option value="NAM">Namibia</option>
    <option value="NRU">Nauru</option>
    <option value="NPL">Nepal</option>
    <option value="NLD">Netherlands</option>
    <option value="NCL">New Caledonia</option>
    <option value="NZL">New Zealand</option>
    <option value="NIC">Nicaragua</option>
    <option value="NER">Niger</option>
    <option value="NGA">Nigeria</option>
    <option value="NIU">Niue</option>
    <option value="NFK">Norfolk Island</option>
    <option value="MNP">Northern Mariana Islands</option>
    <option value="NOR">Norway</option>
    <option value="OMN">Oman</option>
    <option value="PAK">Pakistan</option>
    <option value="PLW">Palau</option>
    <option value="PSE">Palestinian Territory, Occupied</option>
    <option value="PAN">Panama</option>
    <option value="PNG">Papua New Guinea</option>
    <option value="PRY">Paraguay</option>
    <option value="PER">Peru</option>
    <option value="PHL">Philippines</option>
    <option value="PCN">Pitcairn</option>
    <option value="POL">Poland</option>
    <option value="PRT">Portugal</option>
    <option value="PRI">Puerto Rico</option>
    <option value="QAT">Qatar</option>
    <option value="REU">Réunion</option>
    <option value="ROU">Romania</option>
    <option value="RUS">Russian Federation</option>
    <option value="RWA">Rwanda</option>
    <option value="BLM">Saint Barthélemy</option>
    <option value="SHN">Saint Helena, Ascension and Tristan da Cunha</option>
    <option value="KNA">Saint Kitts and Nevis</option>
    <option value="LCA">Saint Lucia</option>
    <option value="MAF">Saint Martin (French part)</option>
    <option value="SPM">Saint Pierre and Miquelon</option>
    <option value="VCT">Saint Vincent and the Grenadines</option>
    <option value="WSM">Samoa</option>
    <option value="SMR">San Marino</option>
    <option value="STP">Sao Tome and Principe</option>
    <option value="SAU">Saudi Arabia</option>
    <option value="SEN">Senegal</option>
    <option value="SRB">Serbia</option>
    <option value="SYC">Seychelles</option>
    <option value="SLE">Sierra Leone</option>
    <option value="SGP">Singapore</option>
    <option value="SXM">Sint Maarten (Dutch part)</option>
    <option value="SVK">Slovakia</option>
    <option value="SVN">Slovenia</option>
    <option value="SLB">Solomon Islands</option>
    <option value="SOM">Somalia</option>
    <option value="ZAF">South Africa</option>
    <option value="SGS">South Georgia and the South Sandwich Islands</option>
    <option value="SSD">South Sudan</option>
    <option value="ESP">Spain</option>
    <option value="LKA">Sri Lanka</option>
    <option value="SDN">Sudan</option>
    <option value="SUR">Suriname</option>
    <option value="SJM">Svalbard and Jan Mayen</option>
    <option value="SWZ">Swaziland</option>
    <option value="SWE">Sweden</option>
    <option value="CHE">Switzerland</option>
    <option value="SYR">Syrian Arab Republic</option>
    <option value="TWN">Taiwan, Province of China</option>
    <option value="TJK">Tajikistan</option>
    <option value="TZA">Tanzania, United Republic of</option>
    <option value="THA">Thailand</option>
    <option value="TLS">Timor-Leste</option>
    <option value="TGO">Togo</option>
    <option value="TKL">Tokelau</option>
    <option value="TON">Tonga</option>
    <option value="TTO">Trinidad and Tobago</option>
    <option value="TUN">Tunisia</option>
    <option value="TUR">Turkey</option>
    <option value="TKM">Turkmenistan</option>
    <option value="TCA">Turks and Caicos Islands</option>
    <option value="TUV">Tuvalu</option>
    <option value="UGA">Uganda</option>
    <option value="UKR">Ukraine</option>
    <option value="ARE">United Arab Emirates</option>
    <option value="GBR">United Kingdom</option>
    <option value="USA">United States</option>
    <option value="UMI">United States Minor Outlying Islands</option>
    <option value="URY">Uruguay</option>
    <option value="UZB">Uzbekistan</option>
    <option value="VUT">Vanuatu</option>
    <option value="VEN">Venezuela, Bolivarian Republic of</option>
    <option value="VNM">Viet Nam</option>
    <option value="VGB">Virgin Islands, British</option>
    <option value="VIR">Virgin Islands, U.S.</option>
    <option value="WLF">Wallis and Futuna</option>
    <option value="ESH">Western Sahara</option>
    <option value="YEM">Yemen</option>
    <option value="ZMB">Zambia</option>
    <option value="ZWE">Zimbabwe</option>
    </select>

    </td>
    </tr>

    <tr>
    <th> Email Id : </th>
    <td> <input type="text" name="email_id"/></td>
    </tr>

    <tr>
    <th> Mobile Number : </th>
    <td><input type="text" name="mobile_number"/></td>
    </tr>

    <tr>
    <th>I have : </th>
    <td>
    <select id="old" name="i_have">
    <option value = "select_option">Select Option</option>
    <option value = "three_compact">3 Compact</option>
    <option value = "three_regular">3 Regular</option>
    <option value = "three_triple">3 Triple</option>
    <option value = "five_compact">5 Compact</option>
    <option value = "five_regular">5 Regular</option>
    <option value = "five_triple">5 Triple</option>
    <option value = "seven_compact">7 Compact</option>
    <option value = "seven_regular">7 Regular</option>
    <option value = "seven_triple">7 Triple</option>
    <option value = "nine_compact">9 Compact</option>
    <option value = "nine_regular">9 Regular</option>
    <option value = "nine_triple">9 Triple</option>
    </select>
    </td>
    </tr>

    <tr>
    <th>I want : </th>
    <td>
    <select id="new" name="i_want">
    <option value = "select_option">Select Option</option>
    <option value = "three_compact">3 Compact</option>
    <option value = "three_regular">3 Regular</option>
    <option value = "three_triple">3 Triple</option>
    <option value = "five_compact">5 Compact</option>
    <option value = "five_regular">5 Regular</option>
    <option value = "five_triple">5 Triple</option>
    <option value = "seven_compact">7 Compact</option>
    <option value = "seven_regular">7 Regular</option>
    <option value = "seven_triple">7 Triple</option>
    <option value = "nine_compact">9 Compact</option>
    <option value = "nine_regular">9 Regular</option>
    <option value = "nine_triple">9 Triple</option>
    </select>
    </td>
    </tr>

    <tr>
    <th>Price (Rs) : </th>
    <td id="result"></td>
    <input type="hidden" name="price" id="price"/>
    </tr>

    <tr>
    <th>Shipping Weight : </th>
    <td id="show"></td>
    <input type="hidden" name="shipping_weight" id="shipping_weight"/>
    </tr>
    <tr>
    <th>Shipping Cost (Rs) : </th>
    <td id="findata"></td>
    <input type="hidden" name="shipping_cost" id="shipping_cost"/>
    </tr>
    <tr>
    <th> Grand Total: </th>
    <td> <input type="text" name="grand_total"/></td>
    </tr>

    </table> <br/>

    <!-- <b><div id="result" name="price"></div></b> -->

    <input type="Submit" value="Pay" name="pay"/>

    </form>
    </body>

    <!-- <button id="btn_check_value">Check for value</button> -->

    <!-- Ajax Script -->

    <!-- Ajax Price -->

    <script>
    $(document).ready(function(){
    $('#new').on('change',function(){

    var old_val = $("#old option:selected").val();
    var new_val = $("#new option:selected").val();

    $.ajax({
    type: "POST",
    url: "ajax_data.php",
    dataType: "text",
    data: { old: old_val, new: new_val},
    success: function(data)
    {
    // Check the output of ajax call on firebug console
    // console.log(data);
    $('#result').html(data);
    $('#price').val(data);

    }
    });

    });
    });

    </script>

    <!-- Ajax shipping weight -->

    <script>
    $(document).ready(function(){
    $('#new').on('change',function(){

    var old_val = $("#old option:selected").val();
    var new_val = $("#new option:selected").val();

    $.ajax({
    type: "POST",
    url: "ajax_ship_weight.php",
    dataType: "text",
    data: { old: old_val, new: new_val},
    success: function(data)
    {
    // Check the output of ajax call on firebug console
    //console.log(data);
    $('#show').html(data);
    $('#shipping_weight').val(data);

    }
    });

    });
    });
    </script>

    <!-- Ajax shipping cost -->

    <script>
    $(document).ready(function(){
    $('#new').on('change',function(){

    var old_val = $("#old option:selected").val();
    var new_val = $("#new option:selected").val();

    $.ajax({
    type: "POST",
    // url: "ajax_ship_cost_data.php",
    url: "sp_cost.php",
    dataType: "text",
    data: { old: old_val, new: new_val},
    success: function(data)
    {
    // Check the output of ajax call on firebug console
    //console.log(data);
    $('#findata').html(data);
    $('#shipping_cost').val(data);

    }
    });

    });
    });
    </script>
    </html>


    PHP :



    <?php
    ob_start();
    require_once('./../app/Mage.php');
    umask(0);
    ini_set('display_errors',true);
    ini_set('memory_limit', '1024M');
    Mage::app()->loadArea('frontend');

    function getShippingEstimate($productId,$productQty,$countryId,$postcode ) {

    $quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore('english')->getId());
    $_product = Mage::getModel('catalog/product')->load($productId);

    $_product->getStockItem()->setUseConfigManageStock(false);
    $_product->getStockItem()->setManageStock(false);

    $quote->addProduct($_product, $productQty);
    $quote->getShippingAddress()->setCountryId($countryId)->setPostcode($postcode);
    $quote->getShippingAddress()->collectTotals();
    $quote->getShippingAddress()->setCollectShippingRates(true);
    $quote->getShippingAddress()->collectShippingRates();

    $_rates = $quote->getShippingAddress()->getShippingRatesCollection();

    $shippingRates = array();
    foreach ($_rates as $_rate):
    if($_rate->getPrice() > 0) {
    $shippingRates = array("Title" => $_rate->getMethodTitle(), "Price" => $_rate->getPrice());
    }
    endforeach;

    return $shippingRates;

    }

    ###### Post #######
    // if(isset($_POST['zip_postal_code']) && isset($_POST['country']))
    // {
    // $zip_postal_code = $_POST['zip_postal_code'];
    // $country = $_POST['country'];

    // }


    //$results = getShippingEstimate('14419','1',"$zip_postal_code","$country");

    ###### Post #######

    $results = getShippingEstimate('14419','1',"IN","642001"); // Predefined Value

    $count = -1;
    echo "<select>";
    foreach ($results as $result):
    $count++;
    ?>
    <option value="<?php echo $count; ?>"> <?php echo $result["Title"]." - Rs ".$result["Price"]?>
    </option>

    <?php
    endforeach;
    echo "</select>";
    ?>


    Expected output :



    Get input value Zip-code and Country from form and passing values to php function using Ajax and return my shipping cost.



    Workout output:



    using predefined value,



    $results = getShippingEstimate('14419','1',"IN","642001"); // Predefined Value


    enter image description here



    Try with $_POST,



     if(isset($_POST['zip_postal_code']) && isset($_POST['country']))
    {
    $zip_postal_code = $_POST['zip_postal_code'];
    $country = $_POST['country'];

    }


    $results = getShippingEstimate('14419','1',"$zip_postal_code","$country");


    enter image description here



    Code help thanks to Stack Overflow









    share



























      1












      1








      1








      I have PHP function for getting shipping cost form magento based on productId, productQty, countryId, postcode. Here my productId, productQty are static and countryId, postcode is dynamic based on user entry,



      if i try with predefined value,



      $results = getShippingEstimate('14419','1',"IN","642001"); // Predefined Value


      Return my shipping cost as per based on magento using "IN","642001" (Country &Zipcode)



      //if i try with PHP function,

      if(isset($_POST['zip_postal_code']) && isset($_POST['country']))
      {
      $zip_postal_code = $_POST['zip_postal_code'];
      $country = $_POST['country'];

      }


      $results = getShippingEstimate('14419','1',"$zip_postal_code","$country");


      Not working.



      NOTE : Here my shipping cost field as Auto response,



      HTML FORM :



      <html>
      <head>
      <title> Upgrade Cost</title>

      <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

      </head>
      <body>
      <!-- Ajax data -->
      <h2>KoluPadi Upgrade Form</h2>
      <form action='ajax_data.php' method='POST' >

      <table border="0" cellspacing="1" cellpadding="2">
      <tr>
      <th> First Name : </th>
      <td> <input type="text" name="first_name"/> </td>
      </tr>
      <tr>
      <th> Last Name : </th>
      <td> <input type="text" name="last_name"/> </td>
      </tr>

      <tr>
      <th> Address : </th>
      <td> <input type="text" name="address"/> </td>
      </tr>

      <tr>
      <th> City : </th>
      <td> <input type="text" name="city"/> </td>
      </tr>

      <tr>
      <th> State/Province : </th>
      <td> <input type="text" name="state_province"/> </td>
      </tr>

      <tr>
      <th> Zip/Postal Code : </th>
      <td> <input type="text" name="zip_postal_code"/> </td>
      </tr>

      <tr>
      <th> Country : </th>
      <td>
      <select name="country" style="width: 173px;">
      <option value="AFG">Afghanistan</option>
      <option value="ALA">Åland Islands</option>
      <option value="ALB">Albania</option>
      <option value="DZA">Algeria</option>
      <option value="ASM">American Samoa</option>
      <option value="AND">Andorra</option>
      <option value="AGO">Angola</option>
      <option value="AIA">Anguilla</option>
      <option value="ATA">Antarctica</option>
      <option value="ATG">Antigua and Barbuda</option>
      <option value="ARG">Argentina</option>
      <option value="ARM">Armenia</option>
      <option value="ABW">Aruba</option>
      <option value="AUS">Australia</option>
      <option value="AUT">Austria</option>
      <option value="AZE">Azerbaijan</option>
      <option value="BHS">Bahamas</option>
      <option value="BHR">Bahrain</option>
      <option value="BGD">Bangladesh</option>
      <option value="BRB">Barbados</option>
      <option value="BLR">Belarus</option>
      <option value="BEL">Belgium</option>
      <option value="BLZ">Belize</option>
      <option value="BEN">Benin</option>
      <option value="BMU">Bermuda</option>
      <option value="BTN">Bhutan</option>
      <option value="BOL">Bolivia, Plurinational State of</option>
      <option value="BES">Bonaire, Sint Eustatius and Saba</option>
      <option value="BIH">Bosnia and Herzegovina</option>
      <option value="BWA">Botswana</option>
      <option value="BVT">Bouvet Island</option>
      <option value="BRA">Brazil</option>
      <option value="IOT">British Indian Ocean Territory</option>
      <option value="BRN">Brunei Darussalam</option>
      <option value="BGR">Bulgaria</option>
      <option value="BFA">Burkina Faso</option>
      <option value="BDI">Burundi</option>
      <option value="KHM">Cambodia</option>
      <option value="CMR">Cameroon</option>
      <option value="CAN">Canada</option>
      <option value="CPV">Cape Verde</option>
      <option value="CYM">Cayman Islands</option>
      <option value="CAF">Central African Republic</option>
      <option value="TCD">Chad</option>
      <option value="CHL">Chile</option>
      <option value="CHN">China</option>
      <option value="CXR">Christmas Island</option>
      <option value="CCK">Cocos (Keeling) Islands</option>
      <option value="COL">Colombia</option>
      <option value="COM">Comoros</option>
      <option value="COG">Congo</option>
      <option value="COD">Congo, the Democratic Republic of the</option>
      <option value="COK">Cook Islands</option>
      <option value="CRI">Costa Rica</option>
      <option value="CIV">Côte d'Ivoire</option>
      <option value="HRV">Croatia</option>
      <option value="CUB">Cuba</option>
      <option value="CUW">Curaçao</option>
      <option value="CYP">Cyprus</option>
      <option value="CZE">Czech Republic</option>
      <option value="DNK">Denmark</option>
      <option value="DJI">Djibouti</option>
      <option value="DMA">Dominica</option>
      <option value="DOM">Dominican Republic</option>
      <option value="ECU">Ecuador</option>
      <option value="EGY">Egypt</option>
      <option value="SLV">El Salvador</option>
      <option value="GNQ">Equatorial Guinea</option>
      <option value="ERI">Eritrea</option>
      <option value="EST">Estonia</option>
      <option value="ETH">Ethiopia</option>
      <option value="FLK">Falkland Islands (Malvinas)</option>
      <option value="FRO">Faroe Islands</option>
      <option value="FJI">Fiji</option>
      <option value="FIN">Finland</option>
      <option value="FRA">France</option>
      <option value="GUF">French Guiana</option>
      <option value="PYF">French Polynesia</option>
      <option value="ATF">French Southern Territories</option>
      <option value="GAB">Gabon</option>
      <option value="GMB">Gambia</option>
      <option value="GEO">Georgia</option>
      <option value="DEU">Germany</option>
      <option value="GHA">Ghana</option>
      <option value="GIB">Gibraltar</option>
      <option value="GRC">Greece</option>
      <option value="GRL">Greenland</option>
      <option value="GRD">Grenada</option>
      <option value="GLP">Guadeloupe</option>
      <option value="GUM">Guam</option>
      <option value="GTM">Guatemala</option>
      <option value="GGY">Guernsey</option>
      <option value="GIN">Guinea</option>
      <option value="GNB">Guinea-Bissau</option>
      <option value="GUY">Guyana</option>
      <option value="HTI">Haiti</option>
      <option value="HMD">Heard Island and McDonald Islands</option>
      <option value="VAT">Holy See (Vatican City State)</option>
      <option value="HND">Honduras</option>
      <option value="HKG">Hong Kong</option>
      <option value="HUN">Hungary</option>
      <option value="ISL">Iceland</option>
      <option value="IND">India</option>
      <option value="IDN">Indonesia</option>
      <option value="IRN">Iran, Islamic Republic of</option>
      <option value="IRQ">Iraq</option>
      <option value="IRL">Ireland</option>
      <option value="IMN">Isle of Man</option>
      <option value="ISR">Israel</option>
      <option value="ITA">Italy</option>
      <option value="JAM">Jamaica</option>
      <option value="JPN">Japan</option>
      <option value="JEY">Jersey</option>
      <option value="JOR">Jordan</option>
      <option value="KAZ">Kazakhstan</option>
      <option value="KEN">Kenya</option>
      <option value="KIR">Kiribati</option>
      <option value="PRK">Korea, Democratic People's Republic of</option>
      <option value="KOR">Korea, Republic of</option>
      <option value="KWT">Kuwait</option>
      <option value="KGZ">Kyrgyzstan</option>
      <option value="LAO">Lao People's Democratic Republic</option>
      <option value="LVA">Latvia</option>
      <option value="LBN">Lebanon</option>
      <option value="LSO">Lesotho</option>
      <option value="LBR">Liberia</option>
      <option value="LBY">Libya</option>
      <option value="LIE">Liechtenstein</option>
      <option value="LTU">Lithuania</option>
      <option value="LUX">Luxembourg</option>
      <option value="MAC">Macao</option>
      <option value="MKD">Macedonia, the former Yugoslav Republic of</option>
      <option value="MDG">Madagascar</option>
      <option value="MWI">Malawi</option>
      <option value="MYS">Malaysia</option>
      <option value="MDV">Maldives</option>
      <option value="MLI">Mali</option>
      <option value="MLT">Malta</option>
      <option value="MHL">Marshall Islands</option>
      <option value="MTQ">Martinique</option>
      <option value="MRT">Mauritania</option>
      <option value="MUS">Mauritius</option>
      <option value="MYT">Mayotte</option>
      <option value="MEX">Mexico</option>
      <option value="FSM">Micronesia, Federated States of</option>
      <option value="MDA">Moldova, Republic of</option>
      <option value="MCO">Monaco</option>
      <option value="MNG">Mongolia</option>
      <option value="MNE">Montenegro</option>
      <option value="MSR">Montserrat</option>
      <option value="MAR">Morocco</option>
      <option value="MOZ">Mozambique</option>
      <option value="MMR">Myanmar</option>
      <option value="NAM">Namibia</option>
      <option value="NRU">Nauru</option>
      <option value="NPL">Nepal</option>
      <option value="NLD">Netherlands</option>
      <option value="NCL">New Caledonia</option>
      <option value="NZL">New Zealand</option>
      <option value="NIC">Nicaragua</option>
      <option value="NER">Niger</option>
      <option value="NGA">Nigeria</option>
      <option value="NIU">Niue</option>
      <option value="NFK">Norfolk Island</option>
      <option value="MNP">Northern Mariana Islands</option>
      <option value="NOR">Norway</option>
      <option value="OMN">Oman</option>
      <option value="PAK">Pakistan</option>
      <option value="PLW">Palau</option>
      <option value="PSE">Palestinian Territory, Occupied</option>
      <option value="PAN">Panama</option>
      <option value="PNG">Papua New Guinea</option>
      <option value="PRY">Paraguay</option>
      <option value="PER">Peru</option>
      <option value="PHL">Philippines</option>
      <option value="PCN">Pitcairn</option>
      <option value="POL">Poland</option>
      <option value="PRT">Portugal</option>
      <option value="PRI">Puerto Rico</option>
      <option value="QAT">Qatar</option>
      <option value="REU">Réunion</option>
      <option value="ROU">Romania</option>
      <option value="RUS">Russian Federation</option>
      <option value="RWA">Rwanda</option>
      <option value="BLM">Saint Barthélemy</option>
      <option value="SHN">Saint Helena, Ascension and Tristan da Cunha</option>
      <option value="KNA">Saint Kitts and Nevis</option>
      <option value="LCA">Saint Lucia</option>
      <option value="MAF">Saint Martin (French part)</option>
      <option value="SPM">Saint Pierre and Miquelon</option>
      <option value="VCT">Saint Vincent and the Grenadines</option>
      <option value="WSM">Samoa</option>
      <option value="SMR">San Marino</option>
      <option value="STP">Sao Tome and Principe</option>
      <option value="SAU">Saudi Arabia</option>
      <option value="SEN">Senegal</option>
      <option value="SRB">Serbia</option>
      <option value="SYC">Seychelles</option>
      <option value="SLE">Sierra Leone</option>
      <option value="SGP">Singapore</option>
      <option value="SXM">Sint Maarten (Dutch part)</option>
      <option value="SVK">Slovakia</option>
      <option value="SVN">Slovenia</option>
      <option value="SLB">Solomon Islands</option>
      <option value="SOM">Somalia</option>
      <option value="ZAF">South Africa</option>
      <option value="SGS">South Georgia and the South Sandwich Islands</option>
      <option value="SSD">South Sudan</option>
      <option value="ESP">Spain</option>
      <option value="LKA">Sri Lanka</option>
      <option value="SDN">Sudan</option>
      <option value="SUR">Suriname</option>
      <option value="SJM">Svalbard and Jan Mayen</option>
      <option value="SWZ">Swaziland</option>
      <option value="SWE">Sweden</option>
      <option value="CHE">Switzerland</option>
      <option value="SYR">Syrian Arab Republic</option>
      <option value="TWN">Taiwan, Province of China</option>
      <option value="TJK">Tajikistan</option>
      <option value="TZA">Tanzania, United Republic of</option>
      <option value="THA">Thailand</option>
      <option value="TLS">Timor-Leste</option>
      <option value="TGO">Togo</option>
      <option value="TKL">Tokelau</option>
      <option value="TON">Tonga</option>
      <option value="TTO">Trinidad and Tobago</option>
      <option value="TUN">Tunisia</option>
      <option value="TUR">Turkey</option>
      <option value="TKM">Turkmenistan</option>
      <option value="TCA">Turks and Caicos Islands</option>
      <option value="TUV">Tuvalu</option>
      <option value="UGA">Uganda</option>
      <option value="UKR">Ukraine</option>
      <option value="ARE">United Arab Emirates</option>
      <option value="GBR">United Kingdom</option>
      <option value="USA">United States</option>
      <option value="UMI">United States Minor Outlying Islands</option>
      <option value="URY">Uruguay</option>
      <option value="UZB">Uzbekistan</option>
      <option value="VUT">Vanuatu</option>
      <option value="VEN">Venezuela, Bolivarian Republic of</option>
      <option value="VNM">Viet Nam</option>
      <option value="VGB">Virgin Islands, British</option>
      <option value="VIR">Virgin Islands, U.S.</option>
      <option value="WLF">Wallis and Futuna</option>
      <option value="ESH">Western Sahara</option>
      <option value="YEM">Yemen</option>
      <option value="ZMB">Zambia</option>
      <option value="ZWE">Zimbabwe</option>
      </select>

      </td>
      </tr>

      <tr>
      <th> Email Id : </th>
      <td> <input type="text" name="email_id"/></td>
      </tr>

      <tr>
      <th> Mobile Number : </th>
      <td><input type="text" name="mobile_number"/></td>
      </tr>

      <tr>
      <th>I have : </th>
      <td>
      <select id="old" name="i_have">
      <option value = "select_option">Select Option</option>
      <option value = "three_compact">3 Compact</option>
      <option value = "three_regular">3 Regular</option>
      <option value = "three_triple">3 Triple</option>
      <option value = "five_compact">5 Compact</option>
      <option value = "five_regular">5 Regular</option>
      <option value = "five_triple">5 Triple</option>
      <option value = "seven_compact">7 Compact</option>
      <option value = "seven_regular">7 Regular</option>
      <option value = "seven_triple">7 Triple</option>
      <option value = "nine_compact">9 Compact</option>
      <option value = "nine_regular">9 Regular</option>
      <option value = "nine_triple">9 Triple</option>
      </select>
      </td>
      </tr>

      <tr>
      <th>I want : </th>
      <td>
      <select id="new" name="i_want">
      <option value = "select_option">Select Option</option>
      <option value = "three_compact">3 Compact</option>
      <option value = "three_regular">3 Regular</option>
      <option value = "three_triple">3 Triple</option>
      <option value = "five_compact">5 Compact</option>
      <option value = "five_regular">5 Regular</option>
      <option value = "five_triple">5 Triple</option>
      <option value = "seven_compact">7 Compact</option>
      <option value = "seven_regular">7 Regular</option>
      <option value = "seven_triple">7 Triple</option>
      <option value = "nine_compact">9 Compact</option>
      <option value = "nine_regular">9 Regular</option>
      <option value = "nine_triple">9 Triple</option>
      </select>
      </td>
      </tr>

      <tr>
      <th>Price (Rs) : </th>
      <td id="result"></td>
      <input type="hidden" name="price" id="price"/>
      </tr>

      <tr>
      <th>Shipping Weight : </th>
      <td id="show"></td>
      <input type="hidden" name="shipping_weight" id="shipping_weight"/>
      </tr>
      <tr>
      <th>Shipping Cost (Rs) : </th>
      <td id="findata"></td>
      <input type="hidden" name="shipping_cost" id="shipping_cost"/>
      </tr>
      <tr>
      <th> Grand Total: </th>
      <td> <input type="text" name="grand_total"/></td>
      </tr>

      </table> <br/>

      <!-- <b><div id="result" name="price"></div></b> -->

      <input type="Submit" value="Pay" name="pay"/>

      </form>
      </body>

      <!-- <button id="btn_check_value">Check for value</button> -->

      <!-- Ajax Script -->

      <!-- Ajax Price -->

      <script>
      $(document).ready(function(){
      $('#new').on('change',function(){

      var old_val = $("#old option:selected").val();
      var new_val = $("#new option:selected").val();

      $.ajax({
      type: "POST",
      url: "ajax_data.php",
      dataType: "text",
      data: { old: old_val, new: new_val},
      success: function(data)
      {
      // Check the output of ajax call on firebug console
      // console.log(data);
      $('#result').html(data);
      $('#price').val(data);

      }
      });

      });
      });

      </script>

      <!-- Ajax shipping weight -->

      <script>
      $(document).ready(function(){
      $('#new').on('change',function(){

      var old_val = $("#old option:selected").val();
      var new_val = $("#new option:selected").val();

      $.ajax({
      type: "POST",
      url: "ajax_ship_weight.php",
      dataType: "text",
      data: { old: old_val, new: new_val},
      success: function(data)
      {
      // Check the output of ajax call on firebug console
      //console.log(data);
      $('#show').html(data);
      $('#shipping_weight').val(data);

      }
      });

      });
      });
      </script>

      <!-- Ajax shipping cost -->

      <script>
      $(document).ready(function(){
      $('#new').on('change',function(){

      var old_val = $("#old option:selected").val();
      var new_val = $("#new option:selected").val();

      $.ajax({
      type: "POST",
      // url: "ajax_ship_cost_data.php",
      url: "sp_cost.php",
      dataType: "text",
      data: { old: old_val, new: new_val},
      success: function(data)
      {
      // Check the output of ajax call on firebug console
      //console.log(data);
      $('#findata').html(data);
      $('#shipping_cost').val(data);

      }
      });

      });
      });
      </script>
      </html>


      PHP :



      <?php
      ob_start();
      require_once('./../app/Mage.php');
      umask(0);
      ini_set('display_errors',true);
      ini_set('memory_limit', '1024M');
      Mage::app()->loadArea('frontend');

      function getShippingEstimate($productId,$productQty,$countryId,$postcode ) {

      $quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore('english')->getId());
      $_product = Mage::getModel('catalog/product')->load($productId);

      $_product->getStockItem()->setUseConfigManageStock(false);
      $_product->getStockItem()->setManageStock(false);

      $quote->addProduct($_product, $productQty);
      $quote->getShippingAddress()->setCountryId($countryId)->setPostcode($postcode);
      $quote->getShippingAddress()->collectTotals();
      $quote->getShippingAddress()->setCollectShippingRates(true);
      $quote->getShippingAddress()->collectShippingRates();

      $_rates = $quote->getShippingAddress()->getShippingRatesCollection();

      $shippingRates = array();
      foreach ($_rates as $_rate):
      if($_rate->getPrice() > 0) {
      $shippingRates = array("Title" => $_rate->getMethodTitle(), "Price" => $_rate->getPrice());
      }
      endforeach;

      return $shippingRates;

      }

      ###### Post #######
      // if(isset($_POST['zip_postal_code']) && isset($_POST['country']))
      // {
      // $zip_postal_code = $_POST['zip_postal_code'];
      // $country = $_POST['country'];

      // }


      //$results = getShippingEstimate('14419','1',"$zip_postal_code","$country");

      ###### Post #######

      $results = getShippingEstimate('14419','1',"IN","642001"); // Predefined Value

      $count = -1;
      echo "<select>";
      foreach ($results as $result):
      $count++;
      ?>
      <option value="<?php echo $count; ?>"> <?php echo $result["Title"]." - Rs ".$result["Price"]?>
      </option>

      <?php
      endforeach;
      echo "</select>";
      ?>


      Expected output :



      Get input value Zip-code and Country from form and passing values to php function using Ajax and return my shipping cost.



      Workout output:



      using predefined value,



      $results = getShippingEstimate('14419','1',"IN","642001"); // Predefined Value


      enter image description here



      Try with $_POST,



       if(isset($_POST['zip_postal_code']) && isset($_POST['country']))
      {
      $zip_postal_code = $_POST['zip_postal_code'];
      $country = $_POST['country'];

      }


      $results = getShippingEstimate('14419','1',"$zip_postal_code","$country");


      enter image description here



      Code help thanks to Stack Overflow









      share
















      I have PHP function for getting shipping cost form magento based on productId, productQty, countryId, postcode. Here my productId, productQty are static and countryId, postcode is dynamic based on user entry,



      if i try with predefined value,



      $results = getShippingEstimate('14419','1',"IN","642001"); // Predefined Value


      Return my shipping cost as per based on magento using "IN","642001" (Country &Zipcode)



      //if i try with PHP function,

      if(isset($_POST['zip_postal_code']) && isset($_POST['country']))
      {
      $zip_postal_code = $_POST['zip_postal_code'];
      $country = $_POST['country'];

      }


      $results = getShippingEstimate('14419','1',"$zip_postal_code","$country");


      Not working.



      NOTE : Here my shipping cost field as Auto response,



      HTML FORM :



      <html>
      <head>
      <title> Upgrade Cost</title>

      <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

      </head>
      <body>
      <!-- Ajax data -->
      <h2>KoluPadi Upgrade Form</h2>
      <form action='ajax_data.php' method='POST' >

      <table border="0" cellspacing="1" cellpadding="2">
      <tr>
      <th> First Name : </th>
      <td> <input type="text" name="first_name"/> </td>
      </tr>
      <tr>
      <th> Last Name : </th>
      <td> <input type="text" name="last_name"/> </td>
      </tr>

      <tr>
      <th> Address : </th>
      <td> <input type="text" name="address"/> </td>
      </tr>

      <tr>
      <th> City : </th>
      <td> <input type="text" name="city"/> </td>
      </tr>

      <tr>
      <th> State/Province : </th>
      <td> <input type="text" name="state_province"/> </td>
      </tr>

      <tr>
      <th> Zip/Postal Code : </th>
      <td> <input type="text" name="zip_postal_code"/> </td>
      </tr>

      <tr>
      <th> Country : </th>
      <td>
      <select name="country" style="width: 173px;">
      <option value="AFG">Afghanistan</option>
      <option value="ALA">Åland Islands</option>
      <option value="ALB">Albania</option>
      <option value="DZA">Algeria</option>
      <option value="ASM">American Samoa</option>
      <option value="AND">Andorra</option>
      <option value="AGO">Angola</option>
      <option value="AIA">Anguilla</option>
      <option value="ATA">Antarctica</option>
      <option value="ATG">Antigua and Barbuda</option>
      <option value="ARG">Argentina</option>
      <option value="ARM">Armenia</option>
      <option value="ABW">Aruba</option>
      <option value="AUS">Australia</option>
      <option value="AUT">Austria</option>
      <option value="AZE">Azerbaijan</option>
      <option value="BHS">Bahamas</option>
      <option value="BHR">Bahrain</option>
      <option value="BGD">Bangladesh</option>
      <option value="BRB">Barbados</option>
      <option value="BLR">Belarus</option>
      <option value="BEL">Belgium</option>
      <option value="BLZ">Belize</option>
      <option value="BEN">Benin</option>
      <option value="BMU">Bermuda</option>
      <option value="BTN">Bhutan</option>
      <option value="BOL">Bolivia, Plurinational State of</option>
      <option value="BES">Bonaire, Sint Eustatius and Saba</option>
      <option value="BIH">Bosnia and Herzegovina</option>
      <option value="BWA">Botswana</option>
      <option value="BVT">Bouvet Island</option>
      <option value="BRA">Brazil</option>
      <option value="IOT">British Indian Ocean Territory</option>
      <option value="BRN">Brunei Darussalam</option>
      <option value="BGR">Bulgaria</option>
      <option value="BFA">Burkina Faso</option>
      <option value="BDI">Burundi</option>
      <option value="KHM">Cambodia</option>
      <option value="CMR">Cameroon</option>
      <option value="CAN">Canada</option>
      <option value="CPV">Cape Verde</option>
      <option value="CYM">Cayman Islands</option>
      <option value="CAF">Central African Republic</option>
      <option value="TCD">Chad</option>
      <option value="CHL">Chile</option>
      <option value="CHN">China</option>
      <option value="CXR">Christmas Island</option>
      <option value="CCK">Cocos (Keeling) Islands</option>
      <option value="COL">Colombia</option>
      <option value="COM">Comoros</option>
      <option value="COG">Congo</option>
      <option value="COD">Congo, the Democratic Republic of the</option>
      <option value="COK">Cook Islands</option>
      <option value="CRI">Costa Rica</option>
      <option value="CIV">Côte d'Ivoire</option>
      <option value="HRV">Croatia</option>
      <option value="CUB">Cuba</option>
      <option value="CUW">Curaçao</option>
      <option value="CYP">Cyprus</option>
      <option value="CZE">Czech Republic</option>
      <option value="DNK">Denmark</option>
      <option value="DJI">Djibouti</option>
      <option value="DMA">Dominica</option>
      <option value="DOM">Dominican Republic</option>
      <option value="ECU">Ecuador</option>
      <option value="EGY">Egypt</option>
      <option value="SLV">El Salvador</option>
      <option value="GNQ">Equatorial Guinea</option>
      <option value="ERI">Eritrea</option>
      <option value="EST">Estonia</option>
      <option value="ETH">Ethiopia</option>
      <option value="FLK">Falkland Islands (Malvinas)</option>
      <option value="FRO">Faroe Islands</option>
      <option value="FJI">Fiji</option>
      <option value="FIN">Finland</option>
      <option value="FRA">France</option>
      <option value="GUF">French Guiana</option>
      <option value="PYF">French Polynesia</option>
      <option value="ATF">French Southern Territories</option>
      <option value="GAB">Gabon</option>
      <option value="GMB">Gambia</option>
      <option value="GEO">Georgia</option>
      <option value="DEU">Germany</option>
      <option value="GHA">Ghana</option>
      <option value="GIB">Gibraltar</option>
      <option value="GRC">Greece</option>
      <option value="GRL">Greenland</option>
      <option value="GRD">Grenada</option>
      <option value="GLP">Guadeloupe</option>
      <option value="GUM">Guam</option>
      <option value="GTM">Guatemala</option>
      <option value="GGY">Guernsey</option>
      <option value="GIN">Guinea</option>
      <option value="GNB">Guinea-Bissau</option>
      <option value="GUY">Guyana</option>
      <option value="HTI">Haiti</option>
      <option value="HMD">Heard Island and McDonald Islands</option>
      <option value="VAT">Holy See (Vatican City State)</option>
      <option value="HND">Honduras</option>
      <option value="HKG">Hong Kong</option>
      <option value="HUN">Hungary</option>
      <option value="ISL">Iceland</option>
      <option value="IND">India</option>
      <option value="IDN">Indonesia</option>
      <option value="IRN">Iran, Islamic Republic of</option>
      <option value="IRQ">Iraq</option>
      <option value="IRL">Ireland</option>
      <option value="IMN">Isle of Man</option>
      <option value="ISR">Israel</option>
      <option value="ITA">Italy</option>
      <option value="JAM">Jamaica</option>
      <option value="JPN">Japan</option>
      <option value="JEY">Jersey</option>
      <option value="JOR">Jordan</option>
      <option value="KAZ">Kazakhstan</option>
      <option value="KEN">Kenya</option>
      <option value="KIR">Kiribati</option>
      <option value="PRK">Korea, Democratic People's Republic of</option>
      <option value="KOR">Korea, Republic of</option>
      <option value="KWT">Kuwait</option>
      <option value="KGZ">Kyrgyzstan</option>
      <option value="LAO">Lao People's Democratic Republic</option>
      <option value="LVA">Latvia</option>
      <option value="LBN">Lebanon</option>
      <option value="LSO">Lesotho</option>
      <option value="LBR">Liberia</option>
      <option value="LBY">Libya</option>
      <option value="LIE">Liechtenstein</option>
      <option value="LTU">Lithuania</option>
      <option value="LUX">Luxembourg</option>
      <option value="MAC">Macao</option>
      <option value="MKD">Macedonia, the former Yugoslav Republic of</option>
      <option value="MDG">Madagascar</option>
      <option value="MWI">Malawi</option>
      <option value="MYS">Malaysia</option>
      <option value="MDV">Maldives</option>
      <option value="MLI">Mali</option>
      <option value="MLT">Malta</option>
      <option value="MHL">Marshall Islands</option>
      <option value="MTQ">Martinique</option>
      <option value="MRT">Mauritania</option>
      <option value="MUS">Mauritius</option>
      <option value="MYT">Mayotte</option>
      <option value="MEX">Mexico</option>
      <option value="FSM">Micronesia, Federated States of</option>
      <option value="MDA">Moldova, Republic of</option>
      <option value="MCO">Monaco</option>
      <option value="MNG">Mongolia</option>
      <option value="MNE">Montenegro</option>
      <option value="MSR">Montserrat</option>
      <option value="MAR">Morocco</option>
      <option value="MOZ">Mozambique</option>
      <option value="MMR">Myanmar</option>
      <option value="NAM">Namibia</option>
      <option value="NRU">Nauru</option>
      <option value="NPL">Nepal</option>
      <option value="NLD">Netherlands</option>
      <option value="NCL">New Caledonia</option>
      <option value="NZL">New Zealand</option>
      <option value="NIC">Nicaragua</option>
      <option value="NER">Niger</option>
      <option value="NGA">Nigeria</option>
      <option value="NIU">Niue</option>
      <option value="NFK">Norfolk Island</option>
      <option value="MNP">Northern Mariana Islands</option>
      <option value="NOR">Norway</option>
      <option value="OMN">Oman</option>
      <option value="PAK">Pakistan</option>
      <option value="PLW">Palau</option>
      <option value="PSE">Palestinian Territory, Occupied</option>
      <option value="PAN">Panama</option>
      <option value="PNG">Papua New Guinea</option>
      <option value="PRY">Paraguay</option>
      <option value="PER">Peru</option>
      <option value="PHL">Philippines</option>
      <option value="PCN">Pitcairn</option>
      <option value="POL">Poland</option>
      <option value="PRT">Portugal</option>
      <option value="PRI">Puerto Rico</option>
      <option value="QAT">Qatar</option>
      <option value="REU">Réunion</option>
      <option value="ROU">Romania</option>
      <option value="RUS">Russian Federation</option>
      <option value="RWA">Rwanda</option>
      <option value="BLM">Saint Barthélemy</option>
      <option value="SHN">Saint Helena, Ascension and Tristan da Cunha</option>
      <option value="KNA">Saint Kitts and Nevis</option>
      <option value="LCA">Saint Lucia</option>
      <option value="MAF">Saint Martin (French part)</option>
      <option value="SPM">Saint Pierre and Miquelon</option>
      <option value="VCT">Saint Vincent and the Grenadines</option>
      <option value="WSM">Samoa</option>
      <option value="SMR">San Marino</option>
      <option value="STP">Sao Tome and Principe</option>
      <option value="SAU">Saudi Arabia</option>
      <option value="SEN">Senegal</option>
      <option value="SRB">Serbia</option>
      <option value="SYC">Seychelles</option>
      <option value="SLE">Sierra Leone</option>
      <option value="SGP">Singapore</option>
      <option value="SXM">Sint Maarten (Dutch part)</option>
      <option value="SVK">Slovakia</option>
      <option value="SVN">Slovenia</option>
      <option value="SLB">Solomon Islands</option>
      <option value="SOM">Somalia</option>
      <option value="ZAF">South Africa</option>
      <option value="SGS">South Georgia and the South Sandwich Islands</option>
      <option value="SSD">South Sudan</option>
      <option value="ESP">Spain</option>
      <option value="LKA">Sri Lanka</option>
      <option value="SDN">Sudan</option>
      <option value="SUR">Suriname</option>
      <option value="SJM">Svalbard and Jan Mayen</option>
      <option value="SWZ">Swaziland</option>
      <option value="SWE">Sweden</option>
      <option value="CHE">Switzerland</option>
      <option value="SYR">Syrian Arab Republic</option>
      <option value="TWN">Taiwan, Province of China</option>
      <option value="TJK">Tajikistan</option>
      <option value="TZA">Tanzania, United Republic of</option>
      <option value="THA">Thailand</option>
      <option value="TLS">Timor-Leste</option>
      <option value="TGO">Togo</option>
      <option value="TKL">Tokelau</option>
      <option value="TON">Tonga</option>
      <option value="TTO">Trinidad and Tobago</option>
      <option value="TUN">Tunisia</option>
      <option value="TUR">Turkey</option>
      <option value="TKM">Turkmenistan</option>
      <option value="TCA">Turks and Caicos Islands</option>
      <option value="TUV">Tuvalu</option>
      <option value="UGA">Uganda</option>
      <option value="UKR">Ukraine</option>
      <option value="ARE">United Arab Emirates</option>
      <option value="GBR">United Kingdom</option>
      <option value="USA">United States</option>
      <option value="UMI">United States Minor Outlying Islands</option>
      <option value="URY">Uruguay</option>
      <option value="UZB">Uzbekistan</option>
      <option value="VUT">Vanuatu</option>
      <option value="VEN">Venezuela, Bolivarian Republic of</option>
      <option value="VNM">Viet Nam</option>
      <option value="VGB">Virgin Islands, British</option>
      <option value="VIR">Virgin Islands, U.S.</option>
      <option value="WLF">Wallis and Futuna</option>
      <option value="ESH">Western Sahara</option>
      <option value="YEM">Yemen</option>
      <option value="ZMB">Zambia</option>
      <option value="ZWE">Zimbabwe</option>
      </select>

      </td>
      </tr>

      <tr>
      <th> Email Id : </th>
      <td> <input type="text" name="email_id"/></td>
      </tr>

      <tr>
      <th> Mobile Number : </th>
      <td><input type="text" name="mobile_number"/></td>
      </tr>

      <tr>
      <th>I have : </th>
      <td>
      <select id="old" name="i_have">
      <option value = "select_option">Select Option</option>
      <option value = "three_compact">3 Compact</option>
      <option value = "three_regular">3 Regular</option>
      <option value = "three_triple">3 Triple</option>
      <option value = "five_compact">5 Compact</option>
      <option value = "five_regular">5 Regular</option>
      <option value = "five_triple">5 Triple</option>
      <option value = "seven_compact">7 Compact</option>
      <option value = "seven_regular">7 Regular</option>
      <option value = "seven_triple">7 Triple</option>
      <option value = "nine_compact">9 Compact</option>
      <option value = "nine_regular">9 Regular</option>
      <option value = "nine_triple">9 Triple</option>
      </select>
      </td>
      </tr>

      <tr>
      <th>I want : </th>
      <td>
      <select id="new" name="i_want">
      <option value = "select_option">Select Option</option>
      <option value = "three_compact">3 Compact</option>
      <option value = "three_regular">3 Regular</option>
      <option value = "three_triple">3 Triple</option>
      <option value = "five_compact">5 Compact</option>
      <option value = "five_regular">5 Regular</option>
      <option value = "five_triple">5 Triple</option>
      <option value = "seven_compact">7 Compact</option>
      <option value = "seven_regular">7 Regular</option>
      <option value = "seven_triple">7 Triple</option>
      <option value = "nine_compact">9 Compact</option>
      <option value = "nine_regular">9 Regular</option>
      <option value = "nine_triple">9 Triple</option>
      </select>
      </td>
      </tr>

      <tr>
      <th>Price (Rs) : </th>
      <td id="result"></td>
      <input type="hidden" name="price" id="price"/>
      </tr>

      <tr>
      <th>Shipping Weight : </th>
      <td id="show"></td>
      <input type="hidden" name="shipping_weight" id="shipping_weight"/>
      </tr>
      <tr>
      <th>Shipping Cost (Rs) : </th>
      <td id="findata"></td>
      <input type="hidden" name="shipping_cost" id="shipping_cost"/>
      </tr>
      <tr>
      <th> Grand Total: </th>
      <td> <input type="text" name="grand_total"/></td>
      </tr>

      </table> <br/>

      <!-- <b><div id="result" name="price"></div></b> -->

      <input type="Submit" value="Pay" name="pay"/>

      </form>
      </body>

      <!-- <button id="btn_check_value">Check for value</button> -->

      <!-- Ajax Script -->

      <!-- Ajax Price -->

      <script>
      $(document).ready(function(){
      $('#new').on('change',function(){

      var old_val = $("#old option:selected").val();
      var new_val = $("#new option:selected").val();

      $.ajax({
      type: "POST",
      url: "ajax_data.php",
      dataType: "text",
      data: { old: old_val, new: new_val},
      success: function(data)
      {
      // Check the output of ajax call on firebug console
      // console.log(data);
      $('#result').html(data);
      $('#price').val(data);

      }
      });

      });
      });

      </script>

      <!-- Ajax shipping weight -->

      <script>
      $(document).ready(function(){
      $('#new').on('change',function(){

      var old_val = $("#old option:selected").val();
      var new_val = $("#new option:selected").val();

      $.ajax({
      type: "POST",
      url: "ajax_ship_weight.php",
      dataType: "text",
      data: { old: old_val, new: new_val},
      success: function(data)
      {
      // Check the output of ajax call on firebug console
      //console.log(data);
      $('#show').html(data);
      $('#shipping_weight').val(data);

      }
      });

      });
      });
      </script>

      <!-- Ajax shipping cost -->

      <script>
      $(document).ready(function(){
      $('#new').on('change',function(){

      var old_val = $("#old option:selected").val();
      var new_val = $("#new option:selected").val();

      $.ajax({
      type: "POST",
      // url: "ajax_ship_cost_data.php",
      url: "sp_cost.php",
      dataType: "text",
      data: { old: old_val, new: new_val},
      success: function(data)
      {
      // Check the output of ajax call on firebug console
      //console.log(data);
      $('#findata').html(data);
      $('#shipping_cost').val(data);

      }
      });

      });
      });
      </script>
      </html>


      PHP :



      <?php
      ob_start();
      require_once('./../app/Mage.php');
      umask(0);
      ini_set('display_errors',true);
      ini_set('memory_limit', '1024M');
      Mage::app()->loadArea('frontend');

      function getShippingEstimate($productId,$productQty,$countryId,$postcode ) {

      $quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore('english')->getId());
      $_product = Mage::getModel('catalog/product')->load($productId);

      $_product->getStockItem()->setUseConfigManageStock(false);
      $_product->getStockItem()->setManageStock(false);

      $quote->addProduct($_product, $productQty);
      $quote->getShippingAddress()->setCountryId($countryId)->setPostcode($postcode);
      $quote->getShippingAddress()->collectTotals();
      $quote->getShippingAddress()->setCollectShippingRates(true);
      $quote->getShippingAddress()->collectShippingRates();

      $_rates = $quote->getShippingAddress()->getShippingRatesCollection();

      $shippingRates = array();
      foreach ($_rates as $_rate):
      if($_rate->getPrice() > 0) {
      $shippingRates = array("Title" => $_rate->getMethodTitle(), "Price" => $_rate->getPrice());
      }
      endforeach;

      return $shippingRates;

      }

      ###### Post #######
      // if(isset($_POST['zip_postal_code']) && isset($_POST['country']))
      // {
      // $zip_postal_code = $_POST['zip_postal_code'];
      // $country = $_POST['country'];

      // }


      //$results = getShippingEstimate('14419','1',"$zip_postal_code","$country");

      ###### Post #######

      $results = getShippingEstimate('14419','1',"IN","642001"); // Predefined Value

      $count = -1;
      echo "<select>";
      foreach ($results as $result):
      $count++;
      ?>
      <option value="<?php echo $count; ?>"> <?php echo $result["Title"]." - Rs ".$result["Price"]?>
      </option>

      <?php
      endforeach;
      echo "</select>";
      ?>


      Expected output :



      Get input value Zip-code and Country from form and passing values to php function using Ajax and return my shipping cost.



      Workout output:



      using predefined value,



      $results = getShippingEstimate('14419','1',"IN","642001"); // Predefined Value


      enter image description here



      Try with $_POST,



       if(isset($_POST['zip_postal_code']) && isset($_POST['country']))
      {
      $zip_postal_code = $_POST['zip_postal_code'];
      $country = $_POST['country'];

      }


      $results = getShippingEstimate('14419','1',"$zip_postal_code","$country");


      enter image description here



      Code help thanks to Stack Overflow







      magento-1.9 php forms autocomplete





      share














      share












      share



      share








      edited 3 mins ago







      zus

















      asked 8 mins ago









      zuszus

      119120




      119120






















          0






          active

          oldest

          votes












          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "479"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f272658%2fhow-to-get-shipping-cost-based-user-entries%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Magento Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f272658%2fhow-to-get-shipping-cost-based-user-entries%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          What other Star Trek series did the main TNG cast show up in?

          Berlina muro

          Berlina aerponto