surendar
surendar

Reputation: 29

WooCommerce - Increase prices of simple and variable products

I would like to increase all product prices by some percentage in WooCommerce.

May be using a hook (example: regular price 100$ + 10%= 110$ ) for simple and variable products.

I would like to increase all simple and variable products price to extra 10% of the regular price.

How can I increase prices?

Thanks

Upvotes: 2

Views: 2640

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253921

THERE IS 2 CASES

CASE 1 - all products (Bulk increase products prices by a percentage)

This custom function will update all products prices by a percentage that you can set at the end of this code snippet. This will be done only one time.

All the product prices, regular prices and sale price will be updated…

If you need to do it another time later on, see after this snippet code (below), the procedure.

This script will work only for logged admin users.

function bulk_update_product_prices($percent=0){
    if(get_option( 'wc_bulk_updated_prices' ) != 'yes' && is_admin() ) {

        // prevent updating prices more than once
        add_option( 'wc_bulk_updated_prices' );

        $args = array(
            // WC product post types
            'post_type'   => array('product', 'product_variation'),
            // all posts
            'numberposts' => -1,
            'post_status' => 'publish',
        );
        if($percent == 0) return;
        echo 'bla bla';
        $percent = 1 . '.' . $percent;
        $shop_products = get_posts( $args );
       foreach( $shop_products as $item){
            $meta_data = get_post_meta($item->ID);
            if (!empty($meta_data['_regular_price'])) {
                $regular_price = $meta_data['_regular_price'][0] * $percent;
                update_post_meta($item->ID, '_regular_price', $regular_price);
            }
            if (!empty($meta_data['_sale_price'])) {
                $sale_price = $meta_data['_sale_price'][0] * $percent;
                update_post_meta($item->ID, '_sale_price', $sale_price);
            }
            if (!empty($meta_data['_price'])) {
                $price = $meta_data['_price'][0] * $percent;
                update_post_meta($item->ID, '_price', $price);
            }
        }
        // Once done an option is set to yes to prevent multiple updates.
        update_option( 'wc_bulk_updated_prices', 'yes');
    }
}

// set your percentage (if you want 20%, so you put 20)
bulk_update_product_prices(20); // <== == == == == == Here set your percent value

This code goes on function.php file of your active child theme (or theme) or in any plugin file.

NOW, browse a page of your web site (logged as admin). you are done.

After that you can remove this code.


If you need to use this script once again, you will need to do this 4 steps. in the code snippet above:

Step 1 - Reseting security option - Replace this:

// set your percentage (here the percentage is 20%, so we put 20)
bulk_update_product_prices(20);

By this:

// Do it again later (Resetting the script)
update_option( 'wc_bulk_updated_prices', 'no');

Step 2 - browse a page of your web site (logged as admin):

Step 3 - Replace back this:

// Do it again later (Resetting the script)
update_option( 'wc_bulk_updated_prices', 'no');

By this:

// set your percentage (here the percentage is 20%, so we put 20)
bulk_update_product_prices(20);

Step 4 - **Update prices - browse a page of your web site (logged as admin)

This code is tested and works.


CASE 2 - Cart (Increasing cart items price)

You can use woocommerce_before_calculate_totals hook to customize your cart items prices.
Here in the code below, I add 10% to each items in cart.

This works for all kinds of product types.

This is the code:

add_action( 'woocommerce_before_calculate_totals', 'add_custom_percentage', 10 );
function add_custom_percentage( $cart_object ) {
    // set your percent (here 10 is 10%)
    $percent = 10;

    foreach ( $cart_object->cart_contents as $item ) {
        $item['data']->price *= 1 . '.' . $percent;
    }
}

This code is tested and working.

Naturally this code goes on function.php file of your active child theme (or theme) or in any plugin file.

References:

Upvotes: 1

Related Questions