2013-02-03 21 views
6

HiTôi đang cố gắng để có được giỏ hàng hàng đầu trong woocommerce để cập nhật số lượng và giá cả tự động.woocommerce custom ajax top cart

tôi đã có nó để làm việc chừng mực nào đó bằng cách sử dụng điều này như một mẫu:

http://www.amberweinberg.com/developing-custom-woocommerce-themes/

Vấn đề là tôi cần nó để sử dụng ajax để thay đổi 2 yếu tố chứ không chỉ một,

đây là html tôi đang sử dụng trong các chủ đề fuctions.php nộp

   <div class="cartWrapper"> 
       <a href="#" title="Checkout"> 
        <div id="cartsummary"><p> 
         <span class="cart-bubble cart-contents"><a class="cart-bubble cart-contents"><?php echo sprintf(_n('%d', '%d', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?></a> 
       <?php if($woocommerce->cart->get_cart_url() != ''){ $cart=$woocommerce->cart->get_cart_url();} 
       else {$cart = home_url().'/cart/';}; 
       ?></span> 
        </div> 
       </a> 
       <div id="carttotal"> 
        <div id="cartprice"> 
        <p> 
         <a class="cart-total"><?php echo $woocommerce->cart->get_cart_total() ?></a> 
        </p> 
        </div> 
        <a class="button" href="#" title="Checkout" type="button">View Basket</a> 
       </div> 
      </div> 

và mã để cập nhật tự động giỏ hàng mà không cần refresh:

// Ensure cart contents update when products are added to the cart via AJAX (place the following in functions.php) 
add_filter('add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment'); 

function woocommerce_header_add_to_cart_fragment($fragments) { 
    global $woocommerce; 
    ob_start(); 
    ?> 
    <a class="cart-bubble cart-contents"><?php echo sprintf(_n('%d', '%d', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?></a> 
    <a class="cart-total"><?php echo $woocommerce->cart->get_cart_total() ?></a> 
    <?php 

    $fragments['a.cart-contents a.cart-total'] = ob_get_clean(); 

    return $fragments; 

} 

Vấn đề là trong khi công việc này, nó tạo danh sách tổng số giỏ hàng và mặt hàng trong giỏ hàng mà tôi phải ẩn bằng cách sử dụng oveflow css style: hidden trên phần tử liên quan. Có lẽ điều này là bởi vì tôi đã mã hóa các yếu tố ajax sai, bất cứ ai có thể chỉ cho tôi đi đúng hướng?

Cảm ơn

Trả lời

14

Hãy thử điều này:

functions.php

add_filter('add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment'); 

function woocommerce_header_add_to_cart_fragment($fragments) 
{ 
    global $woocommerce; 
    ob_start(); ?> 

    <a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> <?php echo $woocommerce->cart->get_cart_total(); ?></a> 

    <?php 
    $fragments['a.cart-contents'] = ob_get_clean(); 
    return $fragments; 
} 

giỏ hàng Mã số:

<div class="header_cart"> 
    <h5><a href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php _e('Shopping Cart', 'home-shopper'); ?></a></h5> 
    <div class="cart_contents"> 
     <a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> <?php echo $woocommerce->cart->get_cart_total(); ?></a> 
    </div> 
</div> 
+0

Tình yêu nó, các công trình lớn. Cảm ơn. – acorncom