1

Looking for a way to remove the sale price from all products in woocommerce. The admin has a limited view, so with a site that has over 10,000 products, removing the sale price manually can be a very tedious task.

Can this be done?

Andrew Lazarus
  • 989
  • 2
  • 16
  • 25
  • Well which do you want to do? They are very different questions. To only show sale items in the loop [see this](http://stackoverflow.com/a/28005478/383847) – helgatheviking Jan 20 '15 at 16:41
  • I would like to do the first option, but if it cannot be done, I would like to see all of the sale items in my shop. At the moment, the products list in the dashboard doesn't give you the option to filter by sale, so i've got 1000+ products that I cannot manage a sale on :/ – Andrew Lazarus Jan 20 '15 at 21:42

3 Answers3

9

You can bulk edit the sale price on the products admin.

Or you could drop this into your functions.php temporarily.... load the admin once, and then remove after. (untested so use at your own risk)

function so_28048702_update(){
    // in theory this will grab all variations FIRST
    $args = array( 'post_type' => array( 'product','product_variation' ), 'nopaging' => true, 'sortby' => 'post_type', 'order' => 'desc' );

    $all_products = new WP_Query( $args );

    if ( $all_products->have_posts() ) : 
    while ( $all_products->have_posts() ) : 

        $all_products->the_post();
        $id = get_the_ID();
        update_post_meta( $id, '_sale_price', '' );
        $regular_price = get_post_meta( $id, '_regular_price', true );
        update_post_meta( $id, '_price', $regular_price );

        // we're on a variable product
        if( has_term( 'variable', 'product_type', $id ) ){
           variable_product_sync( $id );
        }

    endwhile;
    endif;

    wp_reset_postdata();

}
add_action( 'admin_init', 'so_28048702_update' );
helgatheviking
  • 25,596
  • 11
  • 95
  • 152
  • Thanks Helga, how much of a risk would it be to just use this code? – Andrew Lazarus Jan 21 '15 at 12:22
  • The risk is that I potentially left some kind of typographic error. It looks good, but I haven't tried it. A typo error would cause a fatal error (white screen), but as long as you have FTP access to your site and can remove this code and recover quickly. If you have a lot of items with a sale price, we might also need to set the `_price` field with the `_regular_price` value... actually that is probably true, so I will go ahead and edit it. Lastly, variable products would need to be re-synced (just re-save) if you have a lot of variations that are on sale. – helgatheviking Jan 21 '15 at 13:04
  • When you say "variable products would need to be re-synced". Do you mean that I would have to go through each product & update them one by one, to trigger a form of cache clearing for that product? Because there are 2000+ products on sale, all variable. – Andrew Lazarus Jan 21 '15 at 16:01
  • It isn't a cache per se. WC looks at all the variations and saves the highest and lowest variation prices as data for the parent... so that it can get the price string. So, yes, you'd need to re-save the variable products to trigger this. There are a couple options: 1. use bulk edit and do several hundred at a time. 2. run this script again but modified to only query variable product, initialize the `$product` object and trigger the sync or 3. assume that variations always have a higher ID than their parents so we might be able to adjust the above. – helgatheviking Jan 21 '15 at 16:37
  • 1
    I spent last night doing option A there, going thorugh at 200 products at a time, going into each of the sale items and saving them. Bulk edit doesn't trigger it like updating the page does for some reason. I'd love to have a script to update all pages without me needing to spend an hour tapping a pattern with my hands so i forget that I'm just a data monkey there. – Andrew Lazarus Jan 21 '15 at 16:54
  • That's weird, because bulk edit should trigger `save_post` which is where I *think* the sync is happening. Anyway, I updated my code... that's probably the best I can do w/o a data-set of my own to test on. Don't forget to make a backup of your data first!! – helgatheviking Jan 21 '15 at 16:59
  • Sync would need to be done *after* the individual variation's sale prices have been updated. That is why what you did last night didn't work. (I think) – helgatheviking Jan 21 '15 at 17:03
  • This still works but the post type for product variations is 'product_variations' not 'product-variations'. Good work. – Matthew Ediger Apr 14 '21 at 19:06
2

Step 1: Remove discount prices data:

DELETE FROM wp_postmeta WHERE meta_key IN ('_sale_price', '_sale_price_dates_to', '_sale_price_dates_from', '_price');

Step 2: Insert product price based on regular price

INSERT INTO wp_postmeta SELECT NULL, post_id, '_price', meta_value FROM wp_postmeta WHERE meta_key = '_regular_price';

Why DB queries?

In my case I have

SELECT COUNT(*) FROM marin2_postmeta WHERE meta_key = '_price';
+----------+
| COUNT(*) |
+----------+
|    10213 |
+----------+
1 row in set (0.03 sec)
Maxim Colesnic
  • 408
  • 1
  • 4
  • 12
-3

You can completely remove sale badge from the product just past the below code in your function file.

remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10 );
pratik vegad
  • 176
  • 2
  • 9
  • 1
    This only hides the sale flash, it doesn't change the products price by removing the sale price. To avoid confusion, i've elaborated on the question – Andrew Lazarus Mar 29 '16 at 10:03