当使用magento中的ajax添加到购物车时,无法渲染迷你购物车(顶部标题购物车)价格

时间:2015-01-28 16:43:19

标签: ajax magento shopping-cart product

  

我创建了自定义产品价格页面,还创建了自定义模板   和自定义模板文件。我已经覆盖了cartController和   编辑addAction()方法覆盖。

     

我的代码问题:添加到购物车时   返回响应数据和我设置或添加时使用ajax的产品   minicart中的产品信息('当标题为“购物车”链接时)   悬停显示购物车信息')数据但产品名称设置但价格   没有设置。它的显示价格为零。标题购物中未设置价格   购物车内容。

Below my addAction() code.
 public function addAction()
        {
            $cart   = $this->_getCart();
            $params = $this->getRequest()->getParams();
            try {
                if (isset($params['qty'])) {
                    $filter = new Zend_Filter_LocalizedToNormalized(
                        array('locale' => Mage::app()->getLocale()->getLocaleCode())
                    );
                    $params['qty'] = $filter->filter($params['qty']);
                }

                $product = $this->_initProduct();
                $related = $this->getRequest()->getParam('related_product');

                /**
                 * Check product availability
                 */
                if (!$product) {
                    $this->_goBack();
                    return;
                }

                $cart->addProduct($product, $params);
                if (!empty($related)) {
                    $cart->addProductsByIds(explode(',', $related));
                }

                $cart->save();

                $this->_getSession()->setCartWasUpdated(true);

                /**
                 * @todo remove wishlist observer processAddToCart
                 */
                Mage::dispatchEvent('checkout_cart_add_product_complete',
                    array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse())
                );

                if (!$this->_getSession()->getNoCartRedirect(true)) {
                    if (!$cart->getQuote()->getHasError()) {
                        $message = $this->__('%s was added to your shopping cart.', Mage::helper('core')->escapeHtml($product->getName()));
                        $this->_getSession()->addSuccess($message);
     $response['status'] = 'SUCCESS';
                            $response['message'] = $message;
    //My Custom Code start
                            $this->loadLayout();                
            $sidebar = $this->getLayout()->getBlock('cart_sidebar')->toHtml();
                            $response['toplink'] = $toplink;
                            $response['sidebar'] = $sidebar;
     $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
                return;
                    }
                    //$this->_goBack();
//My Custom Code end
                }
            } catch (Mage_Core_Exception $e) {
                if ($this->_getSession()->getUseNotice(true)) {
                    $this->_getSession()->addNotice(Mage::helper('core')->escapeHtml($e->getMessage()));
                } else {
                    $messages = array_unique(explode("\n", $e->getMessage()));
                    foreach ($messages as $message) {
                        $this->_getSession()->addError(Mage::helper('core')->escapeHtml($message));
                    }
                }

                $url = $this->_getSession()->getRedirectUrl(true);
                if ($url) {
                    $this->getResponse()->setRedirect($url);
                } else {
                    $this->_redirectReferer(Mage::helper('checkout/cart')->getCartUrl());
                }
            } catch (Exception $e) {
                $this->_getSession()->addException($e, $this->__('Cannot add the item to shopping cart.'));
                Mage::logException($e);
                $this->_goBack();
            }
        }

在我的自定义产品价格页面中,我在下面写了ajax代码。

  jQuery.ajax({
                              url: "example.com/checkout/cart/add/product_id/2",
                              type : 'post',
                              success: function(data){
    var response = data.evalJSON(true);                                
    jQuery('#ajax_loader').hide();
    jQuery('.top-cart-content').html(response.sidebar);
                              }
                        });

1 个答案:

答案 0 :(得分:0)

您是否尝试在控制器中创建此块的新实例,而不是从现有布局中获取块?