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

wqlqzqxt  于 2023-01-12  发布在  PHP
关注(0)|答案(1)|浏览(197)

想添加运费使用吴宇商务代码.这里是我的要求.
如果订单的最大重量为500克,并且:如果订单价值〈= $89.99,则如果订单价值为$90.00〉$178.99,则运输费用为$30.00,如果订单价值为$179.00〉$269.99,则运输费用为$32.50,如果订单价值为$270.00〉$359.99,则运输费用为$35.00,如果订单价值为$360.00〉$650.00,则运费为$37.50,运费为$40.00
如果订单的最大重量大于或等于500克,并且:如果订单价值超过$650.00,则运费为$70
所以,我怎么能添加自定义运费根据我的上述要求,我尝试了下面的代码
但它导致网站空白。

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 );
    }
3bygqnnd

3bygqnnd1#

最后我解决了:
通过添加新的自定义统一费率并隐藏默认统一费率。
下面是我在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 $2 to the cost
    $method->add_rate( $new_rate );
}

相关问题