2015-06-03 123 views
2

在產品類別頁面,當有人點擊「添加到購物車」時,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

回答

2

如果您從Woocommerce回購看here,您可以看到add-to-cart.js是從該類中本地化的。

不幸的是,沒有過濾器來添加自己的鏈接。您可以嘗試的是將add-to-cart.js複製到您的主題,並使用this method將已註冊的add-to-cart.js的新src設置爲新的本地副本。

從那裏你可以改變在Woocommerce回購中找到的this conditional

因此,技術上是的,你可能會這樣,但也有注意事項:

  • 您將需要重複這個過程變化的產品
  • 如果轉換是一個問題,你需要解決的以及
  • 任何時候插件更新,你現在必須梳理這些文件的任何差異,可能會破壞功能或導致安全問題。
相關問題