• Resolved teamcoltra

    (@teamcoltra)


    So I created a product type that uses the Variable type as its base. It works great, wordpress treats it like a variable product. HOWEVER, there is a weird edge case that I’m working with and I can’t seem to figure out the solution:

    If I create a product as a Variable product first, generate the variations, then save it. The variations work.

    If I create a product as Variable-Clone product, generate variations, then save it. The variations work.

    If I create a product as a Variable product, generate variations, then save it, then change the product type to Variable-Clone and save it again…. the variations get deleted. I checked it’s not like some ID changing thing where wordpress just doesn’t see the variations. I checked the database and the X number of products with post_type “product_variation” for that item are removed from the wp_posts table.

    I just also ran the test and converted a product from Variable product to “Simple Product” and then back to Variable product and the same thing happens (so it’s not code on my end).

    I’m guessing this is some kind of housekeeping to make sure there aren’t stray variable products sitting around causing issues. However, it really messes with my setup. Is there a way to fix this? If not can someone help me find where in the code it’s happening and I’ll just create a hook manually and maybe submit it to core (and just keep updating my own version until it gets accepted)

    Thanks so much.

    • This topic was modified 1 year, 3 months ago by teamcoltra.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @teamcoltra

    This is indeed a default behavior of WooCommerce, as it cleans up variations when a product type is changed to prevent any potential issues.

    However, I understand your use case and why you want to preserve these variations. Unfortunately, there isn’t a straightforward solution to prevent this in the WooCommerce settings.

    As for your request to find the code that’s causing this, you can see it here: https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce/includes/admin/meta-boxes/class-wc-meta-box-product-data.php#L443

    There’s a function called save_variations which is responsible for saving product variations. Inside this function, there’s a line that deletes the variations when the product type is changed.

    If you’re comfortable with coding, you can create a custom function with a hook to override this behavior. Please note that modifying the core WooCommerce files is not recommended as it can lead to issues with updates in the future.

    Hope this helps and let us know if you have any other questions.

    Thread Starter teamcoltra

    (@teamcoltra)

    I finally got a chance to sit down and look at this. I don’t think the reference you made contains the part that actually deletes the variations. Unless I’m mistaken.

    I believe it takes place here: https://github.com/woocommerce/woocommerce/blob/a32e1a3922660c28e9048241f27e68649f5b2926/plugins/woocommerce/includes/class-wc-post-data.php#L137

    I believe I can actually fix this with the code:

    add_filter( 'woocommerce_delete_variations_on_product_type_change', '__return_false' );

    and that should fix my problem. I’ll update this post if I had to make other changes or get stuck.

    Here is my contention: This doesn’t feel like it should be the default behavior. Deleting content without the user knowingly expecting to delete content feels bad to me. If a person fills in a bunch of details on hundreds of different variations, then goes to test to see what the product would look like in “Simple Product” there is no indication or warning or anything that would make the user expect that they shouldn’t do that and all their variations will be deleted.

    With as much random junk gets added to a database in a wordpress installation (spam, etc) the small number of variable products that get changed to single products on purpose feels like such a small niche that wouldn’t create that much junk vs the risk of something bad happening to someone’s data if they make a mistake.

    I’m empathetic to the desire to have a clean database though and perhaps this should be fixed in Javascript with a “would you like to delete your variations?” on change.

    Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @teamcoltra,

    You are correct; the deletion of variations is initiated in the file you shared.

    Your proposed solution, using the woocommerce_delete_variations_on_product_type_change filter and returning false, should indeed prevent the variations from being deleted.

    Let us know if you encounter any further issues or have additional questions.

    jgramp

    (@jgramp)

    I also changed the product type from a variable product to a simple product, assuming that WordPress wouldn’t actually delete the data in the database (since it doesn’t do this for plugins that are uninstalled either) and now all of my orders of these variable products have problems displaying the data. Please can you explain which database tables now need to be restored to recover this data?

    I have identified these so far:

    • wp_wc_product_meta_lookup
    • wp_wc_product_attributes_lookup

    My Tickera integration is now also no longer working.

    I still can’t believe that data would be deleted with zero warning!!!

    This is a big issue that I hope will be resolved with a warning, so other people don’t do the same as me and end up losing important order data for a production Woocommerce site ??

    I’m quite nervous now that other setting changes could potentially cause big issues like this with absolutely no warning.

    Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @jgramp,

    Sorry to hear that you are experiencing the same issue.

    However, to align with forum best practices, please create a new topic so that we can address your issue(s) separately.

    You can create a new thread here.

    Thanks for understanding!

    Thread Starter teamcoltra

    (@teamcoltra)

    The reason the forum best practices say that is because the person might not have exactly the same issue. This is an identical issue and I’m glad they wrote in this thread because it really highlights the importance of this being fixed. Regardless of the intention, it is a bug.

    jgramp

    (@jgramp)

    Yes, it’s the same issue, as described by the original ticket. I spent all day working on a fix. I have had to manually restore the attributes and variations that were deleted from a backup copy of my website database, that I had not modified and thankfully all of the other order data, including the links to my Ticket integration (Tickera) have now been restored. I’ll post further details tomorrow, but for now I wanted to share that I managed to restore the data in the following 4 tables.

    First, I changed?the product back to a variable product and saved it, but the attributes and variations were still missing.

    So I then had to retrieve and restore the relevant rows from the following tables:

    – wp_posts

    – wp_postmeta

    – wp_wc_product_meta_lookup

    – wp_wc_product_attributes_lookup

    I’ll post further details tomorrow and hopefully this will help others who have experienced this same issue.

    What a relief anyway! I thought I had lost the information for 711 ticket sales with the change of one (seemingly innocent) change of setting in WooCommerce. But luckily I could recover all this data!

    Hopefully something is changed to avoid others making the same mistake!

    Cheers.

    Jess

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Product Variations Deleted On Save (When changing product type)’ is closed to new replies.