Unit Price for WooCommerce

For which kind of products the plugin is suitable?
1. Products which are sold by units but priced by weight (suitable for fish store and butcher).
2. Products which are sold by weight (per kg/gram, suitable for supermarket and greengrocer, deli and bakery, selling fruits, vegetables, nuts, spices).

How to display the quantity field of the ‘Archive Quantity’ option only on fully supported pages?

add_filter( 'upw_quantity_field_simple_enabled', 'upw_quantity_field_exclude_pages' );
function upw_quantity_field_exclude_pages( $enabled ) {
	return is_shop() || is_product_category() ? $enabled : false;
}

Why the Quantity Suffix isn’t displayed after the quantity field?
Make sure that this line is present in your templates/global/quantity-input.php of your theme/plugins:

do_action( 'woocommerce_after_quantity_input_field' );

How to apply some Quantity Step for all products from certain categories?
Update the categories ids and the desired Quantity Step:

add_filter( 'woocommerce_product_get__upw_step', 'upw_set_step', 10, 2 );
function upw_set_step( $step, $product ) {
	$cat_ids = [ '24', '37' ];
	$new_step = '0.01';
	return ! $step && array_intersect( $cat_ids, wc_get_product_cat_ids( $product->get_id() ) ) ? $new_step : $step;
}

How to apply some Quantity Units for all products from certain categories?
Update the categories ids and the desired Quantity Units:

add_filter( 'woocommerce_product_get__upw_measurement', 'upw_set_measurement', 10, 2 );
function upw_set_measurement( $measurement, $product ) {
	$cat_ids = [ '15', '58' ];
	$new_measurement = 'weight';
	return 'none' === $measurement && array_intersect( $cat_ids, wc_get_product_cat_ids( $product->get_id() ) ) ? $new_measurement : $measurement;
}

How to hide the products’ main price on archive pages (suitable when displaying their subtotal)?

remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price' );

How to set minimum quantity input value for all products which configured with certain Quantity Step (not compatible with the ‘Quantity Auto Update’ option)?
Update the Quantity Step and the minimum values:

add_filter( 'upw_skip_args_validation', '__return_true' );
add_filter( 'upw_product_min_value', 'upw_set_min_value' );
add_filter( 'upw_variation_min_qty', 'upw_set_min_value' );
add_filter( 'woocommerce_update_cart_validation', 'upw_apply_min_quantity', 10, 4 );

function upw_get_min_values() {
	return [
		'0.1'	=> '0.3',
		'0.01'	=> '0.5',
	];
}

function upw_set_min_value( $step ) {
	$min_values = upw_get_min_values();
	return $min_values[ $step ] ?? $step;
}

function upw_apply_min_quantity( $passed, $cart_item_key, $values, $quantity ) {
	$min_values = upw_get_min_values();
	$step = $values['data']->get_meta( '_upw_step' );
	if ( $step && isset( $min_values[ $step ] ) && $quantity > 0 && $quantity < $min_values[ $step ] ) {
		wc_add_notice( __( 'A minimum order amount', 'woocommerce' ) . ' (' . $values['data']->get_name() . '): ' . $min_values[ $step ], 'error' );
		return false;
	}
	return $passed;
}

How to set the Quantity Step as the add to cart default quantity on archive pages when not using the ‘Archive Quantity’ option?

add_filter( 'pre_option_woocommerce_enable_ajax_add_to_cart', 'wc_force_ajax_add_to_cart' );
add_filter( 'woocommerce_loop_add_to_cart_args', 'upw_set_default_quantity', 10, 2 );
add_filter( 'woocommerce_product_add_to_cart_text', 'upw_set_text_quantity', 10, 2 );

function wc_force_ajax_add_to_cart() {
	return 'yes';
}

function upw_set_default_quantity( $args, $product ) {
	$step = $product->get_meta( '_upw_step' );
	if ( $step ) {
		$args['quantity'] = $step;
	}
	return $args;
}

function upw_set_text_quantity( $text, $product ) {
	return $product->is_purchasable() && $product->is_in_stock() ? $text . ' ' . $product->get_meta( '_upw_step' ) . $product->get_meta( '_upw_quantity_suffix' ) : $text;
}

How to display the price suffix in the main variable product price when all its variations have identical one?

add_filter( 'woocommerce_variable_price_html', 'upw_set_main_price_suffix', 10, 2 );
function upw_set_main_price_suffix( $price, $product ) {
	foreach ( $product->get_available_variations( 'object' ) as $variation ) {
		$variation_suffix = $variation->get_meta( '_upw_price_suffix' );
		if ( isset( $product_suffix ) && $product_suffix !== $variation_suffix ) {
			return $price;
		} else {
			$product_suffix = $variation_suffix;
		}
	}
	return $price . ( $product_suffix ?? '' );
}

How to hide the variable products’ price range, attribute name and reset link?

add_filter( 'woocommerce_reset_variations_link', '__return_empty_string' );
add_filter( 'woocommerce_attribute_label', '__return_empty_string' );
add_filter( 'woocommerce_variable_sale_price_html', '__return_empty_string' );
add_filter( 'woocommerce_variable_price_html', '__return_empty_string' );

How to display the quantity field of simple products only on hover when using the ‘Quantity Auto Update’ option?

add_filter( 'upw_auto_update_hide_field', '__return_true' );

How to uninstall the plugin?
Deactivate the plugin, and add this code to keep seeing the prices and quantities in orders with products with decimal quantities:

remove_filter( 'woocommerce_stock_amount', 'intval' );
add_filter( 'woocommerce_stock_amount', 'floatval' );
if ( is_admin() ) {
	add_filter( 'woocommerce_quantity_input_step', 'upw_admin_quantity_step' );
}

function upw_admin_quantity_step() {
	return 'any';
}

33 responses to “Unit Price for WooCommerce”

  1. Peter Wass Avatar
    Peter Wass

    Hi Philippe,

    Great plugin! Just one issue…

    By default I show prices excluding tax and the quantity conversions work fine, but if I also want to show the price including tax (using the Woocommerce {price_including_tax} ‘Price display suffix’ option) the price including tax does not seem to use the quantity multiplication and instead shows in the default unit of measure of the product.

    All the best,

    Peter

  2. komeil Avatar

    hello
    First of all tnx for your best plugin!
    but today we have problem to edit and see products in our panel. i deactive ur plugin and every thing runs as well.
    so my php ver is 7.4.33 and woocommerce ver is 9.3.2 and i use woodmart theme ver 7.6

Leave a Reply

Your email address will not be published. Required fields are marked *