Skip to content

Instantly share code, notes, and snippets.

@krisives
Created June 7, 2012 19:52
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 krisives/2891166 to your computer and use it in GitHub Desktop.
Save krisives/2891166 to your computer and use it in GitHub Desktop.
/**
* wpsc_product_variation_price_available function
* Checks for the lowest price of a products variations
*
* @return $price (string) number formatted price
*/
function wpsc_product_variation_price_available( $product_id, $from_text = false, $only_normal_price = false ){
global $wpdb;
$joins = array(
"INNER JOIN {$wpdb->postmeta} AS pm ON pm.post_id = p.id AND pm.meta_key = '_wpsc_price'",
);
$selects = array(
'pm.meta_value AS price',
);
if ( ! $only_normal_price ) {
$joins[] = "INNER JOIN {$wpdb->postmeta} AS pm2 ON pm2.post_id = p.id AND pm2.meta_key = '_wpsc_special_price'";
$selects[] = 'pm2.meta_value AS special_price';
}
$joins = implode( ' ', $joins );
$selects = implode( ', ', $selects );
$sql = $wpdb->prepare( "
SELECT {$selects}
FROM {$wpdb->posts} AS p
{$joins}
WHERE
p.post_type = 'wpsc-product'
AND
p.post_parent = %d
", $product_id );
$results = $wpdb->get_results( $sql );
$prices = array();
$unspecial_prices = array();
$special_prices = array();
foreach ( $results as $row ) {
$price = (float) $row->price;
if ( ! $only_normal_price ) {
$special_price = (float) $row->special_price;
if ( $special_price != 0 && $special_price < $price ) {
$price = $special_price;
$unspecial_prices[] = $row->price;
$special_prices[] = $row->special_price;
}
}
$prices[] = $price;
}
sort( $prices );
sort( $unspecial_prices );
sort( $special_prices );
$price = apply_filters( 'wpsc_do_convert_price', $prices[0] );
$price = wpsc_currency_display( $price, array( 'display_as_html' => false ) );
if ( $prices[0] == $prices[count( $prices ) - 1] )
$from_text = false;
if ( $from_text ) {
$price = sprintf( $from_text, $price );
}
if (count($special_prices) > 1) {
$oldPrice = wpsc_currency_display( reset($unspecial_prices), array( 'display_as_html' => false ) );
$newPrice = wpsc_currency_display( reset($special_prices), array( 'display_as_html' => false ) );
$price = "<s>$oldPrice</s> <strong style='color:#f00'>SALE:</strong> $newPrice";
}
return $price;
}
@krisives
Copy link
Author

krisives commented Jun 7, 2012

Patched function for wordpress-ecommerce wpsc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment