Created
March 31, 2021 18:11
-
-
Save AcademicHumber/ca102422d56fe9413cbd59c71281cd42 to your computer and use it in GitHub Desktop.
WooCommerce - Modify order name depending on the state
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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