Unit Price for WooCommerce

Which pricing options the plugin offers?

  • Selling products by weight (kilogram/gram)- suitable for fruits and vegetables.
  • Selling products by units but pricing it by weight (kg/gram)- suitable for chicken and fish, supports authorize payment.
  • Selling products both by units and by weight (by creating variable product), and the option for pricing by weight the variation which is sold by units.

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 only on hover when using the Quantity Auto Update option?

add_filter( 'upw_auto_update_hide_field', '__return_true' );

How to apply the measurements option on all the products that configured as ‘weight’ via the WooCommerce Price Calculator plugin?

add_filter( 'woocommerce_product_get__upw_measurement', 'upw_set_measurement', 10, 2 );
function upw_set_measurement( $measurment, $product ) {
	return is_plugin_active( 'woocommerce-measurement-price-calculator/woocommerce-measurement-price-calculator.php' ) && 'weight' === ( new WC_Price_Calculator_Settings( $item->get_product() ) )->get_calculator_type() ? 'mpc' : $measurment;
}

How to set 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 set the add to cart default quantity on archive pages when the quantity input is absent (suitable only if the AJAX add to cart option is enabled)?

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 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 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 steps 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 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 display the quantity field (of the archive quantity option) only on fully supported pages (archive 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;
}
Integrations

OceanWP theme doesn’t supports in all the the decimal numbers (supports 0.1, 0.25, 0.5 and more).

How to make the quantity archive option compatible with OceanWP theme?

add_filter( 'upw_simple_qauntity_base_trigger', 'upw_oceanwp_base_trigger' );
function upw_oceanwp_base_trigger( $trigger ) {
	return 'change';
}

How to make Quantity Suffix compatible with Qty Increment Buttons plugin?
Add this into the end of the plugin’s quantity-input template:

do_action( 'woocommerce_after_quantity_input_field' );

How to make Quantity Suffix compatible with Yith Request a Quote plugin?

add_filter( 'ywraq_request_quote_view_item_data', 'upw_save_suffix', 10, 3 );
add_action( 'woocommerce_after_quantity_input_field', 'upw_display_suffix' );

function upw_save_suffix( $item_data, $raq, $_product ) {
	$_GET['quantity_suffix'] = $_product->get_meta( '_upw_quantity_suffix' );
	return $item_data;
}

function upw_display_suffix() {
	if ( isset( $_GET['quantity_suffix'] ) ) {
		echo wc_clean( $_GET['quantity_suffix'] );
	}
}

How to make Quantity Suffix compatible with WoodMart theme (CSS)?

span.quantity-suffix { font-size: 14px; }

How to disable the extra quantity field in FiboSearch search bar?

add_filter( 'shortcode_atts_product_add_to_cart', 'upw_check_if_fibo' );
add_filter( 'upw_quantity_field_simple_enabled', 'upw_disable_quantity_fibo' );
add_filter( 'upw_enable_shop_quantity', 'upw_disable_quantity_fibo' );
add_filter( 'upw_auto_update_excluded_quantity_input_name', 'upw_disable_fibo_auto_update' );

function upw_check_if_fibo( $out ) {
	$_POST['fibo'] = ! $out['show_price'];
	return $out;
}

function upw_disable_quantity_fibo( $enabled ) {
	return isset( $_POST['fibo'] ) && $_POST['fibo'] ? false : $enabled;
}

function upw_disable_fibo_auto_update( $excluded ) {
	return 'js-dgwt-wcas-quantity';
}

How to sell by units products which are priced by weight and their prices are synced vie the REST API by kg?

add_filter( 'woocommerce_product_get_regular_price', 'upl_modify_product_price', 10, 2 );
add_filter( 'woocommerce_product_get_price', 'upl_modify_product_price', 10, 2 );
function upl_modify_product_price( $price, $product ) {
	return 'weight' === $product->get_meta( '_upw_measurement' ) && $product->get_weight() ? round( $price * $product->get_weight(), 2 ) : $price;
}

How to integrate with Bridge/Enfold themes?
Link.

How to sync decimal stock via the REST API?
Link.

How to set the admin minimum and step quantity when edit orders?
Link.

Uninstall

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

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

5 comments

  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

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

Leave a comment

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