Answer the question
In order to leave comments, you need to log in
SKU generation from title and variation?
Greetings!
How to write Title + Variation in _SKU (only Title for a simple product)? This is necessary to update through WP All Import existing products in the store (more than 8000) and add new ones. Now it only updates simple products by Title.
It's sad that I didn't make this field beforehand...
Answer the question
In order to leave comments, you need to log in
Maybe someone will come in handy (if there are a lot of goods, increase the time_limit):
add_filter( 'init', 'sku_from_title_and_variation', 10, 1);
function sku_from_title_and_variation(){
set_time_limit(300);
$query = array(
'numberposts' => -1,
'post_status' => 'published',
);
$products = wc_get_products( $query );
foreach ($products as $product) {
if( $product->is_type('variable') ){
$product_id = $product->get_id();
$product_title = $product->get_title();
update_post_meta( $product_id, '_sku', $product_title );
wc_delete_product_transients( $product_id );
foreach( $product->get_available_variations() as $variation_values ){
$variation_id = $variation_values['variation_id']; // variation id
$variation_attr = $variation_values['attributes'];
$variation_attr = $variation_attr['attribute_pa_variant'];
$variation_attr = get_term_by('slug', $variation_attr , 'pa_variant')->name;
update_post_meta( $variation_id, '_sku', $product_title.$variation_attr );
wc_delete_product_transients( $variation_id );
}
wc_delete_product_transients( $product->get_id() );
} else {
update_post_meta( $product->get_id(), '_sku', $product->get_title());
wc_delete_product_transients( $product->get_id() );
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question