How do I add a product's sku number to my product list?

I have a client who wants the SKU number to show up below the product title on his global product listing pages. I am able to get the sku to show up on individual product pages, but not on category or "all product" pages. I am using mp_list_products which hooks into mp_product_list. I've tried about everything I can think of, so any help would be much appreciated. Thanks!

  • MasatoEM

    It may need to be a standard copy and paste into the title field as far as I can tell.
    You can also do a standard product add with the short code provided in the listings page, creating a more customizable storefront and "window shopping" area, is the best way I can put it.

    Basically what you are doing is creating a new layout by using the shortcodes to display products, and you would be doing standard HTML editing to make it display in a table like manner, giving you a more flexible storefront, more customizable, and in the long run, a better looking page if done right.

    That is my suggestion to you.

    Hope this helps!

  • aecnu

    Greetings joshmath,

    Thank you for the question and indeed MasatoEM offered a good answer and possibly placing it right below the title by using the following in the title itself:

    Title of product <br>SKU Number</br>

    But this would indeed need to be tested to see if it displays right with the theme you are using.

    Thank you for being a WPMU DEV Community Member and have a GREAT upcoming weekend!

    Cheers, Joe

  • joshmath

    @masato, thanks for getting back to me. I'd prefer not to go that route, because I am using the title field as is on the individual product pages. Might do that if I have to though. Preferably though, I would like to just modify the function "get_products_html_list" found in template-functions.php to also include the SKU number. Below is the code I am using with the product sku trying to be insterted as an <h4>, which isn't currently working. The working site url is here.

    function get_products_html_list($post_array=array()){
      global $mp;
      $html='';
      $total = count($post_array);
      $count = 0;
      foreach($post_array as $post){
    
          $count++;
    
          //add last css class for styling grids
          if ($count == $total)
              $class = array('mp_product', 'last-product');
          else
              $class = 'mp_product';
    
          $html .= '<div '.mp_product_class(false, $class, $post->ID).'>';
          // $html .= '<h3 class="mp_product_name"><a href="' . get_permalink( $post->ID ) . '">' . $post->post_title . '</a></h3>';
          // $html .= '<div class="mp_product_content">';
          $product_content = mp_product_image( false, 'list', $post->ID );
          // if ($mp->get_setting('show_excerpt'))
          //     $product_content .= $mp->product_excerpt($post->post_excerpt, $post->post_content, $post->ID);
          $html .= apply_filters( 'mp_product_list_content', $product_content, $post->ID );
          // $html .= '</div>';
          $html .= '<h3 class="mp_product_name"><a href="' . get_permalink( $post->ID ) . '">' . $post->post_title . '</a></h3>';
          // $html .= '<div class="mp_product_meta">';
          //price
          $html .= '<h4>' .mp_product_sku(true, $post->ID). '</h4>';
    
          $meta = mp_product_price(false, $post->ID);
          //button
          // $meta .= mp_buy_button(false, 'list', $post->ID);
          $html .= apply_filters( 'mp_product_list_meta', $meta, $post->ID );
          // $html .= '</div>';
    
          $html .= '</div>';
      }
    
      return $html;
    }
  • joshmath

    Aaron, thanks for getting back to me. I appreciate the help because I've tried just about everything I can think of. However, it doesn't seem to matter if I echo or return, the result is the same, and that is that all content within the function get_products_html_list() disappears, leaving me with just the header showing up. See screenshots. Any other thoughts?

    I also tried adding the following:

    $sku = get_post_meta($post->ID, &quot;mp_sku&quot;, false);
     $html .= '<h4>' . $sku . '</h4>';

    This at least returns something without breaking anything else, but currently it's showing "array." (See screen 3) Is there a way I can get it to give the value? I don't know if this will be an easier approach or not.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.