Name Your Price: JavaScript events

This is a developer-level doc. It’s written for developers who are comfortable with PHP, JavaScript and WordPress hooks. If you aren’t, a Woo agency partner or developer can help. Custom code isn’t covered by our Support Policy, so we can’t write, debug or maintain customizations for you.

Name Your Price’s script checks the price field while the customer types and fires jQuery events on the product’s add-to-cart form (form.cart). Listen for them to react to the amount, for example to show a running total or to change your own button. For where to put the code, see Developers.

Example: show the amount elsewhere on the page

↑ Back to top
jQuery( function ( $ ) {
	$( 'form.cart' ).on( 'wc-nyp-updated', function ( event, nypProduct ) {
		var price = nypProduct.getPrice();
		$( '.my-amount-preview' ).text( price ? wc_nyp_format_price( price ) : '' );
	} );
} );

Load your script after Name Your Price’s own (script handle woocommerce-nyp).

Events

↑ Back to top
  • wc-nyp-updated
    Fired on: The price field, bubbling to form.cart. Passes: the price field object.
    After each change to the amount, once it’s checked.
  • wc-nyp-valid, wc-nyp-invalid
    Fired on: form.cart. Passes: the form object.
    After each check, depending on whether every price field on the form is valid. Name Your Price also enables or disables the add-to-cart button.
  • wc-nyp-valid-item, wc-nyp-invalid-item
    Fired on: The price field. Passes: the price field object.
    The same, for one price field. Useful when a form has several, as in a bundle.
  • wc-nyp-initializing, wc-nyp-initialized
    Fired on: form.cart. Passes: the form object.
    When the script starts on a form, and when it’s ready.

To make Name Your Price check the field again after your own code changes it, trigger wc-nyp-update on the price field (.nyp).

The price field object

↑ Back to top
MethodReturns
getPrice()The amount as a number, or 0 when the field is empty.
getPeriod()The billing period, for subscriptions where the customer chooses it.
isValid()Whether the amount passes the minimum and maximum.
getErrorMessages()The error messages being shown, as an array of strings.

To get the object without an event: jQuery( '.nyp' ).wc_nyp_get_script_object().

Formatting helpers

↑ Back to top

Use these instead of formatting prices yourself. They follow the store’s currency settings.

FunctionDoes
wc_nyp_format_price( price )Formats a number as a price with the currency symbol, for example $4.50.
wc_nyp_format_number( number )Formats a number with the store’s decimal and thousand separators.
wc_nyp_unformat_number( value )Turns what the customer typed back into a number.

Document Version: 1.0.0
Last Updated: November 9th, 2026
NYP Version Covered: 4.0.0

Use of your personal data
We and our partners process your personal data (such as browsing data, IP Addresses, cookie information, and other unique identifiers) based on your consent and/or our legitimate interest to optimize our website, marketing activities, and your user experience.