PHP function for Woocommerce: change SKU on country?
-
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
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘PHP function for Woocommerce: change SKU on country?’ is closed to new replies.