Skip to content

Instantly share code, notes, and snippets.

@emre-edu-tech
Last active October 13, 2024 07:10
Show Gist options
  • Select an option

  • Save emre-edu-tech/10c7905dd44d975b9daf47a38b9288b1 to your computer and use it in GitHub Desktop.

Select an option

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. In this example SKU is in parentheses in Product Title.
<?php
// 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