How to add custom shipping cost in woocommerce?

I want to add shipping cost using code in woocommerce. here are my recommendations.

If my shipping country is Australia, the shipping cost is different and outside of Australia also different. now if my delivery country is Australia and

1. if order value is < 100, then shipping charge is $100 2. if order value is > 100, then shipping charge is $0. 

If my delivery country is outside of Australia and

  1. if order value is < 500, then shipping charge is $60 2. if order value is > 500 and < 1000, then shipping charge is $50 3. if order value is > 1000, then shipping charge is $0 

So, how can I add custom shipping cost in accordance with my above requirements when the user changes the country of delivery from the checkout page. I tried the code below, but it only works on the order value, how can I add the delivery country to the code below in the user plugin.

 class WC_Your_Shipping_Method extends WC_Shipping_Method { public function calculate_shipping( $package ) { global $woocommerce; if($woocommerce->cart->subtotal > 5000) { $cost = 30; }else{ $cost = 3000; } } $rate = array( 'id' => $this->id, 'label' => $this->title, 'cost' => $cost, 'calc_tax' => 'per_order' ); // Register the rate $this->add_rate( $rate ); 

}

+7
php wordpress woocommerce
source share
2 answers

It is better to make your own plug-in for shipping, where you can use the hook.
First add the class "WC_Your_Shipping_Method" to your custom plugin and execute the function as follows:

 public function calculate_shipping( $package ) { session_start(); global $woocommerce; $carttotal = $woocommerce->cart->subtotal; $country = $_POST['s_country']; //$package['destination']['country']; if($country == 'AU') { if($carttotal > 100){ $cost = 5; }else{ $cost = 10;//10.00; } } else { if($carttotal < 500){ $cost = 60;//60.00; }else if($carttotal >= 500 && $carttotal <= 1000){ $cost = 50;//50.00; }else if($carttotal > 1000){ $cost = 0; } } $rate = array( 'id' => $this->id, 'label' => 'Shipping', 'cost' => $cost, 'calc_tax' => 'per_order' ); // Register the rate $this->add_rate( $rate ); } 
+7
source share

first make the delivery method in administrator name as "myship"

then add below code to your theme functions.php file

 add_action('woocommerce_before_cart_table', 'discount_when_produts_in_cart'); function discount_when_produts_in_cart( ) { global $woocommerce; $coupon_code = 'myship'; if( $woocommerce->cart->get_cart_total() > 500 ) { $coupon_code = 'myship'; } else { $woocommerce->cart->remove_coupons(sanitize_text_field($coupon_code)); $woocommerce->clear_messages(); } 
0
source share

All Articles