首页 > 解决方案 > 删除与已删除属性关联的变体

问题描述

当我删除一个属性并保存它时,不会删除与该属性关联的变体。我真的很想自动删除与我已删除的属性相关的变体。
我的情况:
我有 Ship from 属性的三个值,我只想保留 China 。但是当我删除美国和欧洲时,不会删除与美国和欧洲相关的其他变体。
关于如何为我想要更改的每个产品执行此操作的任何想法。非常感谢 https://ibb.co/G0cc1cL

标签: woocommerce

解决方案


将此函数添加到您的functions.php 文件中。

function kotsh_delete_variations_with_no_parent() {

    $variations = new WP_Query( array(
        'post_type' => 'product_variation',
        'posts_per_page' => -1
    ) );

    if ( $variations->have_posts() ) {

        while ( $variations->have_posts() ) {
            $variations->the_post();

            $parent = wp_get_post_parent_id( get_the_id() );

            if ( false === get_post_status( $parent ) ) {
                wp_delete_post( get_the_id(), true );
            }

        }

    }

    wp_reset_postdata();

}
add_action( 'init', 'kotsh_delete_variations_with_no_parent', 10 );

推荐阅读