How to bulk import slugs of products?
-
Hello, I am using bear plugin for bulk product edit. I am very satisfied with plugin but now I have one problem. I want to bulk import slugs of products but I cant find how to “Assign to field” new slugs. Is it possible to bulk import slugs via BEAR plugin?
-
hello
For import we use standard woocommerce functionality. And it seems that this functionality does not have the ability to change the product slug
Hello
Have you got in your csv file products IDs?
I mean next, if you have csv file with data like:
id,slug 123,new-slug-for-product-123 456,new-slug-for-product-456You can apply next function:
function update_product_slugs_from_csv() { $csv_file_path = ABSPATH . 'path/to/your/file.csv'; $file_handle = fopen($csv_file_path, 'r'); if ($file_handle !== FALSE) { global $wpdb; fgetcsv($file_handle); while (($data = fgetcsv($file_handle, 1000, ",")) !== FALSE) { $product_id = intval($data[0]); $new_slug = sanitize_title($data[1]); $slug_exists = $wpdb->get_var($wpdb->prepare("SELECT post_name FROM {$wpdb->posts} WHERE post_name = %s AND ID != %d", $new_slug, $product_id)); if (!$slug_exists) { $wpdb->update( $wpdb->posts, ['post_name' => $new_slug], ['ID' => $product_id] ); echo "Slug for product ID $product_id updated to $new_slug.\n"; } else { echo "Slug $new_slug for product ID $product_id is not unique and was not updated.\n"; } } fclose($file_handle); } else { echo "Failed to open the CSV file.\n"; } }If you know wjat is wp-cli do next:
* to functions.php add next code:
if (defined('WP_CLI') && WP_CLI) { WP_CLI::add_command('update_product_slugs', 'update_product_slugs_from_csv'); }* You can now run this feature using the following WP-CLI command: wp update_product_slugs
-
This reply was modified 1 year, 11 months ago by
RealMag777.
Hello @realmag777, thank you for your reply. I am tried to add this function to my child themes functions.php file but with no changes. Can you check it please?
function update_product_slugs_from_csv() { $csv_file_path = ABSPATH . 'https://foxym.sk/wp-content/uploads/2024/03/ro-slug.csv'; $file_handle = fopen($csv_file_path, 'r'); if ($file_handle !== FALSE) { global $wpdb; fgetcsv($file_handle); while (($data = fgetcsv($file_handle, 1000, ",")) !== FALSE) { $product_id = intval($data[0]); $new_slug = sanitize_title($data[1]); $slug_exists = $wpdb->get_var($wpdb->prepare("SELECT post_name FROM {$wpdb->posts} WHERE post_name = %s AND ID != %d", $new_slug, $product_id)); if (!$slug_exists) { $wpdb->update( $wpdb->posts, ['post_name' => $new_slug], ['ID' => $product_id] ); echo "Slug for product ID $product_id updated to $new_slug.\n"; } else { echo "Slug $new_slug for product ID $product_id is not unique and was not updated.\n"; } } fclose($file_handle); } else { echo "Failed to open the CSV file.\n"; } }Hello @rptr
Please read this -https://share.pluginus.net/image/i20240318132407.png and https://make.wordpress.org/cli/handbook/guides/quick-start/
Hello, thank you for your reply. Now it works. For other people: be careful at line 2. It should looks like this:
$csv_file_path = 'https://xyz.com/path/to/your/file.csv'-
This reply was modified 1 year, 11 months ago by
rptr.
Hello
great. thank you for your cooperation
-
This reply was modified 1 year, 11 months ago by
The topic ‘How to bulk import slugs of products?’ is closed to new replies.