Skip to content

Instantly share code, notes, and snippets.

@masiorama
Created January 13, 2018 11:36
Show Gist options
  • Save masiorama/3d6095a84dc56389bbdb1a28f78f0f6e to your computer and use it in GitHub Desktop.
Save masiorama/3d6095a84dc56389bbdb1a28f78f0f6e to your computer and use it in GitHub Desktop.
Wordpress Woocommerce WCDPD_Product_Pricing customize output
function get_discounted_price_custom( $product = null ) {
if ( $product == null ) {
return false;
}
if ( $product->is_type( 'variable' ) ) {
$default_attributes = $product->get_variation_default_attributes();
$variation_id = iconic_find_matching_product_variation( $product, $default_attributes );
$product = wc_get_product( $variation_id );
} else {
$default_attributes = $product->get_default_attributes();
}
$test_price = RP_WCDPD_Product_Pricing::test_product_price( $product, 1, $default_attributes, true );
if ( $test_price !== false && $test_price !== null ) {
$price = $test_price;
$sale_price = $price['price'];
$regular_price = $price['data']['wc_price'];
$sale = round( ( ($regular_price - $sale_price) / $regular_price ) * 100 );
} else {
$sale = null;
}
return $sale;
}
function iconic_find_matching_product_variation( $product, $attributes ) {
foreach ( $attributes as $key => $value ) {
if ( strpos( $key, 'attribute_' ) === 0 ) {
continue;
}
unset( $attributes[$key] );
$attributes[sprintf( 'attribute_%s', $key )] = $value;
}
if ( class_exists( 'WC_Data_Store' ) ) {
$data_store = WC_Data_Store::load( 'product' );
return $data_store->find_matching_product_variation( $product, $attributes );
} else {
return $product->get_matching_variation( $attributes );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment