Skip to content

Instantly share code, notes, and snippets.

@cliftonc0613
Last active August 29, 2015 14:20
Show Gist options
  • Save cliftonc0613/dbe42a7bc71500056c07 to your computer and use it in GitHub Desktop.
Save cliftonc0613/dbe42a7bc71500056c07 to your computer and use it in GitHub Desktop.
Poduct on sale
<?php
/*
**
** NOT SURE IS ANY OF THIS IS RIGHT LOL
** Thanks for your help buddy!! :)
*/
add_filter( 'woocommerce_get_price_html', 'buchanan__price_html', 100, 2 );
function buchanan__price_html( $price,$product ){
if ($product= is_on_sale( )) {
// If/Else for simple/variable
if( $product->is_type( 'simple' ) ){
// a simple product
$from = $product->regular_price;
$to = $product->sale_price;
return '<div class="old-colt"><span class="oldcoltest">RETAIL</span> '. ( ( is_numeric( $from ) ) ? woocommerce_price( $from ) : $from ) .'</div><div class="live-colst"><span class="livecoltest">OUR PRICE</span> '.( ( is_numeric( $to ) ) ? woocommerce_price( $to ) : $to ) .'</div>';
} elseif( $product->is_type( 'variable' ) ){
// a variable product
// do the code example below
#Step 1: Get product varations
$available_variations = $product->get_available_variations();
#Step 2: Get product variation id
$variation_id=$available_variations[0]['variation_id']; // Getting the variable id of just the 1st product. You can loop $available_variations to get info about each variation.
#Step 3: Create the variable product object
$variable_product1= new WC_Product_Variation( $variation_id );
#Step 4: You have the data. Have fun :)
$regular_price = $variable_product1 ->regular_price;
$sales_price = $variable_product1 ->sale_price;
return '<div class="old-colt"><span class="oldcoltest">RETAIL</span> '. ( ( is_numeric( $regular_price ) ) ? woocommerce_price( $regular_price ) : $regular_price ) .'</div><div class="live-colst"><span class="livecoltest">OUR PRICE</span> '.( ( is_numeric( $sales_price ) ) ? woocommerce_price( $sales_price ) : $sales_price ) .'</div>';
} else {
return '<div class="live-colst"><span class="livecoltest">PRICE ERROR</span> </div>';
}
} else {
// I DONT KNOW WHAT TO PUT HERE!
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment