【发布时间】:2014-09-16 16:05:04
【问题描述】:
在我的标题中,我已经有一个选项可以显示我的购物车中的商品数量,每次我点击添加到购物车按钮时它都会改变。我也有总金额,但是当我将产品添加到购物车时,这不会加起来。在我的标题中是
<?php if( in_array('woocommerce/woocommerce.php', get_option('active_plugins')) ):?>
<p id="shop_links" class="cart_right"><?php _e('Cart:','theme') ?>
<a class="link_cart" href="<?php echo $woocommerce->cart->get_cart_url(); ?>">
<span>
<?php
echo '<span class="items_count">' . $woocommerce->cart->cart_contents_count . '</span> ' ._n('item', 'items', $woocommerce->cart->cart_contents_count ,'theme') . ' ' . '— ' . $woocommerce->cart->get_cart_total() ;
?>
</span>
<i class="icon-shoppingbag"></i>
</a>
</p>
<?php endif; ?>
我的按钮有 .add_to_cart_ajax 类,适用于购物车中商品数量的代码是
$('.add_to_cart_ajax').click(function(e){
e.preventDefault();
var $this = $(this);
var product_id = $this.data('product_id');
var added_title = $this.data('added_title');
var $shop_links = $('#shop_links');
var $cart_no = $shop_links.find('.link_cart span .items_count');
var $cart_amount = $shop_links.find('.link_cart span .amount');
var currentPrice = $this.parents('li.product').find('.price ins .amount , .price > .amount').text();
var data = {
action: 'theme_add_to_cart_wishlist',
product_id: product_id,
add_to: 'cart'
};
$.post(ajaxurl, data, function(response) {
$cart_no.text(parseInt($cart_no.text())+1);
$cart_amount.text(parseFloat($cart_amount.replace(/[.,](?=.*[.,])/g, "").replace(",", ".").text())+currentPrice.replace(/[.,](?=.*[.,])/g, "").replace(",", "."));
if(!$this.hasClass('in-cart')){
$this.addClass('in-cart').tipsy("hide").attr("original-title",added_title).tipsy("show");
}
});
});
但是$cart_amount 部分没有做我想做的事情(在我点击各种产品时添加价格)。我用console.log 进行了测试,当我点击添加到购物车按钮时,我得到了正确的价格。那我错过了什么?
【问题讨论】:
标签: php jquery wordpress woocommerce