Sold Out Message

Display a “Sold Out” message when a choices inventory has been exhausted.

Code

Filename: gpi-sold-out.php

<?php
/**
 * Gravity Perks // Inventory // Sold Out Message
 * https://gravitywiz.com/documentation/gravity-forms-inventory/
 *
 * Display a "Sold Out" message when a choices inventory has been exhausted.
 */
add_filter( 'gpi_remove_choices', '__return_false' );

add_filter( 'gpi_pre_render_choice', function( $choice, $exceeded_limit, $field, $form, $count ) {

	$limit         = (int) rgar( $choice, 'inventory_limit' );
	$how_many_left = max( $limit - $count, 0 );

	if ( $how_many_left <= 0 ) {
		$message         = '(Sold Out)';
		$default_message = gp_inventory_type_choices()->replace_choice_available_inventory_merge_tags( gp_inventory_type_choices()->get_inventory_available_message( $field ), $field, $form, $choice, $how_many_left );
		if ( strpos( $choice['text'], $default_message ) === false ) {
			$choice['text'] .= ' ' . $message;
		} else {
			$choice['text'] = str_replace( $default_message, $message, $choice['text'] );
		}
		return $choice;
	}

	return $choice;
}, 10, 5 );

Leave a Reply

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

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.