0

I am getting my shipping value from Magento,

sp_cost.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',$country,$zip_postal_code);
###### Post #######
// $results = getShippingEstimate('14419','1',"IND","642001"); // Predefined Value

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

and i am using ajax for customer choose anyone, if customer selects anyone of dropdown all the values save into table, how to restrict?

Form

<tr>
    <th>Shipping Cost (Rs) : </th> 
    <td id="findata"></td>
     <input type="hidden" name="shipping_cost"  id="shipping_cost"/>
</tr>  

Ajax :

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

        var zip = $("#zip_postal_code").val();
        var country = $("#country").val();


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

        }
});

});
});
</script>

FYI -> https://snag.gy/fvTWXB.jpg

0

As you want your customer to choose from one of available shipping methods. then use this code. The only change I have made is the value of your dropdown, you have used $count, instead of $count you need to use price in dropdown value

<?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 = "";
    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',$country,$zip_postal_code);
###### Post #######
// $results = getShippingEstimate('14419','1',"IND","642001"); // Predefined Value

echo "<select name='shipping_cost'>";
foreach ($results as $result):
    ?>
    <option value="<?php echo $result["Price"]; ?>"> <?php echo $result["Title"]." - Rs ".$result["Price"];?>
    </option>
    <?php
endforeach;
echo "</select>";
  • The value not applying on form – aveeva May 7 at 5:36
  • Any help.......? – aveeva May 7 at 5:49
  • 1
    Really thanks for your time bro, let me try.. – aveeva May 7 at 6:15
  • 1
    Finally did, In Form :: <tr> <th>Shipping Cost (Rs) : </th> <td> <select id="findata" name="shipping_cost"> </select></td> <!-- <td id="findata"></td> <input type="hidden" name="shipping_cost" id="shipping_cost"/> --> </tr> – aveeva May 7 at 11:26

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.