S
S
Suleimanov_Ismar2020-06-27 17:51:56
WordPress
Suleimanov_Ismar, 2020-06-27 17:51:56

Woocommerce - How to display the price of a variable product?

Hello everyone,
Please tell me how to make it so that each choice of a product variation displays its price?
5ef75d04254d2894545472.png

What hook is responsible for this? Maybe you've come across this before?
Desired Result: "Variation Name: Price"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Suleimanov_Ismar, 2020-06-28
@Suleimanov_Ismar

Solution in case anyone needs it:

add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
function display_price_in_variation_option_name( $term ) {
  global $wpdb, $product;

  if ( empty( $term ) ) return $term;
  if ( empty( $product->id ) ) return $term;

  $id = $product->get_id();

  $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 = $id";

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

  // print_r($term);
  if ( $parent > 0 ) {
    $_product = new WC_Product_Variation( $variation_id[0] );
    return "<div class='comp_name'>".$term.'</div>
        <div class="com_price">(' . wp_kses( woocommerce_price( $_product->get_price() ), array() ) . ')</div>';
  }
  return $term;

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question