WooCommerce 更新所有变体(以编程方式)

WooCommerce update all variations (programmatically)

我正在尝试在所有产品的每个变体下更新 WooCommerce 中的自定义字段。我获得了适用于简单产品的代码,但无法遍历变体并更新它们。我在 Whosebug 上尝试了几乎所有 WooCommerce 变体循环,但所有这些变体都 return 错误或根本不起作用。任何建议将不胜感激。 functions.php 中的当前代码用于简单产品。

add_action('init', 'bulk_update_post_meta_data');
    function bulk_update_post_meta_data() {
        $args = array(
        'posts_per_page' => -1,
        'post_type' => 'product',
        'suppress_filters' => true
        );

        $posts_array = get_posts( $args );

        foreach($posts_array as $post_array) {
        $Cogcost = get_post_meta( $post_array->ID, '_regular_price', true );

            update_post_meta($post_array->ID, '_wc_cog_cost', $Cogcost);
        }
    }
add_action( 'init', 'bulk_update_post_meta_data' );

function bulk_update_post_meta_data() {
    $args = array(
        'posts_per_page'     => -1,
        'post_type'          => 'product_variation',
        'suppress_filters'   => true
    );

    $posts_array = get_posts( $args );

    foreach ( $posts_array as $post_array ) {
        $Cogcost = get_post_meta( $post_array->ID, '_regular_price', true );

        update_post_meta( $post_array->ID, '_wc_cog_cost', $Cogcost );
    }
}

这将获得所有变体。