自一周以来我一直在尝试做同样的事情,但它不能 100% 工作。我认为代码应该是这样的:
第一步:添加一个JS函数:
_instantQuoteShippingResponse {
var cart = document.getElementsByClassName('cart-shipping-block')[0];
cart.innerHTML = response.responseText.evalJSON().cart_shipping;
cart.innerHTML = cart.firstChild.innerHTML;
return true;
}
instantQuoteShipping() {
try {var request = new Ajax.Request(coShippingMethodForm.form.action, {method: 'post',
parameters: {
country_id: $('country').value, region_id: $('region_id').value,
region: $('region').value, estimate_postcode: $('postcode').value},
redirect: 'no',
onSuccess: function(response) {_instantQuoteShippingResponse(response);},
onFailure: function(response) {coShippingMethodForm.submit();}});
} catch(e) { return false;}
return true;
}
第二步:将点击事件添加到运费报价按钮(app\design\frontend\default\modern\template\checkout\cart\shipping.phtml)。
onclick="instantQuoteShipping()"
第三步:编辑购物车控制器(Mage/Checkout/controllers/CartController.php)重写estimatePostAction:
public function estimatePostAction()
{
parent::estimatePostAction();
if ($this->getRequest()->getParam('redirect') == 'no') {
try {
$response = array();
$this->loadLayout();
$this->_getSession()->getMessages(true);
// $response['cart_shipping'] = $this->getLayout()->getBlock('checkout.cart.shipping')->toHtml();
$response['cart_shipping'] = $this->getLayout()
->createBlock('checkout/cart_shipping')
->setTemplate('checkout/cart/shipping.phtml')->toHtml();
$this->getResponse()->clearAllHeaders();
$this->getResponse()->setBody(Zend_Json::encode($response));
$this->getResponse()->setHttpResponseCode(200);
} catch(Exception $e) {
Mage::log($e->getMessage());
}
}
}
第四步:编辑 cart.phtml (app\design\frontend\default\modern\template\checkout\cart.phtml) 从 shipping.phtml 中复制所有内容,估计费率块除外:
// Locate this line:
<?php if (!$this->getIsVirtual()): echo $this->getChildHtml('shipping'); endif; ?>
// Update to...:
<?php if (!$this->getIsVirtual()): ?>
// Paste all content from shipping.phtml here
<?php endif; ?>
// ATENTION: Dont paste the code below
if (($_shippingRateGroups = $this->getEstimateRates())) {
...
}
它应该工作!