How can I add the SKU number and the product category to "get_product_html_list" function?

I am trying to add the product category and the product SKU number to my global product listing through the 'get_product_html_list' in template_functions.php. I have added both to the single product view successfully, but can not get it to work here. The best I have done so far, is that "Uncategorized" shows up as the category for every project rather than the actual category.

Below is the code I'm currently working off of:

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
      $yogi = mp_category_list();
      $html .= '<h5>' . $yogi . '</h5>';

      $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;
}

I'm calling it in mp_productlist.php simply using:

`<?php hd_mp_list_products( $echo = true )?>'

I am by no means a php expert, but will try to stumble my way through any help y'all can give. Thanks and let me know if I need to be more specific.

  • joshmath
    • WPMU DEV Initiate

    Vaughan — here is a sample of what the var_dump is giving me. Don't fully know what I'm doing, but as far as I can tell it's not picking up the sku value in thearray?

    object(WP_Post)#495 (24) { ["ID"]=> int(422) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2012-11-29 22:33:09" ["post_date_gmt"]=> string(19) "2012-11-29 22:33:09" ["post_content"]=> string(216) "Since 1987, Lynn Haney has been creating hand-crafted Santas worthy of becoming heirlooms. Available year-round, Hollyhocks has the largest selection of Lynn Haney Designs in the country. Dimensions: 7" x 18" x 5"" ["post_title"]=> string(17) "Christmas Puppies" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(17) "christmas-puppies" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2012-11-30 17:17:26" ["post_modified_gmt"]=> string(19) "2012-11-30 17:17:26" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(51) "http://localhost:8888/?post_type=product&p=422" ["menu_order"]=> int(0) ["post_type"]=> string(7) "product" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } object(WP_Post)#496 (24) { ["ID"]=> int(427) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2012-11-29 22:34:27" ["post_date_gmt"]=> string(19) "2012-11-29 22:34:27" ["post_content"]=> string(0) "" ["post_title"]=> string(17) "Christmas Sparkle" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(17) "christmas-sparkle" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2012-11-29 22:34:56" ["post_modified_gmt"]=> string(19) "2012-11-29 22:34:56" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(51) "http://localhost:8888/?post_type=product&p=427" ["menu_order"]=> int(0) ["post_type"]=> string(7) "product" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } object(WP_Post)#497 (24) { ["ID"]=> int(431) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2012-11-29 22:35:59" ["post_date_gmt"]=> string(19) "2012-11-29 22:35:59" ["post_content"]=> string(0) "" ["post_title"]=> string(14) "Ocean Memories" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(14) "ocean-memories" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2012-11-29 22:35:59" ["post_modified_gmt"]=> string(19) "2012-11-29 22:35:59" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(51) "http://localhost:8888/?post_type=product&p=431" ["menu_order"]=> int(0) ["post_type"]=> string(7) "product" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" }

  • Vaughan
    • Support/SLS MockingJay

    hi josh

    I think this might do it

    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>';
    
          // add skus
          $html .= mp_product_sku(true, $post->ID);
    
          $html .= '<div class="mp_product_meta">';
    
          //price
          $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;
    }

    i haven't tested, but should work ok.

    remember tho, as you're editing the plugin, if you later update the plugin, you will lose this mod, so would need to re-apply it again.

    however, i'm pretty sure you can call it from inside the theme's template loop. i'll see if i can dig up where, if you have a child theme would be ideal.

  • joshmath
    • WPMU DEV Initiate

    Vaughan, tried your code from above in a couple of different ways (including straight copy and paste), and it doesn't seem to be doing the trick. In fact, it's making any content below the header disappear. Any thoughts? I am also attaching a screen shot of what is showing up with my categories in case you have any thoughts. Thanks for the help man!

  • Vaughan
    • Support/SLS MockingJay

    hi @joshmath

    Sorry for the delay in response.

    the uncategorized means it's not picking up the category data linked to that post ID.

    in my code above, could you try changing

    $html .= mp_product_sku(true, $post->ID);

    to

    $html .= mp_product_sku(false, $post->ID);

    let me know how that goes.

    failing that, if you are in no rush, I'll see if i can properly test this out over the weekend, rather than just working blind on code & getting you to test.

  • joshmath
    • WPMU DEV Initiate

    Appreciate you getting back to me Vaughan. Tried switching out true for false, but that still didn't fix it. None of the store content shows up when using your code. Not in a big rush, just need to get it figured out eventually. I would really appreciate any help you can give, so taking the weekend or longer is no problem. Thanks again!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.