2017-06-09 97 views
1

我有一個問題試圖做一些計算與woocommerce_before_cart_table自定義值,並將其傳遞給woocommerce_cart_calculate_fees。問題是,woocommerce_before_cart_table加載第一,我不能夠通過該值從woocommerce_before_cart_tablewoocommerce_cart_calculate_fees鉤......woocommerce_cart_calculate_fees加載後woocommerce_before_cart_table

add_action('woocommerce_before_cart_table', 'my_custom_cart_items_raw_output'); 
function my_custom_cart_items_raw_output() { 
    //$sum = array(); 
    foreach(WC()->cart->get_cart() as $cart_item_key => $item_values){ 

     global $wpdb; 

     $mylink = $wpdb->get_row("SELECT * FROM tax_hotel_and_name WHERE hotel_id= '".$item_values['st_booking_data']['room_id']."'"); 
     $x = $mylink->tax_percentage; 
     // Outputting the raw Cart items data to retrieve Bookings related data 
     $y = $item_values['st_booking_data']['total_price']; 

     $sum[]= ($x/100) * $y; 
      ($x/100) * $y.'<br>'; 
    // echo '<pre>'; print_r($item_values); echo '</pre>'; 
    } 
    array_sum($sum); 
} 

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

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

    // $percentage = 0; 
    // $taxes = array_sum($woocommerce->cart->taxes); 
    // $surcharge = ($woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total + $taxes) * $percentage; 
    // // Make sure that you return false here. We can't double tax people! 
    $woocommerce->cart->add_fee('Processing Fee', 12, false, ''); 
} 

我怎麼能傳遞woocommerce_cart_calculate_fees掛鉤,計算?

感謝

回答

2

所以,如果我理解你正在你的第一個功能相關的預訂數據計算的一些測試中woocommerce_before_cart_table大呼過癮。我有侍女在你的代碼爲global $wpdb;變化不大:

add_action('woocommerce_before_cart_table', 'my_custom_cart_items_raw_output'); 
function my_custom_cart_items_raw_output() { 

    global $wpdb; // To be declared only once at the begining before the foreach loop 

    foreach(WC()->cart->get_cart() as $cart_item_key => $item_values){ 

     $mylink = $wpdb->get_row("SELECT * FROM tax_hotel_and_name WHERE hotel_id= '".$item_values['st_booking_data']['room_id']."'"); 
     $x = $mylink->tax_percentage; 
     // Outputting the raw Cart items data to retrieve Bookings related data 
     $y = $item_values['st_booking_data']['total_price']; 

     $sum[] = ($x/100) * $y; 
     // echo ($x/100) * $y.'<br>'; 
     // echo '<pre>'; print_r($item_values); echo '</pre>'; 
    } 
    array_sum($sum); 
    // test output of $sum 
    echo $sum; 
} 

一旦你確定你的計算,因爲車的數據是怎麼樣的生活,你可以在你的第二掛鉤函數中直接使用你的計算代碼woocommerce_cart_calculate_fees動作鉤子,有一些小的變化,因爲這個鉤子有一個本地$cart_object參數。

所以,你的代碼應該是這樣的:

add_action('woocommerce_cart_calculate_fees','woocommerce_custom_surcharge', 10, 1); 
function woocommerce_custom_surcharge($cart_object) { 

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

    ## ---- CALCULATION ---- ## 

    global $wpdb; 

    foreach($cart_object->get_cart() as $values){ 

     $tax_hotel = $wpdb->get_row("SELECT * FROM tax_hotel_and_name WHERE hotel_id= '".$values['st_booking_data']['room_id']."'"); 

     $sum[] = ($tax_hotel->tax_percentage/100) * $values['st_booking_data']['total_price']; 
    } 

    $fee = array_sum($sum); 

    ## ---- APPLYING CALCULATED FEE ---- ## 

    if($fee > 0) 
     $cart_object->add_fee(__('Processing Fee'), $fee, false); 
} 

代碼放在您的活動子主題(或主題)的function.php文件或也以任何插件文件。

此代碼是未經測試,但它應該工作...

+1

一如既往woocommerce大師感謝您的幫助,並是可用的工作完美 – Shaik