admin管理员组

文章数量:1025259

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

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

本文标签: woocommerce offtopicHow update price for variable product