在 WooCommerce 中乘以运费

问题描述

我尝试根据订单中的托盘数量成倍增加运费。我认为过滤器是错误的什么的。它只是不会改变运费。

$calc = ceil($a+$b / 8); $a > 是托盘的数量,$b > 是计算托盘数量的单位数量

我的代码(function.PHP):

add_filter( 'woocommerce_cart_shipping_total','woocommerce_cart_shipping_total_filter_callback',11,2 );
function woocommerce_cart_shipping_total_filter_callback( $total,$cart )
{
     if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( 0 < $cart->get_shipping_total() ) {
        if ( $cart->display_prices_including_tax() ) {
            $a = 0;
            $b = 0;
            foreach( WC()->cart->get_cart() as $cart_item ){
                if (!empty(get_post_meta($cart_item['variation_id'],'_number_field',true))) {
                    $a += $cart_item['quantity'];
                } else {
                    $atk = strstr(get_post_meta($cart_item['variation_id'],'_alus_al',true),'tk',true);
                    
                    $b += ceil($cart_item['quantity'] / $atk);
                }
            }
            $calc = ceil($a+$b / 8); // 8 pallets maximum
            $total = wc_price( ( $cart->shipping_total + $cart->shipping_tax_total ) * $calc );
            if ( $cart->shipping_tax_total > 0 && ! wc_prices_include_tax() ) {
                $total .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
            }
        } else {
            $a = 0;
            $b = 0;
            foreach( WC()->cart->get_cart() as $cart_item ){
                if (!empty(get_post_meta($cart_item['variation_id'],true);
                    
                    $b += ceil($cart_item['quantity'] / $atk);
                }
            }
            $calc = ceil($a+$b / 8); // 8 pallets maximum
            $total = wc_price( $cart->shipping_total * $calc );
            if ( $cart->shipping_tax_total > 0 && wc_prices_include_tax() ) {
                $total .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat()  . '</small>';
            }
        }
    }
    return  $total;
}

我的问题是它不会改变运费。所以我希望它不起作用。

解决方法

就像用户@LoicTheAztec 提到的,我应该使用 woocommerce_package_rates 来覆盖运输插件的自定义价格。

add_filter( 'woocommerce_package_rates','custom_shipping_costs',20,2 );
function custom_shipping_costs( $rates,$package ) {
    $a = 0;
    $b = 0;
    foreach( WC()->cart->get_cart() as $cart_item ){
        if (!empty(get_post_meta($cart_item['variation_id'],'_number_field',true))) {
            $a += $cart_item['quantity'];
        } else {
            $atk = strstr(get_post_meta($cart_item['variation_id'],'_alus_al',true),'tk',true);
            $atb = $atk / 2;
            if ($cart_item['quantity'] < $atb) {
                $b += 0;
            } else {
                $b += ceil($cart_item['quantity'] / $atk);
            }
        }
    }
    $tt = $a+$b;
    $calc = ceil($tt / 8);

    foreach( $rates as $rate_key => $rate ){
        // Excluding free shipping methods
        if( $rate->method_id != 'free_shipping'){
            // Set rate cost
            $rates[$rate_key]->cost = $rates[$rate_key]->cost * $calc;

            // Set taxes rate cost (if enabled)
            $taxes = array();
            foreach ($rates[$rate_key]->taxes as $key => $tax){
                if( $rates[$rate_key]->taxes[$key] > 0 )
                    $taxes[$key] = $new_cost * $tax_rate;
            }
            $rates[$rate_key]->taxes = $taxes;

        }
    }
    return $rates;
}