• Resolved jeeemmyy

    (@jeeemmyy)


    Hey Folks,

    I want product “variation images” to be deleted when a variation is deleted. I found a code but it only works when deleting whole product & deletes all product images while i only want variation images to be deleted when deleting variations.

    Code I found:

    add_action( ‘before_delete_post’, ‘delete_product_images’, 10, 1 );

    function delete_product_images( $post_id )
    {
    $product = wc_get_product( $post_id );

    if ( !$product ) {
    return;
    }

    $featured_image_id = $product->get_image_id();
    $image_galleries_id = $product->get_gallery_image_ids();

    if( !empty( $featured_image_id ) ) {
    wp_delete_post( $featured_image_id );
    }

    if( !empty( $image_galleries_id ) ) {
    foreach( $image_galleries_id as $single_image_id ) {
    wp_delete_post( $single_image_id );
    }
    }
    }

Viewing 2 replies - 1 through 2 (of 2 total)
  • Mirko P.

    (@rainfallnixfig)

    Hi @jeeemmyy,

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.
    ?
    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.
    ?
    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Thanks.

    Plugin Support lionel.a11n

    (@lioneldaniel)

    Hello @jeeemmyy,

    We haven’t seen any responses to this, so I’m going to mark this as resolved – I hope that Mirko’s suggestions helped you get help with your snippet. ??

    If you have any further questions, you can start a new thread. Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Remove Product Variation’s Images When Deleting a variation’ is closed to new replies.