0
votes

First time posting here so please bear with me. I am using the following code:

function display_price_in_variation_option_name( $term ) {
    global $wpdb, $product;
    $result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );
    $term_slug = ( !empty( $result ) ) ? $result[0] : $term;
    $query = "SELECT postmeta.post_id AS product_id
        FROM {$wpdb->prefix}postmeta AS postmeta
        LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
        WHERE postmeta.meta_key LIKE 'attribute_%'
        AND postmeta.meta_value = '$term_slug'
        AND products.post_parent = $product->id";

    $variation_id = $wpdb->get_col( $query );
    $parent = wp_get_post_parent_id( $variation_id[0] );

    if ( $parent > 0 ) {
        $_product = new WC_Product_Variation( $variation_id[0] );
        return $term . woocommerce_price( $_product->get_price() );
    }

    return $term;
}
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );

This code puts a price next to each item in a variations menu. What I'd like to do is to have it also post a sale price and have the regular price be struck through (as in, here's the regular price and the sale price).

I tried the following:

            $regular_price = woocommerce_price( $_product->get_regular_price() );
            $sale_price = woocommerce_price( $_product->get_price() );
            return $term . $regular_price . $sale_price;

And while that does add in a sale price it also has the sale price and regular prices overlap. I guess what I am looking for is a way to format the prices.

Any help is appreciated.

Thanks

1

1 Answers

0
votes

You can add a bit of HTML within the Filter to add spaces and strike-through the original price. You can use following code

            $regular_price = woocommerce_price( $_product->get_regular_price() );
            $sale_price = woocommerce_price( $_product->get_price() );
            return $term . '&nbsp' . '<strike>' . $regular_price . '</strike>'  . '&nbsp'. $sale_price;