1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?php include( 'wp-load.php' ); $list = []; $args = [ 'post_type' => ['product'/*, 'post', 'page'*/], 'posts_per_page' => -1, 'post_status' => 'publish' ]; $loop = new WP_Query( $args ); $list = array(); while ( $loop->have_posts() ) : $loop->the_post(); global $post; $product = wc_get_product( $post->ID ); $max_sku = '100000000'.time(); if(empty($product->get_sku())){ $product->set_sku($max_sku); } if( $product->is_type('variable')) { $children_ids = $product->get_children(); foreach( $children_ids as $child_id ){ $max_sku = '100000000'.generateRandomString().time(); $variation = wc_get_product($child_id); if(get_post_meta($variation->get_id(), '_sku', true) === ''){ $variation->set_sku( $max_sku ); $variation->save(); } } } endwhile; wp_reset_query(); // Remember to reset ?> |