考虑到购物车的重量和价格,将运费添加到购物车

Add shipping Rate to cart considering both weight and price of the cart

想在 woo commerce 中使用代码添加运费。这是我的要求。

If maximum weight of the order is 500 grams and: if order value is <= .99 , then shipping charge is .00 if order value is .00 > 8.99,then shipping charge is .50 if order value is 9.00 > $ 269.99,then shipping charge is .00 if order value is 0.00 > 9.99,then shipping charge is .50 if order value is 0.00 > 0.00,then shipping charge is .00

If maximum weight of the order is more than or equals to 500 grams and: if order value is more than 0.00,then shipping charge is

那么,如何根据我在下面的代码中尝试的上述要求添加自定义运费

但是会导致网站空白

add_action('woocommerce_before_cart_table', 'calculate_shipping');



function calculate_shipping( $package ) {
        global $woocommerce;

            if($woocommerce->cart->cart_contents_weight > 500) {
                if($woocommerce->cart->subtotal >= 89){
                    $cost = 30; 
                }
                if($woocommerce->cart->subtotal >= 90 && $woocommerce->cart->subtotal <= 178 ){
                    $cost = 30; 
                }
                if($woocommerce->cart->subtotal >= 179 && $woocommerce->cart->subtotal <= 269){
                    $cost = 30; 
                }
                if($woocommerce->cart->subtotal >= 270 && $woocommerce->cart->subtotal <= 359){
                    $cost = 30; 
                }
                if($woocommerce->cart->subtotal >= 360 && $woocommerce->cart->subtotal <= 650){
                    $cost = 30; 
                }

            }else{
                if($woocommerce->cart->subtotal >= 650){
                    $cost = 70; 
                }
          }

    $rate = array(
        'id' => $this->id,
        'label' => $this->title,
        'cost' => $cost,
        'calc_tax' => 'per_order'
    );

    $this->add_rate( $rate );
    }

最后我解决了:

通过添加新的自定义统一费率并隐藏默认统一费率。

这是我在 function.php 文件中添加的函数

add_action( 'woocommerce_flat_rate_shipping_add_rate', 'add_another_custom_flat_rate', 10, 2 );

function add_another_custom_flat_rate( $method, $rate ) {
    $new_rate          = $rate;
    $new_rate['id']    .= ':' . 'custom_rate_name';
    $new_rate['label'] = 'Shipping and handling'; 

global $woocommerce;
//echo 'weight'.$woocommerce->cart->cart_contents_weight;
//echo 'price'.$woocommerce->cart->subtotal;
            if($woocommerce->cart->cart_contents_weight <= 500) {
                if($woocommerce->cart->subtotal >= 89){
                    $cost = 30; 
                }
                if($woocommerce->cart->subtotal >= 90 && $woocommerce->cart->subtotal <= 178 ){
                    $cost = 30; 
                }
                if($woocommerce->cart->subtotal >= 179 && $woocommerce->cart->subtotal <= 269){
                    $cost = 30; 
                }
                if($woocommerce->cart->subtotal >= 270 && $woocommerce->cart->subtotal <= 359){
                    $cost = 30; 
                }
                if($woocommerce->cart->subtotal >= 360 && $woocommerce->cart->subtotal <= 650){
                    $cost = 30; 

                }
                if($woocommerce->cart->subtotal >= 650){
                    $cost = 70; 
                }

            }else{
                if($woocommerce->cart->subtotal >= 650){
                    $cost = 70; 
                }
          }
    $new_rate['cost']  = $cost; // Add  to the cost
    $method->add_rate( $new_rate );
}