php - WooCommerce - Increase prices of simple and variable products -


i increase product prices percentage in woocommerce.

may using hook (example: regular price 100$ + 10%= 110$ ) for simple , variable products.

i increase simple , variable products price 10% of regular price.

how can increase prices?

thanks

there 2 cases

case 1 - products (bulk increase products prices percentage)

this custom function update products prices percentage can set @ end of code snippet. done only 1 time.

all product prices, regular prices , sale price updated…

if need time later on, see after snippet code (below), procedure.

this script work logged admin users.

function bulk_update_product_prices($percent=0){     if(get_option( 'wc_bulk_updated_prices' ) != 'yes' && is_admin() ) {          // prevent updating prices more once         add_option( 'wc_bulk_updated_prices' );          $args = array(             // wc product post types             'post_type'   => array('product', 'product_variation'),             // posts             'numberposts' => -1,             'post_status' => 'publish',         );         if($percent == 0) return;         echo 'bla bla';         $percent = 1 . '.' . $percent;         $shop_products = get_posts( $args );        foreach( $shop_products $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 option set yes prevent multiple updates.         update_option( 'wc_bulk_updated_prices', 'yes');     } }  // set percentage (if want 20%, put 20) bulk_update_product_prices(20); // <== == == == == == here set percent value 

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

now, browse page of web site (logged admin). done.

after that can remove code.


if need use script once again, need 4 steps. in code snippet above:

step 1 - reseting security option - replace this:

// set percentage (here percentage 20%, put 20) bulk_update_product_prices(20); 

by this:

// again later (resetting script) update_option( 'wc_bulk_updated_prices', 'no'); 

step 2 - browse page of web site (logged admin):

step 3 - replace this:

// again later (resetting script) update_option( 'wc_bulk_updated_prices', 'no'); 

by this:

// set percentage (here percentage 20%, put 20) bulk_update_product_prices(20); 

step 4 - **update prices - browse page of web site (logged admin)

this code tested , works.


case 2 - cart (increasing cart items price)

you can use woocommerce_before_calculate_totals hook customize cart items prices.
here in code below, add 10% each items in cart.

this works kinds of product types.

this code:

add_action( 'woocommerce_before_calculate_totals', 'add_custom_percentage', 10 ); function add_custom_percentage( $cart_object ) {     // set percent (here 10 10%)     $percent = 10;      foreach ( $cart_object->cart_contents $item ) {         $item['data']->price *= 1 . '.' . $percent;     } } 

this code tested , working.

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

references:


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -