WP SITES

3093 Coded Tutorials & 297 Plugins

Change Currency for Manual Orders in WooCommerce

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.

add_action('woocommerce_new_order', 'set_custom_currency_for_manual_orders', 10, 1);
function set_custom_currency_for_manual_orders($order_id) {
    // Check if the order is manual (admin-created) by looking at the order status
    $order = wc_get_order($order_id);
    $order_status = $order->get_status();
 
    // Specify the custom currency code you want to set for manual orders
    $custom_currency_code = 'EUR';
 
    // Check if the order is manual (e.g., status is 'on-hold' for admin-created orders)
    if ($order_status === 'on-hold') {
        // Set the custom currency for the order
        $order->set_currency($custom_currency_code);
        $order->save();
    }
}

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.
  • $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.
  • $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.
  • $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.
  • 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 the woocommerce_new_order action with a priority of 10 and expects one parameter (order ID).

Related Solutions

Was this helpful?

Yes
No
Thanks for your feedback!

Leave a Reply