Loading 0
Share

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

Leave a Reply

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

01.

Here you can see all the services I provide

Registration and management of domain names (website address such as www.nicolaslagios.com)

Also management of dns records (e.g. connecting the domain to a specific server, fixing email spam problems, etc.)

Also ssl renewals etc

Installation and management of web & mail server in ubuntu vps with virtualmin, plesk, cpanel

Also studying and fixing server problems.

Necessary condition, the target server meets the conditions

At the moment for new wordpress websites you can choose from ready-made themes and we change the content (no custom changes). You can buy with a fixed price by clicking here!

My team and I undertake any data bridging implementation for Wordpress, Prestashop, Opencart, Joomla platforms.

We can connect data from any source, as long as the structure is stable and there is proper documentation and briefing.

We undertake the creation, regulation and enrichment of pages for social networks: Facebook, Linkedin, Instagram (profile), Twitter (profile), Tiktok (profile).

We also undertake the first boost of your pages for quick results in followers.

We undertake the repair and maintenance of your existing wordpress website.

For more information about the services, you can read the following and return here to schedule a meeting with me: https://maxservices.gr/en/internet-services/website-services-blank/additional-website-services/