Kivylius
Kivylius

Reputation: 6547

Woocommence `update_post_meta` is not updating database values

I creating a plugin that uses the update_post_meta function to update variation prices of products.

If I have a product x (id:5) and a variation y (id:400) and I run the update_post_meta(400,"_regular_price",13.00); It's not updating the database. It's extremely strange as when I click on Edit Product (wp-admin) the updated price 13.00 shows up in variations panel and I have to click Update for it to update for customers to see. Is this regular behavior and if so how to update the database as soon as the update_post_meta function executes?

(Image) Price after update_post_meta() Summary page

.

(Image) Price after same update. Edit Product page

Here is my code for doing the bulk updates

// $attribute_value/$variation_value are set correctly!
while ($loop->have_posts() ) : $loop->the_post();
    global $product;
    $variations = new WC_Product_Variable($product->post->ID);
    $variations = $variations->get_available_variations();
    foreach ($variations as $key => $variation){
        foreach ($variation["attributes"] as $key => $attribute_value):
            if($attribute_value == $variation_value):
                update_post_meta( $variation['variation_id'], '_regular_price', $regular_price);
            endif;
        endforeach;
    }
endwhile;

I have asked the same question but no reply on Wordpress forums http://wordpress.org/support/topic/update_post_meta-is-not-updating-the-actual-values?replies=1#post-5742842

Upvotes: 4

Views: 4834

Answers (4)

Yura Nykolyshyn
Yura Nykolyshyn

Reputation: 1

It's because update_post_meta - updates other DB tab. It updates wp_postmeta. You need to update value in wp_wc_product_meta_lookup. I've used SQL command for this.

Upvotes: 0

Engit
Engit

Reputation: 159

In woocommerce (version 2.1.12):

woocommerce/includes/wc-product-functions.php:417

$regular_price = get_post_meta( $product_id, '_regular_price', true );

update_post_meta( $product_id, '_price', $regular_price );
update_post_meta( $product_id, '_sale_price', '' );
update_post_meta( $product_id, '_sale_price_dates_from', '' );
update_post_meta( $product_id, '_sale_price_dates_to', '' );

wc_delete_product_transients( $product_id );

It seems you'll have to update the "_price" meta and even delete the transients to update the product price.

To delete product transient call:

wc_delete_product_transients( $product_id );

Upvotes: 2

Pelmered
Pelmered

Reputation: 2882

I think you both are making this much more complicated than it needs to be.

First of all, what is the purpose of the this check:

if($attribute_value == $variation_value)

The $variation_value variable does not seam to be set at all the therefore the test fails and the update_post_meta() does not run.

I simplified your code into this(this is tested and works):

//Setup WP_Query
$args = array(
    //Set any arguments you want here to select the products
    'post_type' => 'product', //Make sure it is the correct post type
    'post__in'  => array(157, 156) //Example post IDs
);

$loop = new WP_Query($args);

//The price to set
$regular_price = 110;

//Loop through query results
while ($loop->have_posts() )
{
    //Setup post data
    $loop->the_post();

    //Instantiate the product class 
    $product = new WC_Product_Variable(get_the_ID());

    //Get all variations
    $variations = $product->get_available_variations();

    //Loop though variations and update
    foreach ($variations as $variation){
        update_post_meta( $variation['variation_id'], '_regular_price', $regular_price);
    }

}

Upvotes: 0

Domain
Domain

Reputation: 11808

In above code, $variation_value variable value, may not be assigned. Therefore, there is a possibility that, if condition is not satisfied and update_post_meta() is not executed.

We can use below code instead, for bulk updation.

<?php

    $get_all_products_query = "SELECT `ID`
                FROM `". $wpdb->prefix . "posts`
                WHERE `post_type` LIKE 'product'
                AND `post_status` = 'publish'
                ORDER BY `ID` DESC";

     $get_all_products_result = $wpdb->get_results($get_all_products_query);

     $regular_price = '';

     if(!empty($get_all_products_result))
     {
     foreach($get_all_products_result as $single_product)
     {
         $product_parent_id = $single_product->ID;

        //Get all variations of single product

        $query = "SELECT `post_id`
              FROM `" . $wpdb->prefix . "postmeta`
              WHERE `meta_key` = 'attribute_pa_product'
              AND `post_id`
              IN (
              SELECT `ID`
              FROM `" . $wpdb->prefix . "posts`
              WHERE `post_parent` = " . $product_parent_id . "
              )";

        $variation_result = $wpdb->get_results($query);

        if(!empty($variation_result))
        {
            //As one product may have multiple variation

            foreach($variation_result as $single_variation)
            {
              $post_id = $single_variation->post_id;

                          update_post_meta( $post_id, '_regular_price', $regular_price);
            }
        }
     }
     }


?>

Assign value for the Regular price variable, $regular_price.

Upvotes: 0

Related Questions