【发布时间】:2016-05-31 09:03:07
【问题描述】:
我有一家 Magento 商店,我需要在向其中添加产品后更新侧边栏购物车。
但我无法让它工作......我尝试了很多东西,但我没有选择。
我有这个功能:
function setLocationAjax(url, id)
{
url = url.replace("checkout/cart", "ajax/index");
url += 'isAjax/1/';
if ('https:' == document.location.protocol) {
url = url.replace('http:', 'https:');
}
$('#ajax_loading' + id).css('display', 'block');
try {
$.ajax({
url:url,
dataType:'jsonp',
success:function (data) {
$('#ajax_loading' + id).css('display', 'none');
showMessage(data.message);
if (data.status != 'ERROR' && $('.cart_sidebar_container').length) {
$('.cart_sidebar_container').replaceWith(data.cart_top);
}
}
});
} catch (e) {
}
}
这是 HTML/PHP 代码:
<?php $_cartQty = $this->getSummaryCount(); if ( is_null($_cartQty) ) $_cartQty = 0; ?>
<?php $params = $this->getRequest()->getParams(); if(empty($params['isAjax'])) : ?>
<div class="cart-top-title"><a href="<?php echo $this->getUrl('checkout/cart') ?>" class="clearfix"><span class="icon"></span><?php echo $this->__('Shopping cart'); ?></a></div>
<?php endif; ?>
<div class="cart_sidebar_container">
<div class="cart-top">
<a class="summary" href="<?php echo $this->getUrl('checkout/cart') ?>"></a>
<span><?php echo $this->__('BAG').' ('.$_cartQty.')'; ?></span>
</div>
<div class="details details-sidebar">
<div class="details-border"></div>
<?php $_items = $this->getRecentItems() ?>
<?php if(count($_items)): ?>
<p class="block-subtitle text-recently"><?php echo $this->__('Recently added item(s)') ?></p>
<ol id="cart-sidebar" class="mini-products-list">
<?php foreach($_items as $_item): ?>
<?php echo $this->getItemHtml($_item) ?>
<?php endforeach; ?>
</ol>
<div class="subtotal-wrapper">
<div class="subtotal">
<?php if ($this->canApplyMsrp()): ?>
<span class="map-cart-sidebar-total"><?php echo $this->__('ORDER TOTAL WILL BE DISPLAYED BEFORE YOU SUBMIT THE ORDER'); ?></span>
<?php else: ?>
<span class="label"><?php echo $this->__('Cart Subtotal:') ?></span> <?php echo Mage::helper('checkout')->formatPrice($this->getSubtotal()) ?>
<?php if ($_subtotalInclTax = $this->getSubtotalInclTax()): ?>
<br />(<?php echo Mage::helper('checkout')->formatPrice($_subtotalInclTax) ?> <?php echo Mage::helper('tax')->getIncExcText(true) ?>)
<?php endif; ?>
<?php endif; ?>
</div>
</div>
<div class="buttons">
<div class="button_wrap">
<button type="button" title="<?php echo $this->__('View Cart') ?>" class="button btn-continue" onclick="setLocation('<?php echo $this->getUrl('checkout/cart') ?>')"><span><span><?php echo $this->__('View Cart') ?></span></span></button>
</div>
<?php if($_cartQty && $this->isPossibleOnepageCheckout()): ?>
<?php echo $this->getChildHtml('extra_actions') ?>
<div class="button_wrap">
<button type="button" title="<?php echo $this->__('Checkout') ?>" class="button btn-checkout" onclick="setLocation('<?php echo $this->getCheckoutUrl() ?>')"><span><span><?php echo $this->__('Continue to Checkout') ?></span></span></button>
</div>
<?php endif ?>
</div>
<?php else: ?>
<p class="a-center"><?php echo $this->__('You have no items in your shopping cart.') ?></p>
<?php endif ?>
</div>
有人可以帮我解决这个问题吗?
我需要重新加载当前代码,现在它正在用另一个小 div 替换 div,它在悬停时加载购物车。
谢谢!
问候, 罗伯特
【问题讨论】:
-
$('.cart_sidebar_container').replaceWith(data.cart_top);你能贴出生成data的代码吗? -
@nagyben 我在下面添加了一些代码
-
不清楚你在问什么 - 你想达到什么目的?
-
我在网站的标题中有一个购物车,当您将鼠标悬停在一个图标上时我可以看到它,当您将某些东西添加到购物车时,它会使用 ajax 更新。我想要在页面左侧的同一个购物车,但不是悬停,但始终可见。我在这里粘贴的代码来自标题中的购物车。我想要的是页面左侧的购物车,当您向其中添加内容时它会更新(通过 ajax)。 :)
标签: javascript jquery ajax magento cart