Tag Archives: WooCommerce

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;
}

Add a custom global product tab in WooCommerce

add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {
	
	// Adds the new tab
	
	$tabs['test_tab'] = array(
		'title' 	=> __( 'New Product Tab', 'woocommerce' ),
		'priority' 	=> 50,
		'callback' 	=> 'woo_new_product_tab_content'
	);

	return $tabs;

}
function woo_new_product_tab_content() {

	// The new tab content

	echo '<h2>New Product Tab</h2>';
	echo '<p>Here\'s your new product tab.</p>';
	
}

Replace the description tab with a custom function in WooCommerce

add_filter( 'woocommerce_product_tabs', 'woo_custom_description_tab', 98 );
function woo_custom_description_tab( $tabs ) {

	$tabs['description']['callback'] = 'woo_custom_description_tab_content';	// Custom description callback

	return $tabs;
}

function woo_custom_description_tab_content() {
	echo '<h2>Custom Description</h2>';
	echo '<p>Here\'s a custom description</p>';
}