Skip to content
This repository has been archived by the owner on Feb 25, 2022. It is now read-only.

Advanced Mappings

Erik Pöhler edited this page Mar 22, 2021 · 5 revisions

Every WooCommerce site is different. Different plugins, extensions you made, custom fields and so on. Also, requirements for your new Shopware store may vary. Which sales channels are you mapping customers into? Which WooCommerce payment method matches which Shopware payment method? In order to address these specific, individual requirements for your setup, I added a whole lot of filters one can hook into.

Keep in mind, that these filters are applied to each individual DB result row. So you should avoid making expensive DB queries inside the filters. Let's say you want to export 100k customers, and add a 4s $wpdb query inside a filter, the export would take around 4 1/2 - 5 days instead of a couple of seconds or minutes.

In order to hook into these filters you simply place calls to add_filter in your Wordpress Themes functions.php

Examples

Mapping Customer Languages

If you have a multilingual website, you might need to map languages. Let's say you do this based upon the customers billing country, then you would add a filter like so:

add_filter('shopware_six_exporter_filter_customer_languageId', 'map_language', 15, 4);
function map_language($value, int $user_id, array $user, $default = null) : string
    {
        $settings = json_decode(get_option(Plugin::SETTINGS_KEY), true);
        $default = $settings['customerDefaultLanguageId'];
        $value = $user['defaultBillingAddress.countryId']; // let's get the users country
        
        $map = [
            'DE' => '359791dbc2014d29b2f486de5d92d496', // german language uuid taken from Shopwares' language table
            'FR' => '8fbb88a187404f0fb00233708d648ee3', // french language uuid taken from Shopwares' language table
        ];
        // no billing country given, default to german language
        if (empty($value)) {
            return !empty($default) ? $default : $map['DE'];
        }
        
        // billing found, return the language uuid
        if (array_key_exists($value, $map)) {
            return $map[$value];
        }
        
        // all else default to german language
        return !empty($default) ? $default : $map['DE'];
    }

Mapping Customer Countries

In the same way, you need to tell Shopware, which Country ID any given address is belonging to:

add_filter('shopware_six_exporter_filter_customer_defaultBillingAddress_countryId', 'map_country', 15, 4);
add_filter('shopware_six_exporter_filter_customer_defaultShippingAddress_countryId', 'map_country', 15, 4);
function map_country($value, int $user_id, array $user) : string
    {
        $settings = json_decode(get_option(Plugin::SETTINGS_KEY), true);
        $default = $settings['customerDefaultCountryId'];

        $map =[
            'DE' => 'a0ac9c9b88024f4ea4230697158b9c01',
            'CH' => '3667a6331f7141fdacf9aa2dbd2c61de',
            'AT' => '7deabf41ba494c8ba376c72cb708f342',
            'IT' => 'cd2bb4721f20471d8f2b22ec8a028b97',
            // and so on...
        ];
        
        if ( !is_string($value) || is_null($value) || strlen($value) !== 2 ) {
            return !empty($default) ? $default : $map['DE'];
        }
        
        if (array_key_exists($value, $map)) {
            return $map[$value];
        }
        return !empty($default) ? $default : $map['DE'];
    }

Mapping Sales Channels by Billing Country

Let's say we want to assign and lock customers into distinct sales channels, based on their WooCommerce billing country, you would do it like this:

add_filter('shopware_six_exporter_filter_customer_boundSalesChannelId', 'map_sales_channel_by_billing_country', 15, 4);
add_filter('shopware_six_exporter_filter_customer_salesChannelId', 'map_sales_channel_by_billing_country', 15, 4);
function map_sales_channel_by_billing_country($value, int $user_id, array $user, $default = null) : string
{
        $settings = json_decode(get_option(Plugin::SETTINGS_KEY), true);
        $default = $settings['customerDefaultSalesChannelId'];
        $value = isset($user['defaultBillingAddress.countryId']) ? $user['defaultBillingAddress.countryId'] : null;

        $map =[
            'DE' => '68e43d11ef1949e396cbb588ba76f808',
            'CH' => 'c64374e8f473440081dd11fd55cb1582',
            'AT' => 'f2e7b45bafb24803baaf09bfd38c3c47',
        ];
        
        if ( !is_string($value) || is_null($value) || strlen($value) !== 2 ) {
            return !empty($default) ? $default : $map['DE'];
        }
        
        if (array_key_exists($value, $map)) {
            return $map[$value];
        }
        return !empty($default) ? $default : $map['DE'];
}

Likewise, you would check against the shipping country or any other criteria of your choice.