Magento-控制器到Ajax估计发货:加载块并显示phtml
我复制了estimatePostAction并制作了estimateAjaxPostAction(覆盖核心-我没有破解核心)。控制器操作也起作用(类Mage\u Checkout\u CartController) 现在,我想获得/创建一个块,用于在使用ajax估计发货后替换发货块。我试过这个:Magento-控制器到Ajax估计发货:加载块并显示phtml,ajax,magento,layout,block,shipping,Ajax,Magento,Layout,Block,Shipping,我复制了estimatePostAction并制作了estimateAjaxPostAction(覆盖核心-我没有破解核心)。控制器操作也起作用(类Mage\u Checkout\u CartController) 现在,我想获得/创建一个块,用于在使用ajax估计发货后替换发货块。我试过这个: public function estimateAjaxPostAction() { $country = (string) $this->getRequest()->getP
public function estimateAjaxPostAction()
{
$country = (string) $this->getRequest()->getParam('country_id');
$postcode = (string) $this->getRequest()->getParam('estimate_postcode');
$city = (string) $this->getRequest()->getParam('estimate_city');
$regionId = (string) $this->getRequest()->getParam('region_id');
$region = (string) $this->getRequest()->getParam('region');
$this->_getQuote()->getShippingAddress()
->setCountryId($country)
->setCity($city)
->setPostcode($postcode)
->setRegionId($regionId)
->setRegion($region)
->setCollectShippingRates(true);
$this->_getQuote()->save();
//$this->_goBack();
$this->loadLayout();
$block = $this->getLayout()->createBlock('Mage_Checkout_Block_Cart_Shipping','checkout.cart.shipping.ajax',array('template' => 'checkout/cart/shipping.phtml'));
if($block) {
$response = array();
$response['shipping'] = $block->toHtml();
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
}
}
创建了block checkout.cart.shipping.ajax。但是toHtml()不返回任何内容。
我的JSON返回:
{“运送”:“”}
为什么toHtml
方法不起作用
编辑:我的区块码(checkout/cart/shipping.phtml)
丹尼斯。。。我有修改代码,请检查
public function estimateAjaxPostAction()
{
$country = (string) $this->getRequest()->getParam('country_id');
$postcode = (string) $this->getRequest()->getParam('estimate_postcode');
$city = (string) $this->getRequest()->getParam('estimate_city');
$regionId = (string) $this->getRequest()->getParam('region_id');
$region = (string) $this->getRequest()->getParam('region');
$this->_getQuote()->getShippingAddress()
->setCountryId($country)
->setCity($city)
->setPostcode($postcode)
->setRegionId($regionId)
->setRegion($region)
->setCollectShippingRates(true);
$this->_getQuote()->save();
$response = array();
$response['shipping']=$this->eastmatesajax();
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
}
protected function eastmatesajax()
{
$layout=$this->getLayout();
$layout->getMessagesBlock()->setMessages(Mage::getSingleton('checkout/session')->getMessages(true),Mage::getSingleton('catalog/session')->getMessages(true));
$block = $this->getLayout()->createBlock('checkout/cart_shipping')->setTemplate( 'checkout/cart/shipping.phtml');
return $block->toHtml();
}
使用$this->_getQuote()->collectTotals()解决更新的区块问题
在$this->_getQuote()->save()之前代码>它可以工作。谢谢您能解释一下为什么我需要创建另一个函数吗?第一次渲染显示估计通知此解决方案用于调用块,但块未更新。在多次使用不同邮政编码的请求之后,结果总是相同的。只有在新页面加载后,最后一次查询的结果才会正确显示。就像控制器中的会话没有更新一样。@AmitBera,对我来说,$block
返回null。
public function estimateAjaxPostAction()
{
$country = (string) $this->getRequest()->getParam('country_id');
$postcode = (string) $this->getRequest()->getParam('estimate_postcode');
$city = (string) $this->getRequest()->getParam('estimate_city');
$regionId = (string) $this->getRequest()->getParam('region_id');
$region = (string) $this->getRequest()->getParam('region');
$this->_getQuote()->getShippingAddress()
->setCountryId($country)
->setCity($city)
->setPostcode($postcode)
->setRegionId($regionId)
->setRegion($region)
->setCollectShippingRates(true);
$this->_getQuote()->save();
$response = array();
$response['shipping']=$this->eastmatesajax();
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
}
protected function eastmatesajax()
{
$layout=$this->getLayout();
$layout->getMessagesBlock()->setMessages(Mage::getSingleton('checkout/session')->getMessages(true),Mage::getSingleton('catalog/session')->getMessages(true));
$block = $this->getLayout()->createBlock('checkout/cart_shipping')->setTemplate( 'checkout/cart/shipping.phtml');
return $block->toHtml();
}