Last active
January 12, 2024 08:10
-
-
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
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
// 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