Title: Generating 1000  rules with a script
Last modified: August 30, 2016

---

# Generating 1000 rules with a script

 *  Resolved [maxmumford](https://wordpress.org/support/users/maxmumford/)
 * (@maxmumford)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/generating-1000-rules-with-a-script/)
 * Hello,
 * I have a shipping scenario that means I will need about 1200 rules. I have written
   a python script that generates these rules, but I am not sure how to go about
   uploading them.
 * My attempts with SQL inserts have failed, presumably because of the enigmatic
   wp_options serialization method that I cannot seem to replicate in python.
 * Can you tell me what the best approach is for this?
 * Here is an example insert that my script generated:
 * INSERT INTO wp_options (option_name, option_value, autoload) VALUES (‘woocommerce_WC_Weight_Based_Shipping_7611625400_settings’,‘
   a:18:{s:7:”enabled”;s:3:”yes”;s:4:”name”;s:50:”FedEx International Economy 62.5
   kg – 63 kg Zone A”;s:5:”title”;s:27:”FedEx International Economy”;s:12:”availability”;
   s:8:”specific”;s:9:”countries”;a:1:{i:0;s:2:”CA”;}s:10:”tax_status”;s:4:”none”;
   s:3:”fee”;i:0;s:4:”rate”;i:0;s:9:”min_price”;d:89.65;s:9:”max_price”;d:89.65;
   s:17:”extra_weight_only”;s:2:”no”;s:11:”weight_step”;i:0;s:10:”min_weight”;d:
   62.5;s:10:”max_weight”;d:63;s:12:”min_subtotal”;i:0;s:12:”max_subtotal”;i:0;s:
   17:”subtotal_with_tax”;s:2:”no”;s:24:”shipping_class_overrides”;O:31:”WBS_Shipping_Class_Override_Set”:
   1:{s:42:”WBS_Shipping_Class_Override_Setoverrides”;a:0:{}}}’, ‘yes’);
 * However when I load the rules in the backend, none of the values are loaded (
   i.e. there is a row for the rule, but every field is blank).
 * Cheers
    Max
 * [https://wordpress.org/plugins/weight-based-shipping-for-woocommerce/](https://wordpress.org/plugins/weight-based-shipping-for-woocommerce/)

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

 *  Plugin Author [Dan](https://wordpress.org/support/users/dangoodman/)
 * (@dangoodman)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/generating-1000-rules-with-a-script/#post-6581429)
 * Hi Max,
 * Although there might be a way to import that into plugin settings I’d discourage
   you from doing this. The reason is that it will not work well in terms of performance
   with such a great number of rules. The plugin is supposed to be used with tens
   of rules, not hundreds.
 * Maybe we can simplify the configuration. Would you mind to describe how the rates
   generated? Is there a source table which you grab rules from with your script?
   Maybe they can be turned into a smaller number of progressive rates, like this:
   take $5 for first 1kg, add $1 for each additional 0.5kg?
 *  Thread Starter [maxmumford](https://wordpress.org/support/users/maxmumford/)
 * (@maxmumford)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/generating-1000-rules-with-a-script/#post-6581442)
 * Hello,
 * Unfortunately the rates do not increase at a set step each kg – each kg becomes
   cheaper and cheaper the larger the order becomes. I.e. 1kg is £1, 2kg is £2, 
   3kg is £2.90 and so on. For this reason I can’t just use the step field.
 * Here are the first 8 rows from the rates table (total of 141 rows):
 *     ```
       Weight,A,B,C,D,E,F,H,R,S,T,U,V,W
       0.5,11.03,20.95,25.34,32.04,38.22,21.22,11.03,10.25,10.25,13.22,14.76,19.71,10.25
       1,12.07,23.21,26.02,36.01,38.22,23.63,12.07,10.25,10.25,13.22,14.76,21.66,10.25
       1.5,13.1,25.46,29.24,39.97,38.22,26.05,13.1,10.25,10.25,13.22,14.76,23.6,10.25
       2,14.14,27.72,32.46,43.92,40.74,28.46,14.14,10.25,10.25,13.22,14.76,25.55,10.25
       2.5,15.18,29.97,35.68,47.88,44.69,30.87,15.18,10.25,10.25,13.22,14.76,27.5,10.25
       3,15.77,31.69,37.54,50.55,48.08,31.68,15.77,10.25,10.25,13.22,14.76,28.63,10.25
       3.5,16.36,33.41,39.38,53.3,51.18,33.24,16.36,10.25,10.25,13.22,14.76,29.76,10.25
       4,16.95,35.14,41.22,56.05,54.27,34.79,16.95,10.25,10.25,13.22,14.76,30.89,10.25
       ```
   
 * Each letter indicates a zone, which is a collection of countries. With one rule
   per country and weight bracket I will need 1833 rules to achieve this. Unless
   you can think of a better way?
 * In the meantime I will do some performance testing to get an idea of how efficient
   it will be.
 * Cheers
    Max.
 *  Plugin Author [Dan](https://wordpress.org/support/users/dangoodman/)
 * (@dangoodman)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/generating-1000-rules-with-a-script/#post-6581461)
 * Thanks.
 * There are consecutive rules in the table having same price per weight. So the
   total number of rules can be reduced by folding similar ones. It seems it would
   be still very high, though.
 * I think it would be better to make a custom shipping plugin in your case. It 
   should not be difficult since you have scripting skills.
 * I’m still sure this plugin will not work well with a large number of rules, due
   to the way it’s working. However, if you have some testing results it would be
   interesting to see them.
 *  Thread Starter [maxmumford](https://wordpress.org/support/users/maxmumford/)
 * (@maxmumford)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/generating-1000-rules-with-a-script/#post-6581555)
 * Hello,
 * Sorry for the late reply; I was working on other parts of the site.
 * With 1200 rules it takes about 7 seconds on a slow testing sever to perform the
   shipping calculation on the checkout page. Once reduced to 600 it took 5 seconds,
   which will be too slow in production once concurrent users visit the site.
 * I’ll take a look into using the fedex API instead, otherwise I’ll create a custom
   plugin. Am I right in thinking the main performance limitation for this plugin
   is the fact that the rules are saved as wp_options and aren’t in their own table?
 * Cheers
    Max.
 *  Plugin Author [Dan](https://wordpress.org/support/users/dangoodman/)
 * (@dangoodman)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/generating-1000-rules-with-a-script/#post-6581556)
 * Hi,
 * No, wp_options is not an issue at all. The issue is that each rule is a WC_Shipping_Method
   instance. I.e. WooCommerce treats each rule as a shipping method which implies
   an overhead in both memory and cpu usage. That’s not an issue when there are 
   a few tens of rules, but it is when there hundreds of them.
 *  Thread Starter [maxmumford](https://wordpress.org/support/users/maxmumford/)
 * (@maxmumford)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/generating-1000-rules-with-a-script/#post-6581557)
 * Ah I see. Thanks for your help.
 * In case anybody in the future sees this thread and decides to go the same route,
   feel free to use my import script found on github here:
 * [https://github.com/maxmumford/woocommerce-weight-shipping-import](https://github.com/maxmumford/woocommerce-weight-shipping-import)
 * It’s very slow but does the job…
 * Max
 *  [starapple](https://wordpress.org/support/users/starapple/)
 * (@starapple)
 * [10 years, 5 months ago](https://wordpress.org/support/topic/generating-1000-rules-with-a-script/#post-6581598)
 * [@maxmumford](https://wordpress.org/support/users/maxmumford/) your script seems
   the sort of solution I’d need in a multi-vendor set up for rates based on country
   zones. Are the rates here based on shipping from one location to multiple destinations?
   I’d like a vendor in Canada or Trinidad to be able to set their “ship from” location
   and outbound parcel post rates to preset zones.
 *  Plugin Author [Dan](https://wordpress.org/support/users/dangoodman/)
 * (@dangoodman)
 * [10 years, 5 months ago](https://wordpress.org/support/topic/generating-1000-rules-with-a-script/#post-6581599)
 * Thanks for sharing your script.
 * [@starapple](https://wordpress.org/support/users/starapple/), the plugin does
   not support multiple origins. It seems the import script has nothing to do with
   this limitation.
 *  Thread Starter [maxmumford](https://wordpress.org/support/users/maxmumford/)
 * (@maxmumford)
 * [10 years, 5 months ago](https://wordpress.org/support/topic/generating-1000-rules-with-a-script/#post-6581604)
 * Hi guys,
 * Sorry for the late reply, I’ve been swamped, and this project unfortunately has
   been put on indefinite hold… Feel free to fork and modify the script though.
 * Good luck
    Max

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

The topic ‘Generating 1000 rules with a script’ is closed to new replies.

 * ![](https://ps.w.org/weight-based-shipping-for-woocommerce/assets/icon-256x256.
   png?rev=2072721)
 * [Weight Based Shipping for WooCommerce](https://wordpress.org/plugins/weight-based-shipping-for-woocommerce/)
 * [Support Threads](https://wordpress.org/support/plugin/weight-based-shipping-for-woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/weight-based-shipping-for-woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/weight-based-shipping-for-woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/weight-based-shipping-for-woocommerce/reviews/)

## Tags

 * [automatic](https://wordpress.org/support/topic-tag/automatic/)
 * [automation](https://wordpress.org/support/topic-tag/automation/)
 * [Generation](https://wordpress.org/support/topic-tag/generation/)
 * [script](https://wordpress.org/support/topic-tag/script/)

 * 9 replies
 * 3 participants
 * Last reply from: [maxmumford](https://wordpress.org/support/users/maxmumford/)
 * Last activity: [10 years, 5 months ago](https://wordpress.org/support/topic/generating-1000-rules-with-a-script/#post-6581604)
 * Status: resolved