Advertisement
murmoon

Show Attribute Name + Show Attribute Price

Mar 24th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 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.  
  5.  
  6. global $wpdb, $product;
  7.  
  8. if ( empty( $term ) ) return $term;
  9.  
  10.  
  11. if ( empty( $product->id ) ) return $term;
  12.  
  13. $result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );
  14.  
  15. $term_slug = ( !empty( $result ) ) ? $result[0] : $term;
  16.  
  17. $query = "SELECT postmeta.post_id AS product_id
  18.  
  19.  
  20. FROM {$wpdb->prefix}postmeta AS postmeta
  21.  
  22.  
  23. LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
  24.  
  25.  
  26. WHERE postmeta.meta_key LIKE 'attribute_%'
  27.  
  28.  
  29. AND postmeta.meta_value = '$term_slug'
  30.  
  31.  
  32. AND products.post_parent = $product->id";
  33.  
  34. $variation_id = $wpdb->get_col( $query );
  35.  
  36. $parent = wp_get_post_parent_id( $variation_id[0] );
  37.  
  38. if ( $parent > 0 ) {
  39.  
  40.  
  41. $_product = new WC_Product_Variation( $variation_id[0] );
  42.  
  43.  
  44. return $term . ' (' . wp_kses( woocommerce_price( $_product->get_price() ), array() ) . ')';
  45.  
  46. }
  47.  
  48.  
  49. return $term;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement