Support » WooCommerce » 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));
            $item->save();
        }
    }

    }

    any ideas??

    Thanks a lot! Mike

Ansicht von 1 Antwort (von insgesamt 1)
  • Moderation Note: sorry, I close this thread as you have asked your question in a German support forum in English. To keep the threads accessible to all people not speaking English, I kindly ask you to either rephrase your question in German or ask in the International support forums. Thank you for your understanding. Best regards, Hans-Gerd

Ansicht von 1 Antwort (von insgesamt 1)
  • Das Thema „SKU change based on shipping country. PHP function?“ ist für neue Antworten geschlossen.