Offer property missing in the listing schema

I get this error that offer is missing in schema.

I am using the vehicle schema type. I tried using this code to add the organization and offer but it only adds the organization:

add_filter(
    'hivepress/v1/models/listing/schema',
    function( $schema ) {
        // Get the current listing context
        $listing = hivepress()->request->get_context( 'listing' );

        if ( ! $listing ) {
            return $schema;
        }

        // Add title to the schema
        $schema['title'] = $listing->get_title();

        // Add offers to the schema
        $price = $listing->get_field( 'hp_price' ); // Replace 'hp_price' with the correct key
        if ( $price ) {
            $schema['offers'] = [
                '@type' => 'Offer',
                'price' => $price,
                'priceCurrency' => 'USD', // Replace with your currency code
            ];
        } else {
            error_log( 'Price field is empty or invalid.' );
        }

        // Add seller details dynamically
        $seller_name = $listing->get_vendor__name();
        $seller_url = $listing->get_vendor__url();

        if ( $seller_name ) {
            $schema['seller'] = [
                '@type' => 'Organization',
                'name' => $seller_name,
                'url' => $seller_url ?: '', // Add URL if available, or leave empty
            ];
        } else {
            error_log( 'Seller information is missing or invalid.' );
        }

        // Add datePosted
        $created_date = $listing->get_created_date();
        if ( $created_date ) {
            $schema['datePosted'] = $created_date;
        } else {
            error_log( 'Created date is missing or invalid.' );
        }

        // Add validThrough if the expiration time is available
        $expired_time = $listing->get_expired_time();
        if ( $expired_time ) {
            $schema['validThrough'] = date( 'Y-m-d\TH:i:s', $expired_time );
        }

        return $schema;
    }
);

I have the price under the custom attribute “price” but hivepress does not show that as a schema option.

Hi,

Yes, offers are added only if you have the Marketplace extension. But, if you have a price as a custom attribute, we can provide a PHP snippet to add it as offers in the schema. Let me know if this will work for you. Also, please note that the rating is added as you use the Reviews extension.

I have the price under the custom attribute: price

I do not need the review or aggregate rating fields in the schema

Can you please provide a PHP snippet as soon as possible since this is effecting our SEO.

Sorry for the delay. Please try this code snippet, it should add the offers property to the listing Schema:

add_filter(
	'hivepress/v1/models/listing/schema',
	function( $schema ) {
		$listing = hivepress()->request->get_context( 'listing' );

		if ( ! $listing ) {
			return $schema;
		}

		$schema['offers'] = [
			'@type'         => 'Offer',
			'availability'  => 'https://schema.org/InStock',
			'price'         => $listing->get_price(),
			'priceCurrency' => 'USD',
		];

		return $schema;
	}
);

Replace “price” in “get_price” if the attribute field name is different.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.