Loading 53
Share
WooCommerce Weight Based…

My Blog

Scroll Down

WooCommerce Weight Based Shipping: Bulk Import Shipping Rules

WooCommerce Weight Based Shipping: Bulk Import Shipping Rules

A well-known plugin for calculating cost by weight in woocommerce is WooCommerce Weight Based Shipping

The problem with this amazing plugin is that even today 10/19/2024, there is no way to bulk import shipping rules.

Or maybe there is?

For this trick to work, the shipping rules format will need to be as follows:
let weights = [1, 2, 3, 4];
let costs = [8.66, 11.12, 12.36, 13.6];

This means that:
From 0 to 1 kg, the cost will be: 8.66 while from 1 to 2 kilos, the cost will be: 11.12 etc.

So how do we bulk add the 4 above shipping rules?

  • First we need to add at least 1 rule manually
  • Then we will need to connect to our database in some way, e.g. with phpmyadmin and execute the following mysql command:
    1
    SELECT * FROM `wp_options` WHERE `option_name` REGEXP 'wbs_';
  • In the results it will bring us, we will process the value of the last result:
  • Next, we open a javascript console in our google chrome and paste the following script, in order to construct the entire serialized array which we will paste into the value of the record in the database:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    function generateSerializedData(weights, costs) {
    let rules = [];
     
    for (let i = 0; i < weights.length; i++) {
    let rule = {
    meta: {
    title: "Courier Service",
    label: "Shipping",
    enabled: 1,
    taxable: 1
    },
    conditions: {
    destination: {
    mode: "all",
    locations: []
    },
    weight: {
    range: {
    min: i === 0 ? 0 : weights[i - 1],
    max: weights[i],
    minInclusive: 1,
    maxInclusive: 0
    }
    },
    subtotal: {
    range: {
    min: 0,
    max: null,
    minInclusive: 0,
    maxInclusive: 0
    },
    tax: 0,
    discount: 1
    }
    },
    charges: {
    base: costs[i],
    weight: {
    cost: 0,
    step: 1,
    skip: 0
    },
    shippingClasses: []
    },
    modifiers: {
    clamp: {
    range: {
    min: null,
    max: null
    }
    }
    }
    };
     
    rules.push(rule);
    }
     
    // Serialize the data into a PHP-like serialized format
    function serialize(obj) {
    let serialized = '';
    if (Array.isArray(obj)) {
    serialized += `a:${obj.length}:{`;
    obj.forEach((item, index) => {
    serialized += `i:${index};${serialize(item)}`;
    });
    serialized += '}';
    } else if (typeof obj === 'object' && obj !== null) {
    const keys = Object.keys(obj);
    serialized += `a:${keys.length}:{`;
    keys.forEach(key => {
    serialized += `s:${key.length}:"${key}";${serialize(obj[key])}`;
    });
    serialized += '}';
    } else if (typeof obj === 'string') {
    serialized += `s:${obj.length}:"${obj}";`;
    } else if (typeof obj === 'number') {
    serialized += Number.isInteger(obj) ? `i:${obj};` : `d:${obj};`;
    } else if (typeof obj === 'boolean') {
    serialized += `b:${obj ? 1 : 0};`;
    } else if (obj === null) {
    serialized += 'N;';
    }
    return serialized;
    }
     
    let data = {
    enabled: 1,
    rules: rules
    };
     
    return serialize(data);
    }
     
    // Example weight ranges and corresponding costs
    let weights = [1, 2, 3, 4];
    let costs = [8.66, 11.12, 12.36, 13.6];
     
    // Generate the serialized data
    let serializedData = generateSerializedData(weights, costs);
    console.log(serializedData);
  • Example of generated serialized array:

That's all.

Of course you can customize the function for the other options, but you get the idea.

If you need help, let me know.

Best regards, Nicolas Lagios

Leave a Reply

Your email address will not be published. Required fields are marked *

01.