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

时间:2015-12-09 11:49:34

标签: php woocommerce shipping

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

  

如果订单的最大重量是500克,并且:如果订单值是< =   89.99美元,如果订单价值为90.00美元,则运费为30.00美元。   $ 178.99,如果订单价值为179.00美元,则运费为32.50美元。 $   269.99,如果订单价值为270.00美元,则运费为35.00美元。 359.99美元,如果订单价值为360.00美元,则运费为37.50美元。   650.00美元,然后运费是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 );
    }

1 个答案:

答案 0 :(得分:0)

最后我解决了它:

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

这是我在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 );
}
相关问题