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.
  • 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 ) {
	if ( $product->is_purchasable() && $product->is_in_stock() ) {
		return $text . ' ' . $product->get_meta( '_upw_step' ) . $product->get_meta( '_upw_quantity_suffix' );
	}
	return $text;
}

How to add the quantity suffix also near the product price?

if ( ! is_admin() ) {
	add_filter( 'woocommerce_get_price_suffix', 'upw_add_price_suffix', 10, 4 );
}
function upw_add_price_suffix( $html, $product, $price, $qty ) {
	$suffix = $product->get_meta( '_upw_quantity_suffix' );
	return $suffix ? $html . '/' . $suffix : $html;
}

How to display the product subtotal by the selected quantity in simple products?

// remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price' );
add_action( 'woocommerce_after_add_to_cart_button', 'upw_product_price_quantity' );
add_action( 'woocommerce_after_shop_loop_item', 'upw_archive_product_price_quantity' );
add_action( 'wp_footer', 'upw_archive_product_price_quantity_js' );

function upw_product_price_quantity() {
	global $product;
	if ( $product->is_type( 'simple' ) ) {
		echo '<div id="subtotal" style="display: inline-block;">' . __( 'Total:', 'woocommerce' ) . ' <span></span></div>'; ?>
		<script type="text/javascript">
		jQuery( function( $ ) {
			$( 'input[name=quantity]' ).on( 'change', function() {
				$( '#subtotal > span' ).html( ( '<?php echo wc_get_price_to_display( $product ); ?>' * $( this ).val() ).toFixed( 2 ) + '<?php echo get_woocommerce_currency_symbol(); ?>' );
			} );
		} );
		</script>
	<?php }
}

function upw_archive_product_price_quantity() {
	global $product;
	if ( ( is_shop() || is_product_category() ) && $product->is_type( 'simple' ) ) {
		echo '<div id="subtotal" style="display: inline-block;">' . __( 'Total:', 'woocommerce' ) . '<span></span></div><div id=product_price style="display: none;"><span>' . wc_get_price_to_display( $product ) . '</span></div>';
	}
}

function upw_archive_product_price_quantity_js() {
	if ( is_shop() || is_product_category() ) { ?>
		<script type="text/javascript">
		jQuery( function( $ ) {
			$( document ).ready( function() {
				$( 'li.product-type-simple' ).find( 'input.qty' ).each( function() {
					if ( $( this ).val() > 0 ) {
						$( this ).parent().parent().find( '#subtotal > span' ).html( ' ' + ( $( this ).parent().parent().find( '#product_price > span' ).html() * $( this ).val() ).toFixed( 2 ) + '<?php echo get_woocommerce_currency_symbol(); ?>' );
					} else {
						$( this ).parent().parent().find( '#subtotal' ).css( 'visibility', 'hidden' );
					}
				} );
			} );
			$( 'input[name=quantity]' ).on( 'change', function() {
				if ( $( this ).val() > 0 ) {
					$( this ).parent().parent().find( '#subtotal' ).css( 'visibility', 'visible' );
					$( this ).parent().parent().find( '#subtotal > span' ).html( ' ' + ( $( this ).parent().parent().find( '#product_price > span' ).html() * $( this ).val() ).toFixed( 2 ) + '<?php echo get_woocommerce_currency_symbol(); ?>' );
				} else {
					$( this ).parent().parent().find( '#subtotal' ).css( 'visibility', 'hidden' );
				}
			} );
		} );
		</script>
	<?php }
}

How to display the product subtotal by the selected quantity in variable products?

add_filter( 'woocommerce_available_variation', 'upw_variable_quantity_price', 10, 3 );
add_action( 'wp_footer', 'upw_display_quantity_price' );

function upw_variable_quantity_price( $data, $product, $variation ) {
	$data['price_html'] .= '<div id="product_total_price" class="total-price-qty variable_products">' . __( 'Total:', 'woocommerce' ) . '<span class="price">' . $data['display_price'] . '</span></div>';
	return $data;
}

function upw_display_quantity_price() {
	if ( is_product() || is_shop() || is_product_category() ) { ?>
		<script type="text/javascript">
		jQuery( function( $ ) {
			$( '.variations_form' ).each( function() {
				$( this ).on( 'found_variation', function( event, variation ) {
					$( 'input[name=quantity]' ).change( function() {
						if ( $( this ).val() > 0 ) {
							$( '#product_total_price' ).show();
							$( '#product_total_price .price' ).html( '<?php echo get_woocommerce_currency_symbol(); ?>' + parseFloat( variation.display_price * $( this ).val() ).toFixed( 2 ) );
						} else {
							$( '#product_total_price' ).hide();
						}
					} );
				} );
			} );
		} );
		</script>
	<?php }
}

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 *