Skip to content

Instantly share code, notes, and snippets.

@GauravKhupse
Created September 16, 2021 09:00
Show Gist options
  • Save GauravKhupse/78c94502f25c3a34f74da32737cf44d7 to your computer and use it in GitHub Desktop.
Save GauravKhupse/78c94502f25c3a34f74da32737cf44d7 to your computer and use it in GitHub Desktop.
We can add price range in product schema. low and high price
add_action( 'after_setup_theme', 'add_my_custom_meta_field' );
function add_my_custom_meta_field() {
add_filter( 'wp_schema_pro_schema_meta_fields', 'my_extra_schema_field' );
add_filter( 'wp_schema_pro_schema_product', 'my_extra_schema_field_mapping', 10, 3 );
}
/**
* Add fields for mapping.
*
* @param array $fields Mapping fields array.
* @return array
*/
function my_extra_schema_field( $fields ) {
$fields['bsf-aiosrs-product']['subkeys']['highPrice'] = array( // `bsf-aiosrs-book` used for Book, `bsf-aiosrs-event` will for Event like that.
'label' => esc_html__( 'High Price', 'aiosrs-pro' ), // Label to display in Mapping fields
'type' => 'number', // text/date/image
'default' => 'none',
'required' => true, // true/false.
);
$fields['bsf-aiosrs-product']['subkeys']['lowPrice'] = array( // `bsf-aiosrs-book` used for Book, `bsf-aiosrs-event` will for Event like that.
'label' => esc_html__( 'Low Price', 'aiosrs-pro' ), // Label to display in Mapping fields
'type' => 'number', // text/date/image
'default' => 'none',
'required' => true, // true/false.
);
$fields['bsf-aiosrs-product']['subkeys']['offerCount'] = array( // `bsf-aiosrs-book` used for Book, `bsf-aiosrs-event` will for Event like that.
'label' => esc_html__( 'Offer count', 'aiosrs-pro' ), // Label to display in Mapping fields
'type' => 'number', // text/date/image
'default' => 'none',
'required' => true, // true/false.
);
return $fields;
}
/**
* Mapping extra field for schema markup.
*
* @param array $schema Schema array.
* @param array $data Mapping fields array.
* @return array
*/
function my_extra_schema_field_mapping( $schema, $data, $post ) {
$schema['offers']['@type'] = 'AggregateOffer';
$schema['offers']['price'] = '';
if ( isset( $data['highPrice'] ) && ! empty( $data['highPrice'] ) ) {
// For date/text type field
$schema['offers']['highPrice'] = esc_html( $data['highPrice'] );
// For image type field
// $schema['workExample'] = BSF_AIOSRS_Pro_Schema_Template::get_image_schema( $data['work-example'] );
}
if ( isset( $data['lowPrice'] ) && ! empty( $data['lowPrice'] ) ) {
// For date/text type field
$schema['offers']['lowPrice'] = esc_html( $data['lowPrice'] );
// For image type field
// $schema['workExample'] = BSF_AIOSRS_Pro_Schema_Template::get_image_schema( $data['work-example'] );
}
if ( isset( $data['offerCount'] ) && ! empty( $data['offerCount'] ) ) {
// For date/text type field
$schema['offers']['offerCount'] = esc_html( $data['offerCount'] );
// For image type field
// $schema['workExample'] = BSF_AIOSRS_Pro_Schema_Template::get_image_schema( $data['work-example'] );
}
return $schema;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment