invp_get_the_price( string $zero_string = null, int|null $post_ID = null )

invp_get_the_price


Description Description

Template tag. Returns the vehicle's price.

Returns the price as a dollar amount with a dollar sign except when it is zero or the vehicle is sold. Returns the $zero_string when the price is zero. Returns "SOLD!" when the vehicle is sold.


Top ↑

Parameters Parameters

$zero_string

(string) (Optional) The text to display when the price is zero

Default value: null

$post_ID

(int|null) (Optional) The post ID of a vehicle. Must be passed when using this method outside the loop.

Default value: null


Top ↑

Return Return

(string)


Top ↑

Source Source

File: includes/template-tags.php

function invp_get_the_price( $zero_string = null, $post_ID = null )
{
	if( empty( $post_ID ) )
	{
		$post_ID = get_the_ID();
	}

	//If this vehicle is sold, just say so
	if ( invp_is_sold( $post_ID ) )
	{
		return apply_filters( 'invp_sold_string', sprintf( '<span class="vehicle-sold">%s</span>', __( 'SOLD!', 'inventory-presser' ) ) );
	}

	if( null === $zero_string )
	{
		$zero_string = __( 'Call For Price', 'inventory-presser' );
	}
	$zero_string = apply_filters( 'invp_zero_price_string', $zero_string, $post_ID );
	
	//How are we displaying the price?
	$settings = INVP::settings();
	if( ! isset( $settings['price_display'] ) )
	{
		$settings['price_display'] = 'default';
	}

	switch( $settings['price_display'] )
	{
		case 'msrp':
			return invp_get_the_msrp( $post_ID );

		//${Price} / ${Down Payment} Down
		case 'full_or_down':
			$output = '';
			$price = invp_get_raw_price( $post_ID );
			if( ! empty( $price ) )
			{
				$output .= sprintf( '$%s', number_format( $price, 0, '.', ',' ) );
			}
			
			$down_payment = invp_get_the_down_payment();
			if( ! empty( $down_payment ) )
			{
				$output .= sprintf( ' / %s Down', $down_payment );
			}

			if( '' == $output )
			{
				return $zero_string;
			}
			return $output;

		// down payment only
		case 'down_only':
			$down_payment = invp_get_the_down_payment();
			if( ! empty( $down_payment ) )
			{
				return sprintf( '%s Down', $down_payment );
			}
			break;

		// call_for_price
		case 'call_for_price':
			//Not $zero_string, but explicity "Call for Price"
			return __( 'Call For Price', 'inventory-presser' );
			break;

		// was_now_discount - MSRP = was price, regular price = now price, discount = was - now.
		case 'was_now_discount':
			$msrp = INVP::get_meta( 'msrp', $post_ID );
			$price = invp_get_raw_price( $post_ID );
			if( ! empty( $msrp )
				&& ! empty( $price )
				&& $msrp > $price
			)
			{
				return sprintf(
					'<div class="price-was-discount">%s %s</div>%s $%s<div class="price-was-discount-save">%s $%s</div>',
					__( 'Retail', 'inventory-presser' ),
					invp_get_the_msrp( $post_ID ),
					__( 'Now', 'inventory-presser' ),
					number_format( $price, 0, '.', ',' ),
					__( 'You Save', 'inventory-presser' ),
					number_format( ( $msrp - $price ), 0, '.', ',' )
				);
			}
			break;

		//$75 per week
		case 'payment_only':
			$payment = invp_get_the_payment( $post_ID );
			$payment_frequency = invp_get_the_payment_frequency( $post_ID );
			if( empty( $payment ) || empty( $payment_frequency ) )
			{
				return $zero_string;
			}

			switch( $payment_frequency )
			{
				case 'weekly':
					$payment_frequency = __( 'per week', 'inventory-presser' );
					break;

				case 'monthly':
					$payment_frequency = __( 'per month', 'inventory-presser' );
					break;

				case 'biweekly':
					$payment_frequency = __( 'every other week', 'inventory-presser' );
					break;

				case 'semimonthly':
					$payment_frequency = __( 'twice a month', 'inventory-presser' );
					break;
			}
			return sprintf(
				'%s %s',
				$payment,
				$payment_frequency
			);
			break;

		case 'default':
			//Normally, show the price field as currency.
			$price = invp_get_raw_price( $post_ID );
			if( empty( $price ) )
			{
				return $zero_string;
			}
			return '$' . number_format( $price, 0, '.', ',' );
			break;

		case 'down_and_payment':
			$down_payment = invp_get_the_down_payment();
			$payment = invp_get_the_payment( $post_ID );
			if ( ! empty( $down_payment ) && ! empty( $payment ) )
			{
				return sprintf(
					'%s %s / %s %s',
					$down_payment,
					__( 'Down', 'inventory-presser' ),
					$payment,
					ucfirst( invp_get_the_payment_frequency() )
				);
			}

		default:
			/**
			 * The price display type is something beyond what this
			 * plugin supports. Allow the value to be filtered.
			 */
			return apply_filters( 'invp_price_display', $zero_string, $settings['price_display'], $post_ID );
			break;
	}

	return $zero_string;
}