Viewing 4 replies - 1 through 4 (of 4 total)
  • HELLO!

    This would be very important if it did.

    Today, if you duplicate a product, it does not regenerate the main SKU or the variations.

    If you click update on the main SKU and save the product, only the main one changes, the variations still have the old code.

    To “solve” this, the steps are as follows:

    1- Duplicate the product
    2- Regenerate the Main SKU
    3- Access variation by variation and delete the entire SKU. 4- Click on save variations
    5- Click on update product 1x
    6- Click on update product 2x (this time, the correct SKU will be created in the variation with -01 at the end.

    Is there any way to improve this? In other words, if it was duplicated, does it automatically generate everything?

    I know it is possible to regenerate it by properties, but this ends up regenerating ALL of them, and not just the new ones or those with problems.

    If there was a button inside the product to regenerate the main one and the variations, it would be perfect!

    Would this be possible? Is there anything we can do or collaborate on to make this improvement?

    Thank you very much, your plugin is amazing.

    Use this method can force override with new SKU, tested and work well.

    // Method 2: Completely override SKU generation when duplicating products

    add_action(‘woocommerce_product_duplicate_before_save’, function($new_product, $old_product) { $new_product->set_sku(”); }, 999, 2);

    Hello friend!

    I put your code and it broke the site lol, it was hard to reactivate, I had to put the snippet plugin in safe mode to activate it and remove the code, it was wrong, here is the fix:

    add_action(‘woocommerce_product_duplicate_before_save’, function($new_product, $old_product) {
    $new_product->set_sku(”);
    }, 999, 2);

    The code only solved the problem by clearing the duplicate main SKU and generating a new one.

    But the variations still need to save the product TWICE to generate the variation SKU based on the parent SKU.

    I tried to get help from Chatgpt, I created several codes, but nothing worked.

    Would you be able to see this improvement?

    Thread Starter schero86

    (@schero86)

    I solved the issue using following code in functions.php.
    Also no need to push generate new sku, it is automaticaly generated on duplication.

    function clear_sku_on_duplicate( $new_id, $post ) {
    // Delete SKU of duplicated product
    update_post_meta( $new_id, '_sku', '' );

    // Check if product has variatioms
    if ( 'product_variation' === get_post_type( $new_id ) ) {
    return;
    }

    // Get variations of duplicated product
    $children = get_children( array(
    'post_parent' => $new_id,
    'post_type' => 'product_variation',
    'post_status' => 'publish'
    ) );
    // Delete SKU for all variations of duplicated product
    foreach ( $children as $child ) {
    update_post_meta( $child->ID, '_sku', '' );
    }
    }
    add_action( 'woocommerce_duplicate_product', 'clear_sku_on_duplicate', 10, 2 );

    • This reply was modified 2 weeks, 2 days ago by schero86.
Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.