This code is written to change the currency of manual (admin-created) orders to a custom currency when the orders are created, specifically when the order status is ‘on-hold’. The hook ensures that this process occurs when a new order is created in the WooCommerce store.
After adding the code to your child themes functions file, click Add Order, change the status to On Hold and click Create.
How The Code Works
- function set_custom_currency_for_manual_orders($order_id) { … }
- This function is designed to be hooked into the
woocommerce_new_order
action. It triggers when a new order is created. - It takes the order ID as a parameter.
- This function is designed to be hooked into the
- $order = wc_get_order($order_id);
- This line uses the
wc_get_order
function to retrieve the WooCommerce order object based on the provided order ID.
- This line uses the
- $order_status = $order->get_status();
- It retrieves the status of the order using the
get_status
method on the order object. The order status represents the current state of the order.
- It retrieves the status of the order using the
- $custom_currency_code = ‘YOUR_CUSTOM_CURRENCY_CODE’;
- A custom currency code is specified here. You should replace
'YOUR_CUSTOM_CURRENCY_CODE'
with the actual currency code you want to set for manual orders.
- A custom currency code is specified here. You should replace
- if ($order_status === ‘on-hold’) { … }
- This condition checks if the order status is ‘on-hold’. This is often used to identify admin-created or manual orders.
- $order->set_currency($custom_currency_code);
- If the order is indeed a manual order (on-hold status), this line sets the order’s currency to the custom currency code specified earlier.
- $order->save();
- This saves the changes made to the order, including the updated currency.
- add_action(‘woocommerce_new_order’, ‘set_custom_currency_for_manual_orders’, 10, 1);
- This line hooks the
set_custom_currency_for_manual_orders
function into thewoocommerce_new_order
action with a priority of 10 and expects one parameter (order ID).
- This line hooks the
Leave a Reply