SKU change based on shipping country. PHP function?

Hey there!

i have a webshop and i sell to Switzerland CH and Germany DE. now i have connected that shop to my ERP Tool and so far all is working fine.

I now need to have a function which adds a prefix to the SKU, based on the country of purchase. so for instance:

  • a customer buys a product with SKU: 1234
  • his delivery address is germany
  • the function should add a prefix DE- to the SKU and write this into the order. so it says SKU: DE-1234
  • how do i do this?

Tryed with my following php snipped, but it is not working:::

add_action(‚woocommerce_new_order‘, ‚customize_sku_based_on_shipping_country‘, 10, 1);

function customize_sku_based_on_shipping_country($order_id) {
// Get the order
$order = wc_get_order($order_id);

// Get the shipping country
$shipping_country = $order->get_shipping_country();

// Define prefixes based on countries
$prefixes = array(
    'DE' => 'DE-', // Prefix for Germany
    'AT' => 'AT-', // Prefix for Austria
    // Add more countries and prefixes as needed

// Check if there is a prefix for the shipping country
if (isset($prefixes[$shipping_country])) {
    // Iterate over the order items and add the prefix to the SKU
    foreach ($order->get_items() as $item_id => $item) {
        $product     = $item->get_product();
        $current_sku = $product->get_sku();
        $new_sku     = $prefixes[$shipping_country] . $current_sku;

        // Update the SKU of the order item
        $item->set_props(array('sku' => $new_sku));


any ideas??

Thanks a lot! Mike


This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer