2014-07-01 126 views
4

我創建了一個插件,它使用update_post_meta函數來更新產品的變化價格。Woocommence`update_post_meta`沒有更新數據庫值

如果我有一個產品xid:5)和變化yid:400)和我運行update_post_meta(400,"_regular_price",13.00);它不更新數據庫。這是非常奇怪的,因爲當我點擊Edit Product(wp-admin)更新後的價格13.00顯示在變體面板中,我必須點擊Update以供客戶查看。這是正常的行爲,如果是的話,update_post_meta函數執行後如何更新數據庫?

(Image) Price after update_post_meta()摘要頁

(Image) Price after same update. Edit Product page

這裏是我做批量更新

// $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; 

我也問過同樣的問題代碼,但WordPress的論壇 http://wordpress.org/support/topic/update_post_meta-is-not-updating-the-actual-values?replies=1#post-5742842

+0

你已經解決了這個問題?我遇到同樣的問題... – Lorenzo

回答

0

在上面的代碼中沒有答覆,$ variation_value變量值,可能不會被分配。因此,如果不滿足條件並且update_post_meta()未被執行,則存在這種可能性。

我們可以使用下面的代碼代替批量更新。

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


?> 

爲常規價格變量$ regular_price分配值。

+0

這是避免儘可能運行手動SQL查詢的最佳實踐。首先應該使用get_available_variations()等內置函數,第二個選擇是WP_query。只有在極少數情況下,當您需要製作更復雜的查詢時,您應該運行自定義SQL。 – Pelmered

0

我認爲你們都做得比它需要的複雜得多。

首先,什麼是該檢查的目的:

if($attribute_value == $variation_value) 

$variation_value變量不縫在所有因此測試不能被設置,update_post_meta()不運行。

我簡化你的代碼到這個(這是測試和工程):

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

} 
+0

我應該提到的是,'$ attribute_value'和'$ variation_value'是在手邊設置的,並且工作正常,我的注意力集中在'update_post_meta'上,我將嘗試@ JOHn-e提及的'wc_delete_product_transients'。 – Kivylius

1

在woocommerce(2.1.12版本):

woocommerce /包括/ WC-產品的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); 

看來你必須更新「_price」元,甚至刪除瞬態來更新產品價格。

要刪除產品短暫的呼叫:

wc_delete_product_transients($product_id); 
+0

我可以在代碼中完美地更改價格而不會刪除瞬變。 –

+0

小心分享,您的代碼與OP? –

+0

爲什麼在這裏重複同樣的事情,其他人已經說過了? –