2017-08-24 136 views

回答

3

这是一个纯粹的MySQL查询,你正在寻找:

首先,你必须使用自联接更新_price_regular_price

UPDATE `wp_postmeta` AS a, 
     `wp_postmeta` AS b 
SET a.`meta_value` = b.meta_value 
WHERE a.post_id = b.post_id 
    AND a.meta_key IN ('_price', '_regular_price') 
    AND b.meta_key = '_sale_price' 
    AND b.meta_value != '' 
    AND a.`post_id` IN 
    (SELECT `ID` 
    FROM `wp_posts` 
    WHERE `post_type` = 'product' 
     AND `post_status` = 'publish'); 

我们重新销售价格和日期必须设置为空白

UPDATE `wp_postmeta` 
SET `meta_value` = '' 
WHERE `meta_key` IN ('_sale_price', '_sale_price_dates_from', '_sale_price_dates_to') 
    AND `post_id` IN 
    (SELECT `ID` 
    FROM `wp_posts` 
    WHERE `post_type` = 'product' 
     AND `post_status` = 'publish'); 

如果你想删除这个字段,那么在WHERE子句中使用DELETE语句,它将解决你的目的。

你也需要删除存储在wp_options桌子底下_transient_timeout_wc_var_prices_{{post_id}}_transient_wc_var_prices_{{post_id}}option_name

DELETE 
FROM `wp_options` 
WHERE (`option_name` LIKE '_transient_wc_var_prices_%' 
    OR `option_name` LIKE '_transient_timeout_wc_var_prices_%') 

特别感谢@LoicTheAztec用于指明了产品价格缓存WooCommerce产品价格缓存

上面的查询经过测试和为我工作。

运行该查询不要采取数据库备份

希望这有助于之前!

+2

完美地工作谢谢你,感谢@LoicTheAztec也。 – user2679140

2

这里是一个自定义函数,将销售价格复制到正常价格,并会重新出售price.Regarding在WooCommerce价格有:

  • 活动价:'_price'
  • 常规价格:'_regular_price'
  • 销售价格:'_sale_price'

所以,你需要更新太正常价格,并重置所有RELA销售价格的数据。你也需要刷新价格。
此功能的用户界面仅出现在商店页面或单个产品页面中,仅用于管理员和店铺经理

如果你有超过1000个产品,过程将在多个步骤自动分割(由1000个产品),以避免出现错误时,有太多的产品。

SQL查询将仅选择具有销售价格的产品ID,而不是所有产品。要更新/重置价格,我使用update_post_meta()函数而不是查询来刷新每个产品缓存...

它看起来像:

  • 开始更新价格(第一步):

enter image description here

  • 继续必要的步骤:

enter image description here

  • 更新价格完成(结束画面):

enter image description here

重要:开始这种进程之前总是让数据库备份

这是那个挂钩的功能代码:

add_action('woocommerce_before_main_content', function(){ 

    // Only admins and shop manargers 
    if(! current_user_can('edit_products')) return; 

    global $wpdb; 
    $products_count = get_option('product_prices_update_count'); 

    // Auto enable multistep price updates for more than 1000 products 
    $limit = $products_count > 2 ? 2 : false; 

    if($limit != false) 
     $offset = get_option('product_prices_update_offset'); 

    if(empty($offset) && $limit != false) { 
     $offset = 0; 
     add_option('product_prices_update_offset', $offset); 
    } 

    $control_process = $limit != false ? "LIMIT $offset, $limit" : ""; 

    if(! isset($_POST['prices_updates'])) $control_process = ''; 

    // 1. First query: Get for all product ids the sale price 
    $results = $wpdb->get_results(" 
     SELECT postmeta.post_id, postmeta.meta_value as price 
     FROM {$wpdb->prefix}postmeta as postmeta 
     INNER JOIN {$wpdb->prefix}posts as posts ON postmeta.post_id = posts.ID 
     WHERE posts.post_type LIKE '%product%' 
     AND postmeta.meta_key = '_sale_price' 
     AND postmeta.meta_value != '' 
     ORDER BY posts.ID ASC 
     $control_process 
    "); 

    if(empty($products_count)){ 
     update_option('product_prices_update_count', count($results)); 
     $count = count($results); 
    } else $count = $products_count; 

    $remaining = ! empty($products_count) && $limit != false ? $count-($offset+$limit) : count($results); 

    $products_updated = 0; 

    echo '<div style="border: solid 1px grey; padding: 16px; margin-bottom: 12px;)"> 
    <form class="cart" method="post" enctype="multipart/form-data">'; 

    if(isset($_POST['prices_updates']) && ($remaining > 0 || ! $limit)){ 

     foreach($results as $result){ 

      $post_id = $result->post_id; 
      $price = $result->price; 

      // 2. Updating Active and Regular prices 
      update_post_meta($post_id, '_price', $price); 
      update_post_meta($post_id, '_regular_price', $price); 

      // 3. Reset Sale price 
      update_post_meta($post_id, '_sale_price', ''); 
      update_post_meta($post_id, '_sale_price_dates_from', ''); 
      update_post_meta($post_id, '_sale_price_dates_to', ''); 

      // 4. Refresh product cache 
      wc_delete_product_transients($post_id); 

      $products_updated++; 
      // echo "$post_id ($products_updated), "; 
     } 
    } 

    if((! $limit && ! empty($products_count)) || $remaining <= 0){ 
     echo "<p><strong>Process is now finished.</strong><br> 
     <em>You can remove or comment the code, Thanks</em></p>"; 
     if(isset($_POST['prices_updates'])){ 
      delete_option('product_prices_update_offset'); 
      delete_option('product_prices_update_count'); 
     } 
    } else { 
     if(isset($_POST['prices_updates'])){ 
      update_option('product_prices_update_offset', $offset+$limit); 
      echo "<p>$products_updated products have been updated.<br>"; 
      echo "There is still $remaining remaining products to update.</p>"; 
     } else 
      echo "<p>There is $remaining products to update.</p>"; 

     $value = empty($products_count) ? "Start update" : "Continue update"; 
     echo '<input type="submit" class="button" name="prices_updates" value="'.$value.'" />'; 
    } 
    echo '</form></div>'; 

}, 2, 0); 

代码放在您的活动子主题(或主题)的function.php文件或也以任何插件文件。

此代码已经过测试并可正常工作。

+0

像往常一样很好的答案+1。我试着用MySQL查询。 –

+0

@RaunakGupta谢谢......我一直在阐述一个用户界面,因为人们经常遇到直接SQL查询问题,并且有些答案保持沉睡......产品缓存也存在这个问题......我不会尝试你的答案,但是因为你差不多是专家在SQL => +1对你也是如此... – LoicTheAztec

+0

做woocommerce缓存的价格还可以吗?我不知道这一点。如果你知道钥匙,你可以告诉我。 –

相关问题