문제

다음과 같은 작은 유틸리티를 표시하고 싶습니다. shipping calculator 상품보기 페이지에서

우리는 기본 magento가 장바구니 페이지에서 이 섹션을 제공한다는 것을 알고 있습니다.

제품 보기 페이지에 어떻게 구현하나요?

국가 드롭다운 및 우편번호와 같은 콘텐츠 필드가 포함된 작은 양식이 있습니다.enter image description here

누군가 클릭하면 Calculate 그런 다음 배송 견적은 Ajax를 사용하여 표시되어야 합니다.

나는 여기에 꽤 붙어 있었다.도와주세요.

내가 만들었다 controller & calculateAction() 방법.거기에 어떤 코드를 쓸 수 있나요? calculateaction() 기능 ?

도움이 되었습니까?

해결책

Ajax 호출을 위한 배송 계산기 양식 아래의 .phtml 파일에 아래 코드를 추가하세요.

<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>

이제 Ajax 요청을 처리하기 위한 컨트롤러 작업을 생성해야 합니다.이에 대해서는 아래 코드 조각을 참조하세요.

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);
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top