Halloween Sale 15% off Ends in See the plans
PHP

Bulk "Put on Sale" and "Remove Sale" Actions for Products

Adds bulk actions to the Products list that put selected products on sale at a set percentage, or clear their sale prices, including all variations.

Don’t have WPIDE yet? Install it on your site

Code

$wpide_bulk_sale_percent = 20; // Percent off the regular price.

add_filter( 'bulk_actions-edit-product', function ( $actions ) use ( $wpide_bulk_sale_percent ) {
    $actions['wpide_sale_on']  = sprintf( 'Put on sale (-%d%%)', $wpide_bulk_sale_percent );
    $actions['wpide_sale_off'] = 'Remove sale price';

    return $actions;
} );

add_filter( 'handle_bulk_actions-edit-product', function ( $redirect, $action, $ids ) use ( $wpide_bulk_sale_percent ) {
    if ( ! in_array( $action, array( 'wpide_sale_on', 'wpide_sale_off' ), true ) ) {
        return $redirect;
    }

    $changed = 0;

    foreach ( array_map( 'absint', (array) $ids ) as $id ) {
        $product = wc_get_product( $id );

        if ( ! $product || ! current_user_can( 'edit_product', $id ) ) {
            continue;
        }

        $targets = $product->is_type( 'variable' ) ? array_filter( array_map( 'wc_get_product', $product->get_children() ) ) : array( $product );

        foreach ( $targets as $target ) {
            $regular = (float) $target->get_regular_price( 'edit' );

            if ( 'wpide_sale_on' === $action && $regular > 0 ) {
                $target->set_sale_price( (string) round( $regular * ( 100 - $wpide_bulk_sale_percent ) / 100, wc_get_price_decimals() ) );
            } elseif ( 'wpide_sale_off' === $action ) {
                $target->set_sale_price( '' );
                $target->set_date_on_sale_from( null );
                $target->set_date_on_sale_to( null );
            }

            $target->save();
        }

        if ( $product->is_type( 'variable' ) ) {
            WC_Product_Variable::sync( $id );
        }

        $changed++;
    }

    return add_query_arg( 'wpide_sale_updated', $changed, $redirect );
}, 10, 3 );

add_action( 'admin_notices', function () {
    if ( isset( $_GET['wpide_sale_updated'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
        printf( '<div class="notice notice-success is-dismissible"><p>%s</p></div>', esc_html( sprintf( 'Sale prices updated on %d product(s).', absint( $_GET['wpide_sale_updated'] ) ) ) ); // phpcs:ignore WordPress.Security.NonceVerification
    }
} );

Notes

Set $wpide_bulk_sale_percent before use; sale prices are rounded to your store's decimals.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.