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 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 add suffix to the variable product price range?
Update the product id and suffix:

add_filter( 'woocommerce_format_price_range', 'upw_format_price_range_prefix' );
function upw_format_price_range_prefix( $price ) {
	$product_ids = [
		'317'	=> '/kg',
		'329'	=> '/gr',
	];
	global $product;
	return isset( $product_ids[ $product->get_id() ] ) ? $price . $product_ids[ $product->get_id() ] : $price;
}

How to set minimum quantity input value for all simple products which configured with certain quantity step (and variable product in cart page)?
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( '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;
}

Does the quantity field can be displayed for products in custom pages?
Yes, but some features may not work as expected.

Integrations

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

Leave a comment

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