My Blog
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:
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:
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