Skip to content

Instantly share code, notes, and snippets.

@emre-edu-tech
Last active January 12, 2024 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emre-edu-tech/10c7905dd44d975b9daf47a38b9288b1 to your computer and use it in GitHub Desktop.
Save emre-edu-tech/10c7905dd44d975b9daf47a38b9288b1 to your computer and use it in GitHub Desktop.
Woocommerce Update the product skus with the number that is gathered from the Product Title. SKU is in parentheses
// Update the product skus with the number that is gathered from the Product Title. SKU is in parentheses
// Make it available as an admin page
function update_product_skus() {
$args = array(
'post_type' => 'product',
'posts_per_page' => -1, // Retrieve all products; adjust if needed
);
$products = get_posts($args);
foreach ($products as $product) {
$title = $product->post_title;
if (preg_match('/\((\d+)\)/', $title, $matches)) {
$number = $matches[1];
echo $number . '<br>';
update_post_meta($product->ID, '_sku', $number);
}
}
echo "Completed updating SKUs.";
}
add_action('admin_menu', 'register_my_custom_menu_page');
function register_my_custom_menu_page(){
add_menu_page(
'Update SKUs',
'Update SKUs',
'manage_options',
'update-skus',
'update_product_skus'
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment