2015-06-20 97 views
3

如何獲取單選按鈕組,以更新WooCommerce結帳頁上選中的結算總計中列出的Surchage,然後選擇其中一個。單選按鈕A的選擇需要增加0.05%作爲附加,因爲單選按鈕B將增加0.10%。WooCommerce基於結賬中的單選按鈕的動態Surchage

我知道我可以添加靜態收費有以下:

add_action('woocommerce_cart_calculate_fees','woocommerce_custom_surcharge'); 
function woocommerce_custom_surcharge() { 
global $woocommerce; 

if (is_admin() && ! defined('DOING_AJAX')) 
    return; 

// $percentage is what will change based on which button is selected A or B 
// however this is not executed after the page is loaded 
$percentage = 0.05; 

$surcharge = ($woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total) * $percentage;  
$woocommerce->cart->add_fee('Service Fee', $surcharge, true, 'standard'); 

} 

回答

1

所以我通過AJAX解決了這個問題,處理按鈕更改並調用設置WC-> session-> set變量的PHP回調函數。然後在AJAX中發佈一個location.reload,強制functions.php重新加載。在我的收費過濾器中,我檢查了會話變量,並根據會話值添加了費用。

0

也許這樣的事情會讓你開始。我不認爲它會自動添加費,但我沒有測試它,它只是一個想法:

jQuery(document).ready(function($){ 

    if (typeof wc_checkout_form === 'undefined' ||) { 
     return false; 
    } 

    $checkout_form = $('form.checkout'); 
    $checkout_form.on('change', '.your_radio_buttons_class', function(){ 
     wc_checkout_form.wc_checkout_form(); 
    }); 

}); 

我相信你會需要利用checkout script,所以你要確保你的自定義腳本在checkout.js之後加載。