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 = [ '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_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';
}

21 responses to “Unit Price for WooCommerce”

  1. Richard LOBATO Avatar
    Richard LOBATO

    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

    1. Condless Avatar

      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. Andrei Avatar
    Andrei

    ישר כוח! תוסף מאד שימושי

  3. Dalmonte Avatar
    Dalmonte

    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.

    1. Condless Avatar

      Hi Dalmonte,
      What’s the website URL?

  4. Emmanuel BLANCHARD Avatar
    Emmanuel BLANCHARD

    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.

    1. Condless Avatar

      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.

  5. mert Avatar

    Hello, is it possible to support price per litre as well?
    or can we change weight to litre?
    Thanks

    1. Condless Avatar

      Hi Mert,
      It will require customization to display the price per liter, please contact us.

  6. Joseph Rosenstock Avatar

    Wonderful work!
    I am using Oxygen Builder using its repeater. For some reason the subtotal is the same for all the
    Entrees in that loop. So I disabled that the subtotal from showing in front-end from the backend. It does not effect the correct total in the shopping cart.
    My client wants to sell fried chicken per piece and charge by pound. How can we do this with your plugin?

    1. Condless Avatar

      Hi Joseph,
      Set the price of 1 unit of Chicken in the price field (General tab), Set the weight of 1 unit of Chicken in the weight field (Shipping tab), Select ‘Weight’ in the ‘Quantity Units’ option (Unit Price tab).

  7. khoury Avatar
    khoury

    its work only for price?
    can is work for tag ?

    1. Condless Avatar

      היי,
      התוסף מאפשר למכור מוצרים שנמכרים במשקל או מוצרים שנמכרים ביחידות אך מתומחרים במשקל,
      מה הכוונה לפעול ב-tags?

    2. wadea Avatar
      wadea

      היי
      אני רוצה להציג נתונים הנמצאים ב attributes
      ראה תמונה https://prnt.sc/wDy5JUGG17MB
      אני רוצה לעשוב חישוב כזה
      הנתון הנמצא בattributse מסוים יחוקל ל מספר שאני בוחר

      יש אפשרות כזו?

    3. Condless Avatar

      יהיה צורך בפיתוח או בתוסף אחר.

  8. gabi Avatar
    gabi

    Hey. I’m looking for a supplement that will show in the store products of drinking bottles a price per 100 milligrams below the price of the product. Does this plugin allow this?

    1. Condless Avatar

      היי גבי,
      תוסף זה מאפשר למכור מוצרים במשקל, אם יש צורך רק בהצגת המחיר ל-100 מ”ל יהיה צורך בפיתוח או בתוסף אחר.

  9. Carissa Avatar

    Order editing has been added to the Woo App. Quantity steps do not work in the app. you can only change the quantity by whole number. Do you have a solution for this or another update coming soon?

    1. Condless Avatar

      Hi Carissa,
      Unfortunately the Woo App doesn’t support decimal quantites on order editing.

  10. Julien Avatar

    Hi, thank you for your free plugin that perfectly works.

    I was just wondering if there was a way to perfectly align the total price with the add to cart button and the text that is the same line, it’s a few pixel upper. And also if you could set a margin between the price and the button, so it can match with the style of the page

    1. Condless Avatar

      Hi Julien,
      Please try the following CSS:
      div#subtotal { margin: 5px; }

Leave a Reply

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