Created
December 21, 2020 17:03
-
-
Save tivnet/bb829cddbc5f810b04900a76cb4c38c4 to your computer and use it in GitHub Desktop.
WooCommerce Multicurrency: create order programmatically
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
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