inovve

Woocommerce show variation price in dropdown

Dec 4th, 2019
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.17 KB | None | 0 0
  1. add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
  2.  
  3. function display_price_in_variation_option_name( $term ) {
  4.     global $wpdb, $product;
  5.  
  6.     if ( empty( $term ) ) return $term;
  7.     if ( empty( $product->id ) ) return $term;
  8.  
  9.     $id = $product->get_id();
  10.  
  11.     $result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );
  12.  
  13.     $term_slug = ( !empty( $result ) ) ? $result[0] : $term;
  14.  
  15.     $query = "SELECT postmeta.post_id AS product_id
  16.                FROM {$wpdb->prefix}postmeta AS postmeta
  17.                    LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
  18.                WHERE postmeta.meta_key LIKE 'attribute_%'
  19.                    AND postmeta.meta_value = '$term_slug'
  20.                    AND products.post_parent = $id";
  21.  
  22.     $variation_id = $wpdb->get_col( $query );
  23.  
  24.     $parent = wp_get_post_parent_id( $variation_id[0] );
  25.  
  26.     if ( $parent > 0 ) {
  27.          $_product = new WC_Product_Variation( $variation_id[0] );
  28.          return $term . ' (' . wp_kses( woocommerce_price( $_product->get_price() ), array() ) . ')';
  29.     }
  30.     return $term;
  31.  
  32. }
Advertisement
Add Comment
Please, Sign In to add comment