Woocommerce“添加到购物车”ajax

问题描述:

在产品类别页面,当有人点击“添加到购物车”时,woocommerce通过Ajax在此按钮下添加了“查看购物车”。我发现处理这个脚本的脚本是/assets/js/frontend/add-to-cart.jsWoocommerce“添加到购物车”ajax

现在,我还想添加“Procceed to checkout”,所以有人可以立即去结帐。

这是脚本的输出:

jQuery(function($) { 

// wc_add_to_cart_params is required to continue, ensure the object exists 
if (typeof wc_add_to_cart_params === 'undefined') 
    return false; 

// Ajax add to cart 
$(document).on('click', '.add_to_cart_button', function(e) { 

// AJAX add to cart request 
var $thisbutton = $(this); 

if ($thisbutton.is('.product_type_simple')) { 

    if (! $thisbutton.attr('data-product_id')) 
     return true; 

    $thisbutton.removeClass('added'); 
    $thisbutton.addClass('loading'); 

    var data = { 
     action: 'woocommerce_add_to_cart', 
    }; 

    $.each($thisbutton.data(), function(key, value) { 
     data[key] = value; 
    }); 

    // Trigger event 
    $('body').trigger('adding_to_cart', [ $thisbutton, data ]); 

    // Ajax action 
    $.post(wc_add_to_cart_params.ajax_url, data, function(response) { 

     if (! response) 
      return; 

     var this_page = window.location.toString(); 

     this_page = this_page.replace('add-to-cart', 'added-to-cart'); 

     if (response.error && response.product_url) { 
      window.location = response.product_url; 
      return; 
     } 

     // Redirect to cart option 
     if (wc_add_to_cart_params.cart_redirect_after_add === 'yes') { 

      window.location = wc_add_to_cart_params.cart_url; 
      return; 

     } else { 

      $thisbutton.removeClass('loading'); 

      fragments = response.fragments; 
      cart_hash = response.cart_hash; 

      // Block fragments class 
      if (fragments) { 
       $.each(fragments, function(key, value) { 
        $(key).addClass('updating'); 
       }); 
      } 

      // Block widgets and fragments 
      $('.shop_table.cart, .updating, .cart_totals').fadeTo('400', '0.6').block({ 
       message: null, 
       overlayCSS: { 
        opacity: 0.6 
       } 
      }); 

      // Changes button classes 
      $thisbutton.addClass('added'); 

      // View cart text 
      if (! wc_add_to_cart_params.is_cart && $thisbutton.parent().find('.added_to_cart').size() === 0) { 
       $thisbutton.after(' <a href="' + wc_add_to_cart_params.cart_url + '" class="added_to_cart wc-forward" title="' + 
        wc_add_to_cart_params.i18n_view_cart + '">' + wc_add_to_cart_params.i18n_view_cart + '</a>'); 
      } 

      // Replace fragments 
      if (fragments) { 
       $.each(fragments, function(key, value) { 
        $(key).replaceWith(value); 
       }); 
      } 

      // Unblock 
      $('.widget_shopping_cart, .updating').stop(true).css('opacity', '1').unblock(); 

      // Cart page elements 
      $('.shop_table.cart').load(this_page + ' .shop_table.cart:eq(0) > *', function() { 

       $('.shop_table.cart').stop(true).css('opacity', '1').unblock(); 

       $('body').trigger('cart_page_refreshed'); 
      }); 

      $('.cart_totals').load(this_page + ' .cart_totals:eq(0) > *', function() { 
       $('.cart_totals').stop(true).css('opacity', '1').unblock(); 
      }); 

      // Trigger event so themes can refresh other areas 
      $('body').trigger('added_to_cart', [ fragments, cart_hash, $thisbutton ]); 
     } 
    }); 

    return false; 

} 

return true; 
}); 

是否有任何人谁做了类似的事情?

+0

将物品添加到购物车后,会出现一个链接,将用户带到购物车。你有什么不同的做法? – helgatheviking

+0

有一个篮子,并有一个结帐。结帐是付款过程中的最后一步 1. http://nanshy.com/basket/ 2. https://nanshy.com/checkout/ 我想立即让用户结帐或篮。所以基本上我只需要在“查看篮子”下添加一个链接 – TomTom

如果您从Woocommerce回购看here,您可以看到add-to-cart.js是从该类中本地化的。

不幸的是,没有过滤器来添加自己的链接。您可以尝试的是将add-to-cart.js复制到您的主题,并使用this method将已注册的add-to-cart.js的新src设置为新的本地副本。

从那里你可以改变在Woocommerce回购中找到的this conditional

因此,技术上是的,你可能会这样,但也有注意事项:

  • 您将需要重复这个过程变化的产品
  • 如果转换是一个问题,你需要解决的以及
  • 任何时候插件更新,你现在必须梳理这些文件的任何差异,可能会破坏功能或导致安全问题。