Skip to content

Instantly share code, notes, and snippets.

@simplenotezy
Last active October 23, 2019 09:45
Show Gist options
  • Save simplenotezy/0f69ba2ad702ea386a2ec06f365bd1d7 to your computer and use it in GitHub Desktop.
Save simplenotezy/0f69ba2ad702ea386a2ec06f365bd1d7 to your computer and use it in GitHub Desktop.
Migrate from "WooCommerce Multi Currency Premium" to WPML's own multi-currency
<?php
/**
* Below script helps you migrate from "WooCommerce Multi Currency Premium" by VillaTheme to WPML's own multi-currency
* This is helpful if you've added WPML to your site, after setting up multi currency, and would like to lower
* your plugin overhead, or if you're experiecing issues with VillaTheme's plugin.
*
* Esentially, when you run below script, it will migrate all the postmeta from your products / variations from meta
* values "_DKK" to "_price_DKK", etc.
*
* NOTE: This script does not handle scheduled prices, but it can be easilly managed. Also sale price is not included,
* but it's very easy to add if you'd like.
*
* How to use:
* 1) Update the $currencies array to the currencies you'd like to update
* 2) Install WP-Cli and run the command "wp convert-currencies-format"
* 3) Verify that you currencies have been updated.
*
*
* Usage: `wp convert-currencies-format`
*
* @author Mattias Fjellvang <simplenotezy@github>
*/
function convert_currencies_format() {
global $wpdb;
$metas_updated = 0;
/**
* Currencies - add your currencies to the below array
*/
$currencies = [
'"_DKK"',
'"_NOK"',
'"_GBP"',
'"_EUR"',
'"_SEK"'
];
/**
* Fetch posts
*/
$query = "
SELECT
p.id
FROM
" . $wpdb->prefix . "postmeta pm
INNER JOIN
" . $wpdb->prefix . "posts p ON p.id = pm.post_id AND p.post_type IN ('product', 'product_variation')
WHERE
pm.meta_key IN (" . implode(',', $currencies) . ")
GROUP BY
p.id
";
$products = $wpdb->get_results($query);
/**
* Look through and update
*/
WP_CLI::success( 'Found ' . count($products) . ' products to update ');
/**
* Promt user to confirm
*/
WP_CLI::warning('Please backup your database before proceeding!');
WP_CLI::confirm('Would you like to continue? Following currencies will be updated if they exist: ' . str_replace(['"', '_'], '', implode(',', $currencies)));
/**
* Loop through and update
* @var [type]
*/
$progress = \WP_CLI\Utils\make_progress_bar( 'Converting product meta', count($products) );
foreach($products as $product) {
/**
* Fetch post meta
*/
$query = "
SELECT
*
FROM
" . $wpdb->prefix . "postmeta pm
WHERE
pm.post_id = '" . $product->id . "' AND pm.meta_key IN (" . implode(',', $currencies + ['_sale_price']) . ")
";
$metas = $wpdb->get_results($query);
/**
* Convert metas
*/
foreach($metas as $meta) {
$metas_updated = $metas_updated + 3; // amount of metas updating...
update_post_meta( $product->id, '_price' . $meta->meta_key, $meta->meta_value);
update_post_meta( $product->id, '_regular_price' . $meta->meta_key, $meta->meta_value);
update_post_meta( $product->id, '_wcml_custom_prices_status', '1');
//update_post_meta( $product->ID, '_sale_price' . $meta->meta_key, $meta->meta_value);
}
$progress->tick();
}
$progress->finish();
// Show success message
WP_CLI::success( 'Finished updating ' . count($products) . ' with ' . $metas_updated . ' meta values' );
}
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::add_command( 'convert-currencies-format', 'convert_currencies_format' );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment