Title: Programmatically select shipping rule
Last modified: March 23, 2020

---

# Programmatically select shipping rule

 *  Resolved [howy](https://wordpress.org/support/users/howy/)
 * (@howy)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/programmatically-select-shipping-rule/)
 * Hi,
 * I have a weight-based shipping rules and i have to check the size of the products
   in the cart in addition to the weight.
    How can i select the maximum weight rule
   if sum of the dimensions exceed a certain value? in `woocommerce_package_rates`
   i have to unset rules in `method_rules` array or is there a better filter?
 * Thanks
 * WordPress 5.3.2
    WooCommerce 3.9.3 Flexible Shipping 3.9.13

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Plugin Support [Marta Borówka](https://wordpress.org/support/users/martapaw/)
 * (@martapaw)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/programmatically-select-shipping-rule/#post-12577436)
 * Hello [@howy](https://wordpress.org/support/users/howy/),
 * Thank you for your message.
 * For now, this feature is not available, but we are really into providing it to
   our clients because we received much feedback that tells us that it could be 
   a useful feature for admins. So I think it could be released very soon, and of
   course, I will let you know.
 * We will be really grateful if you write more about your case, like what exactly
   do you sell and what you would like to have in this feature. The examples will
   be great. That information will be really helpful to us because we want to provide
   a useful feature for you.
 * Best regards,
    Marta
 *  Thread Starter [howy](https://wordpress.org/support/users/howy/)
 * (@howy)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/programmatically-select-shipping-rule/#post-12578077)
 * Hi [@martapaw](https://wordpress.org/support/users/martapaw/),
    Thank you for
   your support.
 * My shop sells furnitures and home decorations. These are my current table rates
   based on weight (kg, VAT not included):
    - 0 – 1 —> 6.90
    - 1.1 – 3 —> 7.30
    - 3.1 – 5 —> 8.40
    - 5.1 – 10 —> 11.50
    - 10.1 – 20 —> 12.30
    - 20+ —> 27.30
 * Before calculating the weight I have to check the dimensions of the products 
   box to see if they exceed a certain value:
    - if the sum of all the dimensions of all the products of the cart exceeds 170cm
      I have to apply the maximum weight rule (20+ kg)
    - if the sum of only one dimension (length or width or height) exceeds 100cm
      I have to apply the maximum weight rule (20+ kg)
 * If I have these products in my cart
    Product 1 (10l x 80w x 20h – 3kg) x 1 Product
   2 (5l x 30w x 10h – 1kg) x 1 The shipment will cost 27.30 because the width has
   exceeded 100cm
 * If I have these products in my cart
    Product 1 (10l x 80w x 50h – 3kg) x 3 The
   shipment will cost 27.30 because the sum of all sizes of all products has exceeded
   170cm
 * If I have these products in my cart
    Product 1 (5l x 7w x 5h – 1kg) x 1 Product
   2 (8l x 30w x 20h – 5kg) x 1 Product 1 (20l x 20w x 20h – 3kg) x 1 The shipment
   will cost 11.50 because the dimensions do not exceed the limits and the weight
   rule is applied.
 * This is the code I’m still testing
 *     ```
       function my_woocommerce_package_rates($rates) {
           $items  = WC()->cart->get_cart();
           $length = $width = $height = 0;
           foreach ($items as $key => $value) {
               $dimentions = $value['data']->get_dimensions(false);
               $length     = $length + $dimentions['length'] * $value['quantity'];
               $width      = $width  + $dimentions['width']  * $value['quantity'];
               $height     = $height + $dimentions['height'] * $value['quantity'];
           }
           if ($length + $width + $height > 170 or $length > 100 or $width > 100 or $height > 100) {
               foreach ($rates as $rate) {
                   $rate->set_cost(27.30);
                   $rate->set_taxes([7.70]);
               }
           }
           return $rates;
       }
       add_filter('woocommerce_package_rates', 'my_woocommerce_package_rates', 100);
       ```
   
 * Hope this helps!
    -  This reply was modified 6 years, 1 month ago by [howy](https://wordpress.org/support/users/howy/).
    -  This reply was modified 6 years, 1 month ago by [howy](https://wordpress.org/support/users/howy/).
    -  This reply was modified 6 years, 1 month ago by [howy](https://wordpress.org/support/users/howy/).
 *  Plugin Support [Marta Borówka](https://wordpress.org/support/users/martapaw/)
 * (@martapaw)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/programmatically-select-shipping-rule/#post-12578514)
 * [@howy](https://wordpress.org/support/users/howy/),
 * Thank you very much!
 * I’ll talk about your case with the dev team. Maybe, we’ll be able to make this
   much faster. I’ll let you know.
 * Best regards,
    Marta

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Programmatically select shipping rule’ is closed to new replies.

 * ![](https://ps.w.org/flexible-shipping/assets/icon.svg?rev=2737045)
 * [Weight Based Shipping Table Rate for WooCommerce – Flexible Shipping](https://wordpress.org/plugins/flexible-shipping/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/flexible-shipping/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/flexible-shipping/)
 * [Active Topics](https://wordpress.org/support/plugin/flexible-shipping/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/flexible-shipping/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/flexible-shipping/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [Marta Borówka](https://wordpress.org/support/users/martapaw/)
 * Last activity: [6 years, 1 month ago](https://wordpress.org/support/topic/programmatically-select-shipping-rule/#post-12578514)
 * Status: resolved