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).

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 = [ '37', '24' ];
	$new_step = '0.01';
	return ! $step && array_intersect( $cat_ids, wc_get_product_cat_ids( $product->get_id() ) ) ? $new_step : $step;
}

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_set_min_value( $step ) {
	$min_values = [
		'0.1'	=> '0.3',
		'0.01'	=> '0.5',
	];
	return isset( $min_values[ $step ] ) ? $min_values[ $step ] : $step;
}

function upw_apply_min_quantity( $passed, $cart_item_key, $values, $quantity ) {
	$min_values = [
		'0.1'	=> '0.3',
		'0.01'	=> '0.5',
	];
	$step = $values['data']->get_meta( '_upw_step' );
	if ( $step && isset( $min_values[ $step ] ) && $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 ) {
	$product_suffix = '';
	foreach ( $product->get_available_variations( 'object' ) as $variation ) {
		$variation_suffix = $variation->get_meta( '_upw_price_suffix' );
		if ( $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' );

Why sometimes there are many digits in the quantity field when using decimal Quantity Step?
Try Quantity Step without zero’s at its end (for example 0.4 instead 0.40), try another Quantity Step (as 0.25/0.5), try the woocommerce built-in quantity buttons, some themes supports only specific Quantity Step as 0.1/0.5 (like OceanWP).

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

9 responses to “Unit Price for WooCommerce”

  1. Hello, congratulations for your plugin Unit Price for WooCommerce.
    I just have that issue about the following option for VARIABLE PRODUCT :
    – Display the product price by the selected quantity in real-time
    On the product page The TOTAL price format is not matching the price format set in woocommerce settings… In my case the currency symbol should be on the right with space and decimal separator with “,” (ex: 1,55 €) but the TOTAL PRICE set by your plugin show the currency symbol on the left and decimal separator with “.” (ex: €1.55)

    Another issue for variable products > On the product page if all the variation have the same price it does not show the price with price suffix (ex: 1,55 € / kg)

    Thank you,
    Best.
    Richard

    • Hi Richard,
      Please update the plugin to its latest release.
      The docs was updated to include the code for ‘How to display the price suffix in the main variable product price’.

  2. Hello, it happens that when I select the quantity of a product it does not add the minimum quantity established for the product, it adds it as a unit therefore it does not perform the calculation in real time in the cart, I have to go to the cart and modify the desired quantity again to update the amount.

  3. Thank you for this free plugin. I want to modify the code to separate the quantity step from the minimum and default value. Have you any collaborative repository on github from which I could work by creating a new branch ?
    Thank you for your answer.

    • Hi Emmanuel,
      The ‘upw_product_min_value’ and ‘upw_product_input_value’ filters can be used to set the minimum and the default value of the products.

Leave a Reply

Your email address will not be published.