最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

woocommerce offtopic - How update price for variable product

matteradmin7PV0评论

I have a problem with update price for variable product

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

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

I use it for product ID and product_variation ID

I check DB. This Field updated.

On shop and the product page shows the old price.

But on cart and checkout i see updated price

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' );
}

I have a problem with update price for variable product

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

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

I use it for product ID and product_variation ID

I check DB. This Field updated.

On shop and the product page shows the old price.

But on cart and checkout i see updated price

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' );
}
Share Improve this question edited Apr 6, 2019 at 20:10 Виктор Юстус asked Apr 5, 2019 at 14:50 Виктор ЮстусВиктор Юстус 15 bronze badges 2
  • Can you please let me know which code you have used for header cart??? Can you please update the code in question so I can check it and will help you to fix this. – Tanmay Patel Commented Apr 6, 2019 at 4:47
  • @TanmayPatel updated post – Виктор Юстус Commented Apr 6, 2019 at 20:11
Add a comment  | 

1 Answer 1

Reset to default 0

I fix it.

I clear transients for variable product before update price:

wc_delete_product_transients($post_id);

Post a comment

comment list (0)

  1. No comments so far