如何更新可变产品的价格

时间:2019-04-05 作者:Виктор Юстус

我对可变产品的更新价格有问题

update_post_meta( $post->ID, \'_regular_price\', (float)$meta_value  );

update_post_meta( $post->ID, \'_price\', (float)$meta_value  );
我将其用于产品ID和product\\u变体ID

我检查数据库。此字段已更新。

在商店和产品页面上显示旧价格。enter image description here

但在购物车和结账时,我看到了最新的价格enter image description here

if ($_POST[\'departments\']) {

    //convert string to array
    $vowels = array("[", "]", "\\"", "\\\\");
    $departments            = explode(",", str_replace($vowels, "", $_POST[\'departments\']));

    update_post_meta( $post_id, \'departments\', $_POST[\'departments\']  );

    foreach ($departments as $key => $value) {

        $assign_product_id      = explode(",", str_replace($vowels, "", $_POST[\'add_product_to_campaign\'][$key]));
        $assign_product_price   = explode(",", str_replace($vowels, "", $_POST[\'add_product_price_to_campaign\'][$key]));

        foreach ($assign_product_id as $key_prod => $value_prod) {
            $meta_value = trim($assign_product_price[$key_prod], \'\\\\\');
            $product_id = trim($value_prod, \'\\\\\');
            update_post_meta( $product_id, \'_regular_price\', $meta_value  );
            update_post_meta( $product_id, \'_price\', $meta_value  );

            $product_id            = (int)$product_id;

            global $wpdb;
            $variable_post = $wpdb->get_results( \'SELECT ID FROM wp_posts WHERE  post_parent = \' . $product_id);

            if ($variable_post) {

                foreach ($variable_post as $post) {
                    update_post_meta( $post->ID, \'_regular_price\', (float)$meta_value  );
                    update_post_meta( $post->ID, \'_price\', (float)$meta_value  );
                }
            }
        }
    }

} else {
    delete_post_meta( $post_id, \'departments\' );
}

1 个回复
最合适的回答,由SO网友:Виктор Юстус 整理而成

我修好了。

我在更新价格之前清除可变产品的瞬态:

wc_delete_product_transients($post_id);

相关推荐