我有一些 url 我将一些项目添加到另一个页面(购物车页面),但是通过刷新页面并且它工作正常,现在我尝试用Ajax做,所以我创建了一个ajax函数调用此URL它可以工作,它会添加我的项目。
现在问题是我必须重新加载其他页面(购物车页面)才能看到该产品。那么如何在添加项目后刷新此页面,我的意思是在调用此URL之后。
网址:<?php echo Mage::helper('checkout/cart')->getAddUrl($_product, $params); ?>
cart.phtml
...
<div class="row">
<div class="col-md-6">
<div class="product-qty">
<div class="custom-qty">
<div class="btn-plus">
<button type="button" class="reduced items" onclick="minusQty('<?php echo $_product->getId() ?>')">
<i class="fa fa-minus"></i>
</button>
<input name="qty" id="qty-<?php echo $_product->getId()?>" maxlength="12" value="1" title="Qté" class="input-text qty" type="text">
<button type="button" class="increase items" onclick="plusQty('<?php echo $_product->getId() ?>')">
<i class="fa fa-plus"></i>
</button>
</div>
</div>
</div>
</div>
<?php $params = array('qty'=>2) // the input value ?>
<div class="col-md-6">
<button type="button" title="<?php echo $this->__('Add') ?>" class="button btn-cart pull-left-none" onclick="addToCartAjax('<?php echo Mage::helper('checkout/cart')->getAddUrl($_product, $params); ?>')" >
<span><span><?php echo $this->__('Add') ?></span></span>
</button>
</div>
</div>
//JS PART
<script type="text/javascript">
function addToCartAjax(addUrl) {
jQuery.ajax ({
url: addUrl,
type : 'GET',
})
}
</script>
修改
cart.phtml
<div class="row">
<div class="col-main col-lg-12">
<div class="cart">
<fieldset>
...
<tbody>
<tr class="first odd"><td>Item1</td></tr>
<tr class="last even"><td>Item2</td></tr>
</tbody>
...
</fieldset>
</div>
</div>
</div>
EDIT2:
动作控制器
public function addAction()
{
if (!$this->_validateFormKey()) {
$this->_goBack();
return;
}
$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);
}
$this->_goBack();
}
} 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();
}
}
答案 0 :(得分:1)
我假设当您向购物车添加商品时,您会从服务器获得有关购物车当前商品的某种响应,如果不是,您必须首先在服务器端实施该商品
将此添加到您的函数中:
function addToCartAjax(addUrl){
$.ajax({
url: addUrl,
type: 'GET',
})
.done(function(response ) {
//response - this is the response from the server
});
}
我不确切知道你的服务器返回的是什么,如果它是html而不是.done关闭这样做:
.done(function(response ) {
// response - this is the response from the server
$('.YourCartContainer').html(response);
});
所以如果你的服务器返回一个包含你的购物车项目的html片段,你应该把它作为html插入你的购物车div容器中,但是如果服务器返回带有项目的Json响应,你必须使用jquery和format循环它们这里是一个例子,但是我不知道你的服务器返回了什么,所以只需应用这个技术:
.done(function(response ) {
// response - this is the response from the server
var htmlResult = '';
$.each(response, function( item ) {
htmlResult += '<p>'+ item.name + '</p><br>'
});
$('.YourCartContainer').html(htmlResult);
});
所以在这里我通过json循环并创建了一些简单的html,你可以获得购物车内的所有商品名称,希望这有帮助!如果您需要有关您的案例的更多信息,请发布服务器响应的内容!!