2012-12-18 46 views
12

Ajax iyi çalışıyor gibi görünüyor, ancak sepet içeriği beklendiği gibi yenilenmiyor. "Sepete ekle" düğmesine tıklandığında sepetin içindekilerin yenilenmesini istiyorum. Şu an olduğu gibi, eklenen ürünleri görmek için sayfayı manuel olarak yenilemeliyim.Bir ürün ekledikten sonra Woocommerce sepeti (jQuery)

benim WooCommerce sepetine ürün eklemek için bu işlevi kullanıyorum:

 function addToCart(p_id) { 
      jQuery.ajax({ 
       type: 'POST', 
       url: '/wp/?post_type=product&add-to-cart='+p_id, 
       data: { 'product_id': p_id, 
       'quantity': amount}, 
       success: function(response, textStatus, jqXHR){ 
        console.log("Product added"); 
       }/*, 
       dataType: 'JSON'*/ 
      }); 
     } 

    jQuery('#addToCart').click(function(e) { 
     e.preventDefault(); 
     addToCart(prod_id["product_id"]); 
     return false; 
    }); 

bir ürün eklendikten sonra sadece arabasını yenilemek mümkün mü? -

+0

Endişeye gerek yok - teşekkürler! – user319940

+0

Bu ürün için yeni bir html oluşturmalı ve bunu html'ye eklemelisiniz. Ayrıca, sepette kaç öğe olduğunu ve diğer bazı şeyleri de değiştirebilirsiniz. Sepeti oluşturan HTML'yi aramayı ve javascript'e çevirmeyi deneyin. Bunu veya dize concat ile veya javascript şablonları ile (çok güzel) yapın. – ioanb7

cevap

7

bu mümkündür, bu kodun değiştirilmiş bir sürümünü kullanmanız gerekir:

http://docs.woothemes.com/document/show-cart-contents-total/

Düzen - sakıncası var mı gelecek 404 en

<a class="cart-customlocation" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e('View your shopping cart'); ?>"><?php echo sprintf (_n('%d item', '%d items', WC()->cart->get_cart_contents_count()), WC()->cart->get_cart_contents_count()); ?> - <?php echo WC()->cart->get_cart_total(); ?></a> 

// Ensure cart contents update when products are added to the cart via AJAX (place the following in functions.php). 
// Used in conjunction with https://gist.github.com/DanielSantoro/1d0dc206e242239624eb71b2636ab148 
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-customlocation" 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-customlocation'] = ob_get_clean(); 

    return $fragments; 

} 
+1

Değişiklik hakkında biraz daha hassas olabilir misiniz? Örneğin, öğe verilerini 'add_to_cart_fragments' ile alabilir miyim? – Luc

+0

bağlantı hatası 404 – Reigel

+0

http://docs.woothemes.com/document/show-cart-contents-total/ –

1

durumunda ise ajax isteği "otomatik", sayfayı kendi başına yeniler mi? o zaman böyle bir şey deneyebilirsiniz ... (test edilmemiş).

function addToCart(p_id) { 
      jQuery.ajax({ 
       type: 'POST', 
       url: '/wp/?post_type=product&add-to-cart='+p_id, 
       data: { 'product_id': p_id, 
       'quantity': amount}, 
       success: function(response, textStatus, jqXHR){ 
        location.reload(true); 
       }/*, 
       dataType: 'JSON'*/ 
      }); 
     } 
0

İlk önce, sepetteki html'nin ne olduğunu ve bunu yerine getirmek için ihtiyacınız olan tüm bilgileri öğrenin. Örnek sepetinize şöyle görünebilir:

<div id="cart"> 
    <form action="checkout" method="POST" id="cart_checkout_form"> 
    <table id="cart_table"> 
     <tr> 
     <td> Product </td> 
     <td> Qty </td> 
     <td> Price </td> 
     </tr> 
     <tr> 
     <td> <input type="hidden" name="product_id" value="221321"/> Product 1</td> 
     <td> 2</td> 
     <td> 200 $</td> 
     </tr> 

    </table> 

    </form> 
</div> 

Şimdi kod ürünü yansıtmalıdır aşağıdaki değişiklikleri içermelidir:

function addToCart(p_id) { 
     jQuery.ajax({ 
      type: 'POST', 
      url: '/wp/?post_type=product&add-to-cart='+p_id, 
      data: { 'product_id': p_id, 
      'quantity': amount}, 
      success: function(response, textStatus, jqXHR){ 
       /* response should contain details required for adding to cart 

        like price quantity name etc in the json response */ 

       var new_product_html = "<tr><td><input type='hidden' value='"+ response.product_id +"' name="product_id"/>"+ response.product_name +" </td><td>"+response.product_qty+"</td><td>"+ response.product_price +"</td></tr>"; 

       $("#cart_table").append(new_product_html); 

       console.log("Product added"); 

      }/*, 
      dataType: 'JSON'*/ 
     }); 
    } 

jQuery('#addToCart').click(function(e) { 
    e.preventDefault(); 
    addToCart(prod_id["product_id"]); 
    return false; 
}); 

Bu sepetinize eklendi ürünü almakla yansıtacaktır. Kafa karıştırıcı bulursanız lütfen aşağıdaki linkleri okuyun. Tutorial Ayrıca jquery eki ve ajax işlevleri daha ayrıntılı olarak jquery.com adresinden

+0

Teşekkür ederiz. Zaten bunu ele alan woocommerce içinde bir işlev olmalı? Woocommerce tarafından üretilen "sepete ekle" düğmesine bastığımda, ürün yeniden sayfa eklenmeden eklenir. Bu işlevi arıyorum, bu yüzden zaten var olması gereken bir şeyi yeniden yaratmak yerine, kendi woocommerce özelleştirme için kendim kullanabilirim. – estrar

İlgili konular