0

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. enter image description here

But on cart and checkout i see updated price 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' );
}
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. Apr 6, 2019 at 4:47
  • @TanmayPatel updated post Apr 6, 2019 at 20:11

1 Answer 1

0

I fix it.

I clear transients for variable product before update price:

wc_delete_product_transients($post_id);

1
  • Please mark answer as accepted.
    – nmr
    Apr 6, 2019 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.