Tag Archives: WordPress

Change product price in WooCommerce programmatically

function change_price_by_type( $product_id, $multiply_price_by, $price_type ) {
	$the_price = get_post_meta( $product_id, '_' . $price_type, true );
	$the_price *= $multiply_price_by;
	update_post_meta( $product_id, '_' . $price_type, $the_price );
}

function change_price_all_types( $product_id, $multiply_price_by ) {
	change_price_by_type( $product_id, $multiply_price_by, 'price' );
	change_price_by_type( $product_id, $multiply_price_by, 'sale_price' );
	change_price_by_type( $product_id, $multiply_price_by, 'regular_price' );
}

/*
 * `change_product_price` is main function you should call to change product's price
 */
function change_product_price( $product_id, $multiply_price_by ) {
	change_price_all_types( $product_id, $multiply_price_by );	
	$product = wc_get_product( $product_id ); // Handling variable products
	if ( $product->is_type( 'variable' ) ) {
		$variations = $product->get_available_variations();
		foreach ( $variations as $variation ) {
			change_price_all_types( $variation['variation_id'], $multiply_price_by );
		}
	}
}

Add a custom currency and currency symbol in WooCommerce

add_filter( 'woocommerce_currencies', 'add_my_currency' );
 
function add_my_currency( $currencies ) {
     $currencies['ABC'] = __( 'Currency name', 'woocommerce' );
     return $currencies;
}
 
add_filter('woocommerce_currency_symbol', 'add_my_currency_symbol', 10, 2);
 
function add_my_currency_symbol( $currency_symbol, $currency ) {
     switch( $currency ) {
          case 'ABC': $currency_symbol = '$'; break;
     }
     return $currency_symbol;
}