【问题标题】:Update Woocommerce cart after adding a product (jQuery)添加产品后更新 Woocommerce 购物车 (jQuery)
【发布时间】:2012-12-05 12:12:35
【问题描述】:

Ajax 似乎工作正常,但购物车内容不会按预期刷新。我希望单击“添加到购物车”按钮后刷新购物车的内容。 就像现在一样,我必须手动刷新页面才能看到添加的产品。

我正在使用此功能将产品添加到我的 woocommerce 购物车:

      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;
    });  

添加产品后是否可以只刷新购物车?

【问题讨论】:

  • 您必须为该产品生成一个新的 html 并将其附加到 html。还可以更改购物车中的物品数量和其他一些东西。尝试搜索生成购物车的 HTML 并将其翻译成 javascript。这样做或通过字符串 concat,或通过 javascript 模板(太酷了)。

标签: javascript jquery woocommerce


【解决方案1】:

这是可能的 - 您需要使用此代码的修改版本:

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

编辑 - 以防将来出现 404

<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;

}

【讨论】:

  • 您能否对修改更准确一点?例如,我可以使用“add_to_cart_fragments”获取商品数据吗?
【解决方案2】:

您是否介意 ajax 请求“自动”自行刷新页面?那么你可以尝试这样的事情......(未经测试)。

 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'*/
            }); 
      }

【讨论】:

    【解决方案3】:

    首先观察购物车中的 html 是什么,以及您需要哪些信息来完成它。例如,您的购物车可能如下所示:

    <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>
    

    现在您的代码应包含以下更改以反映产品:

     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;
    }); 
    

    这将反映在将产品添加到购物车中。如果您感到困惑,请阅读以下链接。 Tutorial 还有 jquery append 和 ajax 函数在 jquery.com 上更详细

    【讨论】:

    • 谢谢。它应该已经是 woocommerce 中处理此问题的功能?当我按下 woocommerce 生成的“添加到购物车”按钮时,无需重新加载页面即可添加产品。我正在寻找这个功能,这样我就可以自己使用它来进行我自己的 woocommerce 定制,而不是重新发明应该已经存在的东西。
    猜你喜欢
    • 1970-01-01
    • 2015-05-03
    • 2015-10-11
    • 1970-01-01
    • 2014-11-21
    • 2015-11-02
    • 1970-01-01
    • 2021-08-22
    • 2018-06-03
    相关资源
    最近更新 更多