Skip to content

Instantly share code, notes, and snippets.

@AcademicHumber
Created March 31, 2021 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AcademicHumber/ca102422d56fe9413cbd59c71281cd42 to your computer and use it in GitHub Desktop.
Save AcademicHumber/ca102422d56fe9413cbd59c71281cd42 to your computer and use it in GitHub Desktop.
WooCommerce - Modify order name depending on the state
<?php
/**
* @snippet Modify order name depending on the state
* @author Foco Azul - Adrian Fernandez
* @testedwith WooCommerce 3.8
*/
function change_woocommerce_new_order_data($order, $data)
{
// Order name prefixes
// They have to be equal to html <option> values
$prefixes = [
'S' => 'SC - ',
'H' => 'BE - ',
'B' => 'CH - ',
'C' => 'CB - ',
'L' => 'LP - ',
'O' => 'OR - ',
'N' => 'PD - ',
'P' => 'PT - ',
'T' => 'TR - ',
];
$state = $order->get_billing_state();
$name = $order->get_billing_first_name();
try {
$order->set_billing_first_name($prefixes[$state] . $name);
} catch (\Throwable $th) {
$order->set_billing_first_name('OTRO - ' . $name);
}
}
// add the action
add_action('woocommerce_checkout_create_order', 'change_woocommerce_new_order_data', 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment