Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save UraraReika/d0cc860469df4d21b10b90de6b803395 to your computer and use it in GitHub Desktop.
Save UraraReika/d0cc860469df4d21b10b90de6b803395 to your computer and use it in GitHub Desktop.
Display needed price from variable product.
<?php
add_filter( 'jet-woo-builder/template-functions/product-price', 'get_needed_price' );
function get_needed_price( $html ) {
global $product;
if ( ! is_a( $product, 'WC_Product' ) ) {
return null;
}
if ( 'variable' === $product->get_type() ) {
$prices = sort_variation_price( $product );
return '<span class="woocommerce-Price-amount amount"> <bdi>' . $prices['regular_min'] . '&nbsp</bdi><span class="woocommerce-Price-currencySymbol">' . get_woocommerce_currency_symbol() . '</span> </span>';
}
return $html;
}
function sort_variation_price( $product ) {
$prices = $product->get_variation_prices( true );
foreach ( $prices['price'] as $product_id => $price ) {
$product_obj = wc_get_product( $product_id );
$prices['price'][ $product_id ] = wc_get_price_to_display( $product_obj );
}
asort( $prices['price'] );
asort( $prices['regular_price'] );
return [
'sale_min' => current( $prices['price'] ),
'sale_max' => end( $prices['price'] ),
'regular_min' => current( $prices['regular_price'] ),
'regular_max' => end( $prices['regular_price'] ),
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment