Advertisement
lorro

WooCommerce - Products list shortcode

Jan 1st, 2017
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.78 KB | None | 0 0
  1. <?php
  2.   // Shortcode to make a list of products from a category
  3.   // usage: [products_list rows="-1" category="kits" orderby="title" order="ASC"]
  4.   // rows defaults to -1, show all products
  5.   // category defaults to '', empty: shows all categories, value: category slug
  6.   // orderby defaults to 'title'
  7.   // order defaults to 'DESC'
  8.   // for orderby options see: https://codex.wordpress.org/Template_Tags/get_posts
  9.   // code goes in functions.php for your child theme
  10.  
  11.   add_shortcode('products_list', 'products_list');
  12.   function products_list( $atts ) {
  13.     global $post;
  14.     // get the parameters
  15.     $args = shortcode_atts( array(
  16.       'rows'            => -1,
  17.       'category'        => '',
  18.       'tag'             => '',
  19.       'orderby'         => 'title',
  20.       'order'           => 'ASC'
  21.       ), $atts );
  22.     $rows = (int) $args['rows'];
  23.     $category = $args['category'];
  24.     $tag = $args['tag'];
  25.     $orderby = $args['orderby'];
  26.     $order = $args['order'];
  27.     // get the products
  28.     $args = array(
  29.       'posts_per_page'  => $rows,
  30.       'offset'          => 0,
  31.       'post_status'     => 'publish',
  32.       'post_type'       => 'product',
  33.       'product_cat'     => $category,
  34.       'product_tag'     => $tag,
  35.       'orderby'         => $orderby,
  36.       'order'           => $order
  37.     );
  38.     $posts_list = get_posts( $args );
  39.     if ( count( $posts_list ) ) {
  40.       $html = '<table class="products-list">'.PHP_EOL;
  41.       $html .= '<tbody>';
  42.       foreach ( $posts_list as $post ) {
  43.         setup_postdata( $post );
  44.         $html .= '<tr>'.PHP_EOL;
  45.         $img_html = woocommerce_get_product_thumbnail( 'shop_thumbnail' );
  46.         $html.= '<td>'.$img_html.'</td>'.PHP_EOL;
  47.         $my_title = get_the_title();
  48.         $html .= '<td>'.$my_title.'</td>'.PHP_EOL;
  49.         $_product = wc_get_product( $post->ID );
  50.         $price = $_product->get_price_html();
  51.         $html .= '<td>'.$price.'</td>'.PHP_EOL;
  52.         $my_permalink = get_the_permalink();
  53.         $html .= '<td><a href="'.$my_permalink.'" target="_blank" class="button">Details</a></td>'.PHP_EOL;
  54.         $html .= '</tr>'.PHP_EOL;
  55.         wp_reset_postdata();
  56.       }
  57.       $html .= '</tbody>';
  58.       $html .= '</table>'.PHP_EOL;
  59.     } else {
  60.       $html = 'No products found'.PHP_EOL;
  61.     }
  62.     return $html;
  63.   }
  64. ?>
  65.  
  66. /* custom styles for the product_list */
  67. /* may need modifying to suit your theme */
  68. /* Custom css can be entered at: Dashboard > Appearance > Customise > Additional CSS */
  69. /* do not put in functions.php */
  70.  
  71. .entry-content .products-list tr {
  72.   border-style:solid;
  73.   border-color:#ddd;
  74.   border-width:1px 0
  75. }
  76. .entry-content .products-list td {
  77.   padding:0 16px;
  78.   border:none
  79. }
  80. .entry-content .products-list td img {
  81.   width:40px;
  82.   height:40px;
  83. }
  84. .products-list .button {
  85.   padding:4px 16px;
  86.   border:1px solid #bbb;
  87.   border-radius:6px;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement