Question

I want to display smalll utility like shipping calculator on product view page.

we know native magento provides this section at Cart Page.

How I can implement it on product view page ?

I have a small form which content fields like Country Dropdown & Zip Code. enter image description here

When someone click on Calculate then shipping quote should be display using Ajax.

I stuck here pretty. Please help me.

I have created controller & calculateAction() method. What code I can write into that calculateaction() function ?

Was it helpful?

Solution

Please add below code in .phtml file under your shipping calculator form for ajax call.

<script type="text/javascript">
            //<![CDATA[
                var coShippingEstimateForm = new VarienForm('your_form_id',true);
            //]]>
            function getEstimateShipping(){
                if (coShippingEstimateForm.validator.validate()) {
                    new Ajax.Updater(
                        { success:'result_container_id' }, "<?php echo $this->getUrl('your_route_name/your_controller/calculate') ?>", {
                            method:'post',
                            asynchronous:true,
                            evalScripts:false,
                            onSuccess:function(transport) {
                                var shiphtml = transport.responseText;
                                if(shiphtml != "" && shiphtml != null){
                                    $('result_container_id').insert(shiphtml).show();
                                }else{
                                    alert("No shipping method available");
                                }
                                $('submit').disabled = false;
                            },
                            onLoading:function(request, json){
                                $('submit').disabled = true;
                            },
                            parameters:jQuery('form').serialize(true)
                        }
                    );
                }
            }
        </script>

Now you need to create a controller action to handle the ajax request. Please refer below code snippet for that.

public function calculateAction()
    {
        $country    = (string) $this->getRequest()->getParam('country_id');
        $postcode   = (string) $this->getRequest()->getParam('estimate_postcode');
        $qty = intval($this->getRequest()->getParam('qty'));
        if($qty == 0 || $qty == null){
            $qty = 1;
        }

        $currentProductId = $this->getRequest()->getPost('currunt_product');
        $quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore('default')->getId());
        $_product = Mage::getModel('catalog/product')->load($currentProductId);
        $params = $this->getRequest()->getParams();
        $reqOb = new Varien_Object($params);
        $_product->getStockItem()->setUseConfigManageStock(false);
        $_product->getStockItem()->setManageStock(false);
        $quote->addProduct($_product, $reqOb);
        $quote->getShippingAddress()->setCountryId($country)->setPostcode($postcode);
        $quote->getShippingAddress()->collectTotals();
        $quote->getShippingAddress()->setCollectShippingRates(true);
        $quote->getShippingAddress()->collectShippingRates();

        $groups = $quote->getShippingAddress()->getGroupedAllShippingRates();

        $shippingRates = array();
        $shippingHtml = "";
        $shippingBlock = new Mage_Checkout_Block_Cart_Shipping();
        foreach($groups as $code=>$_rates){
            $shippingHtml .= "<dt>" . $shippingBlock->getCarrierName($code) . "</dt><dd><ul>";
            foreach ($_rates as $_rate) {
                //if($_rate->getPrice() > 0) {
                $shippingHtml .= "<li><label>";
                $shippingHtml .= $_rate->getMethodTitle();
                $shippingHtml .= " - ";
                $shippingHtml .= Mage::helper('core')->currency($_rate->getPrice(), true, false);
                $shippingHtml .= "</label></li>";
            }
            $shippingHtml .= "</ul></dd>";
        }
        $this->getResponse()->setBody($shippingHtml);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top