woocommerce_cart_calculate_fees 在 woocommerce_before_cart_table 之后加载

woocommerce_cart_calculate_fees load after woocommerce_before_cart_table

我在尝试使用 woocommerce_before_cart_table 自定义值进行一些计算并将它们传递给 woocommerce_cart_calculate_fees 时遇到问题。问题是 woocommerce_before_cart_table 首先加载,我无法将该值从 woocommerce_before_cart_table 传递到 woocommerce_cart_calculate_fees hook…

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 挂钩上传递该计算?

谢谢

因此,据我所知,您正在 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 );
}

代码进入您的活动子主题(或主题)的 functions.php 文件或任何插件文件。

此代码未经测试,但应该可以工作……