【问题标题】:Delete Before Post only if Parent仅在父级时删除之前发布
【发布时间】:2021-12-17 12:25:45
【问题描述】:

我正在使用下面的 sn-p,但是在删除 WooCommerce 变体时,它会触发代码删除图像

无论如何我只能在删除父项而不是变体时触发这个?

// Automatically Delete Woocommerce Images After Deleting a Product
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 );
        }
    }
}

【问题讨论】:

标签: wordpress woocommerce hook


【解决方案1】:

你可以试试这个吗?

if ( !$product->is_type( 'product_variation' ) ) {}

我还没有测试过,我还没有现成的用于测试目的的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-29
    • 1970-01-01
    • 1970-01-01
    • 2016-05-05
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    相关资源
    最近更新 更多