Skip to content

Commit fa23653

Browse files
committed
An initial pass at adding rich markup to the 2.0 theme engine.
* Adds price, title, image, description, currency metadata. * There is ample opportunity for additional data for themes/plugins. * We added a currency code function to the product helper file. This will likely move if and when #1865 lands. Fixes #59
1 parent 4a99183 commit fa23653

File tree

3 files changed

+69
-10
lines changed

3 files changed

+69
-10
lines changed

wpsc-components/theme-engine-v2/helpers/template-tags/general.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ function wpsc_get_breadcrumb( $args = '' ) {
148148
'before_divider' => '<span class="%s">',
149149
'after_divider' => '</span>',
150150
'divider' => '&raquo;',
151-
'padding' => 1,
151+
'padding' => 0,
152152

153153
// Home
154154
'include_home' => true,
@@ -175,9 +175,8 @@ function wpsc_get_breadcrumb( $args = '' ) {
175175

176176
// if padding is set, prepare the length, padding string and divider
177177
if ( $padding ) {
178-
/* @todo: Check if $padding is supposed to use $length, rather than $padding. Otherwise, $length is dead. */
179-
$length = strlen( $divider ) + $padding * 2;
180-
$padding = str_repeat( "&nbsp;", $padding );
178+
$length = strlen( html_entity_decode( $divider, ENT_COMPAT, 'UTF-8' ) ) + $padding * 2;
179+
$padding = str_repeat( "&nbsp;", $length );
181180
$divider = $padding . $divider . $padding;
182181
}
183182
$divider = $before_divider . $divider . $after_divider;

wpsc-components/theme-engine-v2/helpers/template-tags/product.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,64 @@ function wpsc_get_product_thumbnail_id( $product_id = null ) {
351351
return $thumbnail_id;
352352
}
353353

354+
/**
355+
* Sets the structured product name to the product title.
356+
*
357+
* @param array $crumbs Array of breadcrumb elements.
358+
*
359+
* @since 4.0
360+
*
361+
* @return array Array of breadcrumb elements with product title structured semantically.
362+
*/
363+
function wpsc_set_structured_product_name( $crumbs ) {
364+
365+
if ( wpsc_is_single() ) {
366+
$product = str_replace( 'wpsc-breadcrumb-item"', 'wpsc-breadcrumb-item" itemprop="name"', array_pop( $crumbs ) );
367+
$crumbs[] = $product;
368+
}
369+
370+
return $crumbs;
371+
}
372+
373+
add_filter( 'wpsc_breadcrumb_array', 'wpsc_set_structured_product_name' );
374+
375+
/**
376+
* Template tag for base country currency code.
377+
*
378+
* Helpful for templates using structured data, likely other use cases.
379+
* Temporarily located here, until #1865 lands.
380+
*
381+
* @since 4.0
382+
*
383+
* @return string Base country currency code.
384+
*/
385+
function wpsc_base_country_code() {
386+
387+
$base = new WPSC_Country( wpsc_get_base_country() );
388+
389+
echo esc_attr( $base->get_currency_code() );
390+
}
391+
392+
/**
393+
* Sets structured data for product thumbnails.
394+
*
395+
* @param string $html Product thumbnail HTML.
396+
*
397+
* @since 4.0
398+
*
399+
* @return string $html Product thumbnail HTML with strucutred data.
400+
*/
401+
function wpsc_set_structured_image_data( $html ) {
402+
403+
if ( ! wpsc_is_single() ) {
404+
return $html;
405+
}
406+
407+
return str_replace( '<img', '<img itemprop="image"', $html );
408+
}
409+
410+
add_filter( 'post_thumbnail_html', 'wpsc_set_structured_image_data' );
411+
354412
/**
355413
* Return the HTML of a product's featured thumbnail.
356414
*

wpsc-components/theme-engine-v2/theming/template-parts/single.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
<?php wpsc_user_messages(); ?>
2-
<div id="product-<?php wpsc_product_id(); ?>">
2+
<div id="product-<?php wpsc_product_id(); ?>" itemscope itemtype="http://schema.org/Product">
33
<?php wpsc_breadcrumb(); ?>
44

55
<div class="wpsc-product-summary">
6-
<div class="wpsc-product-price">
6+
7+
<div class="wpsc-product-price" itemprop="offers" itemscope itemtype="http://schema.org/Offer">
8+
<meta itemprop="priceCurrency" content="<?php wpsc_base_country_code(); ?>" />
79
<?php if ( wpsc_is_product_on_sale() ): ?>
810
<del class="wpsc-old-price">
911
<strong><?php esc_html_e( 'Old Price', 'wpsc' ); ?>:</strong> <span class="wpsc-amount"><?php wpsc_product_original_price(); ?></span>
1012
</del><br />
1113
<ins class="wpsc-sale-price">
12-
<strong><?php esc_html_e( 'Price', 'wpsc' ); ?>:</strong> <span class="wpsc-amount"><?php wpsc_product_sale_price(); ?></span>
14+
<strong><?php esc_html_e( 'Price', 'wpsc' ); ?>:</strong> <span class="wpsc-amount" itemprop="price"><?php wpsc_product_sale_price(); ?></span>
1315
</ins><br />
1416
<span class="wpsc-you-save">
1517
<strong><?php esc_html_e( 'You save', 'wpsc' ); ?>:</strong> <span class="wpsc-amount"><?php wpsc_product_you_save(); ?></span>
1618
</span>
1719
<?php else: ?>
18-
<strong><?php esc_html_e( 'Price', 'wpsc' ); ?>:</strong> <span class="wpsc-amount"><?php wpsc_product_original_price(); ?></span>
20+
<strong><?php esc_html_e( 'Price', 'wpsc' ); ?>:</strong> <span class="wpsc-amount" itemprop="price"><?php wpsc_product_original_price(); ?></span>
1921
<?php endif; ?>
2022
</div>
2123

22-
<div class="wpsc-product-description">
24+
<div class="wpsc-product-description" itemprop="description">
2325
<?php wpsc_product_description(); ?>
2426
</div>
2527

@@ -45,4 +47,4 @@ class="wpsc-thumbnail wpsc-product-thumbnail"
4547
<?php endif; ?>
4648
</a>
4749
</div>
48-
</div><!-- #post-<?php the_ID(); ?> -->
50+
</div><!-- #product-<?php the_ID(); ?> -->

0 commit comments

Comments
 (0)