Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@bappi-d-great
Created May 8, 2018 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bappi-d-great/85c2a43a71bd5d0ca113c561fc4bd62b to your computer and use it in GitHub Desktop.
Save bappi-d-great/85c2a43a71bd5d0ca113c561fc4bd62b to your computer and use it in GitHub Desktop.
WPMU DEV MarketPress: Support schema.org for product price
<?php
add_filter( 'mp_format_currency_symbol', function() {
return 'USD ';
} );
add_filter( 'mp_product/display_price', function( $snippet, $price, $id ) {
$product = new MP_Product( $id );
$snippet = '<!-- MP Product Price --><div class="mp_product_price" itemtype="http://schema.org/Offer" itemscope="" itemprop="offers">';
if ( $product->has_variations() ) {
if ( $product->on_sale() ) {
// Get price range
if ( $price['lowest'] != $price['highest'] ) {
$snippet .= '<span style="display: none"><span itemprop="priceCurrency" content="' . mp_format_currency() . '">' . mp_format_currency() . '</span><span itemprop="price" content="' . $price['lowest'] . '">' . $price['lowest'] . '</span></span>';
$snippet .= '<span class="mp_product_price-sale">' . mp_format_currency( '', $product->manage_price_tax( $price['lowest'] ) ) . ' - ' . mp_format_currency( '', $product->manage_price_tax( $price['highest'] ) ) . $product->display_tax_string( false ) . '</span>';
} else {
$snippet .= '<span style="display: none"><span itemprop="priceCurrency" content="' . mp_format_currency() . '">' . mp_format_currency() . '</span><span itemprop="price" content="' . $price['lowest'] . '">' . $price['lowest'] . '</span></span>';
$snippet .= '<span class="mp_product_price-sale">' . mp_format_currency( '', $product->manage_price_tax( $price['lowest'] ) ) . $product->display_tax_string( false ) . '</span>';
}
// Get sale price range
if ( $price['lowest_regular'] != $price['highest_regular'] ) {
$snippet .= '<span style="display: none"><span itemprop="priceCurrency" content="' . mp_format_currency() . '">' . mp_format_currency() . '</span><span itemprop="price" content="' . $price['lowest_regular'] . '">' . $price['lowest_regular'] . '</span></span>';
$snippet .= '<span class="mp_product_price-normal mp_strikeout">' . mp_format_currency( '', $product->manage_price_tax( $price['lowest_regular'] ) ) . ' - ' . mp_format_currency( '', $product->manage_price_tax( $price['highest_regular'] ) ) . $product->display_tax_string( false ) . '</span>';
} else {
if ( $price['highest_regular'] > $price['regular'] ) {
$snippet .= '<span class="mp_product_price-normal mp_strikeout">' . mp_format_currency( '', $product->manage_price_tax( ( $price['regular'] * $product->qty ) ) ) . $product->display_tax_string( false ) . '</span>';
} else {
$snippet .= '<span class="mp_product_price-normal mp_strikeout">' . mp_format_currency( '', $product->manage_price_tax( ( $price['highest_regular'] * $product->qty ) ) ) . $product->display_tax_string( false ) . '</span>';
}
}
}
else{
// Get price range
if ( $price['lowest'] != $price['highest'] ) {
$snippet .= '<span style="display: none"><span itemprop="priceCurrency" content="' . mp_format_currency() . '">' . mp_format_currency() . '</span><span itemprop="price" content="' . $price['lowest'] . '">' . $price['lowest'] . '</span></span>';
$snippet .= '<span class="mp_product_price-normal">' . mp_format_currency( '', $product->manage_price_tax( $price['lowest'] ) ) . ' - ' . mp_format_currency( '', $product->manage_price_tax( $price['highest'] ) ) . $product->display_tax_string( false ) . '</span>';
} else {
$snippet .= '<span style="display: none"><span itemprop="priceCurrency" content="' . mp_format_currency() . '">' . mp_format_currency() . '</span><span itemprop="price" content="' . $price['lowest'] . '">' . $price['lowest'] . '</span></span>';
$snippet .= '<span class="mp_product_price-normal">' . mp_format_currency( '', $product->manage_price_tax( $price['lowest'] ) ) . $product->display_tax_string( false ) . '</span>';
}
}
} elseif ( $product->on_sale() ) {
$amt_off = mp_format_currency( '', ( $product->manage_price_tax( $price['highest'] ) - $product->manage_price_tax( $price['lowest'] ) ) * $product->qty ) . $product->display_tax_string( false );
if ( $product->qty > 1 ) {
$snippet .= '<span style="display: none"><span itemprop="priceCurrency" content="' . mp_format_currency() . '">' . mp_format_currency() . '</span><span itemprop="price" content="' . $price['sale']['amount'] . '">' . $price['sale']['amount'] . '</span></span>';
$snippet .= '<span class="mp_product_price-extended">' . mp_format_currency( '', $product->manage_price_tax( ( $price['lowest'] * $product->qty ) ) ) . $product->display_tax_string( false ) . '</span>';
$snippet .= '<span class="mp_product_price-each" itemprop="price">(' . sprintf( __( '%s each', 'mp' ), mp_format_currency( '', $product->manage_price_tax( $price['sale']['amount'] ) ) ) . ') ' . $product->display_tax_string( false ) . '</span>';
} else {
$snippet .= '<span style="display: none"><span itemprop="priceCurrency" content="' . mp_format_currency() . '">' . mp_format_currency() . '</span><span itemprop="price" content="' . $price['sale']['amount'] . '">' . $price['sale']['amount'] . '</span></span>';
$snippet .= '<span class="mp_product_price-sale">' . mp_format_currency( '', $product->manage_price_tax( $price['sale']['amount'] ) ) . '</span>';
}
$snippet .= '<span class="mp_product_price-normal mp_strikeout">' . mp_format_currency( '', $product->manage_price_tax( ( $price['regular'] * $product->qty ) ) ) . $product->display_tax_string( false ) . '</span>';
} else {
if ( $product->qty > 1 ) {
$snippet .= '<span style="display: none"><span itemprop="priceCurrency" content="' . mp_format_currency() . '">' . mp_format_currency() . '</span><span itemprop="price" content="' . $price['lowest'] . '">' . $price['lowest'] . '</span></span>';
$snippet .= '<span class="mp_product_price-extended">' . mp_format_currency( '', $product->manage_price_tax( ( $price['lowest'] * $product->qty ) ) ) . $product->display_tax_string( false ) . '</span>';
$snippet .= '<span class="mp_product_price-each">(' . sprintf( __( '%s each', 'mp' ), mp_format_currency( '', $product->manage_price_tax( $price['lowest'] ) ) ) . ') ' . $product->display_tax_string( false ) . '</span>';
} else {
$snippet .= '<span style="display: none"><span itemprop="priceCurrency" content="' . mp_format_currency() . '">' . mp_format_currency() . '</span><span itemprop="price" content="' . $price['lowest'] . '">' . $price['lowest'] . '</span></span>';
$snippet .= '<span class="mp_product_price-normal">' . mp_format_currency( '', $product->manage_price_tax( $price['lowest'] ) ). $product->display_tax_string( false ) . '</span>';
}
}
$snippet .= '</div><!-- end mp_product_price -->';
return $snippet;
}, 99, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment