2016-06-28 79 views
1

如果購物車總額低於50美元,我有一小部分代碼可計算購物車總額(不含稅)並輸出免費送貨優惠。WooCommerce - 在Ajax更新後計算購物車總額

// Shipping Upsell 
/** 
* Checks the cart for the Total excluding taxes 
* @param $total_required 
* @return bool 
*/ 
function qualifies_basedon_cart_total($total_required) { 
    /* 
    * We only want to run this on the cart or checkout page 
    * If the cart subtotal is less than $total_required for free shipping, return true 
    */ 
if(is_cart() || is_checkout()) { 
    if(WC()->cart->subtotal_ex_tax < $total_required) { 
     return true; 
    } 
} 
// do nothing 
return false; 
} 

function shipup(){ 
    // tests if total is below $50 and displays upsell if query returns ture 
    if(qualifies_basedon_cart_total(50)) { 
    echo "<div class =\"shipup\"><h3>Free Shipping</h3>\n<p>Get free shipping on all orders over $50!</p></div>"; 
    } 
} 

add_action ('woocommerce_cart_collaterals','shipup', 1); 

代碼在初始頁面加載上面的偉大工程,但改變車頁面的數量並選擇「更新購物車」我有以上(在functions.php中)的代碼基於不調整後本身新購物車總數。 enter image description here

我相信更新購物車按鈕使用AJAX,我的代碼無法使用它。如何AJAXIFY我的代碼,以便它的基礎上動態購物車總計?

回答

0

如果您使用的是WooCommerce 2.6或更高版本,WC已經創建了一種方法來做到這一點,而無需額外的代碼。

您需要在「WooCommerce」 - >「設置」 - >「發貨」 - >「運輸區域」下設置「運輸區域」。

例如,我有「本地」和「美國」運輸區。

  1. 對於「美」航運區,然後我創建了一個「航運法」(點擊區增加了送貨方式,然後點擊標題爲「添加運輸方式」的灰色按鈕)。
  2. 選擇「免運費」方式並點擊「添加送貨方式」。
  3. 然後點擊「免費送貨」方法下的「設置」。
  4. 此時,您可以選擇適用免費送貨方式。使用「Free Shipping Requires ...」下拉菜單並選擇「A minimum order amount」,並在「Minimum Order Amount」字段中輸入「50」。
  5. 然後點擊「保存更改」。

現在當購物車的總額是50美元或更多,然後免費送貨將可用。

1
<?php 

/* Update cart total on quantity change */ 

function cart_select_change_js() { 
?> 
    <script type="text/javascript"> 
     jQuery(document).ready(function($){ 
      $(".product-quantity .quantity_select select.qty").change(function(){ 
       $("section.entry .woocommerce").append('<div style="display:none" class="blockUI"></div>'); 
       $("section.entry .woocommerce").append('<div style="z-index: 1000; border: medium none; margin: 0px; padding: 0px; width: 100%; height: 100%; top: 0px; left: 0px; background: url(&quot;/wp-content/plugins/woocommerce/assets/images/[email protected]&quot;) no-repeat scroll center center/16px 16px rgb(255, 255, 255); opacity: 0.6; cursor: wait; position: absolute;" class="blockUI blockOverlay"></div>'); 
       $(".actions input.button").click(); 
      }); 
     }); 
    </script> 
<?php 
} 
add_action('woocommerce_after_cart', 'cart_select_change_js', 10); 
?> 

試試這個代碼片斷

+0

請,這是撿來的代碼沒有任何變化。這裏是你的鏈接,包括更新你的答案:https://gist.github.com/rours/637b722debac38037766;你有沒有測試過它?如果沒有,也可以說未經測試。 – LoicTheAztec

相關問題