简体   繁体   中英

How to update price of all products in woocommerce?

I have almost 1600 products in my store. I want to increase all the the product of my store double of the current price. And want to give all products a discount of 25% after doubling the price. Doing one by one is actually so lengthy. Is there any way of achieving this once at a time?

You'll need some way to activate this function:

function reduce_all_products_price() {
    $products = wc_get_products(['limit' => -1]);
    foreach($products as $product) {
        $new_full_price = floatval($product->get_regular_price('edit')) * 2;
        $product->set_price($new_full_price);
        $product->set_sale_price($new_full_price * 0.75);
    }
}

I'm using the 'sale price' to store the value with discount, because i think it makes more sense. There are several ways to activate your function, but maybe the easiest would be to add a hook to admin_init and check for some parameter in URL.

add_action('admin_init', function () {
    if (isset($_GET['discount-everybody']) && $_GET['discount-everybody'] == 'true') {
        reduce_all_products_price();
    }
});

Then you have to be logged in to wp-admin and access yoursite.com/wp-admin/index.php?discount-everybody=true

Be careful not to do this twice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM