Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tivnet/bb829cddbc5f810b04900a76cb4c38c4 to your computer and use it in GitHub Desktop.
Save tivnet/bb829cddbc5f810b04900a76cb4c38c4 to your computer and use it in GitHub Desktop.
WooCommerce Multicurrency: create order programmatically
function ___return_currency_USD() {
return 'USD';
}
// Temporarily set active currency to "USD".
\add_filter( 'woocommerce_multicurrency_override_currency', '___return_currency_USD' );
// Create order programmatically.
$logger = \wc_get_logger();
$product_id = 10;
try {
$order = \wc_create_order();
$order->add_product( \wc_get_product( $product_id ), 1 );
$order->calculate_totals();
$order->save();
$logger->log( \WC_Log_Levels::INFO, 'Order total = ' . $order->get_total() . ' ' . $order->get_currency() );
} catch ( \Exception $exception ) {
$logger->log( \WC_Log_Levels::ERROR, "Cannot add product ID ${product_id} to the order." );
}
// Stop overriding currency.
\remove_filter( 'woocommerce_multicurrency_override_currency', '___return_currency_USD' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment