Advertisement
Guest User

Untitled

a guest
May 21st, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. <?php
  2. /**
  3. * Plugin Name: WooCommerce - List Products by Attributes
  4. * Plugin URI: http://www.remicorson.com/list-woocommerce-products-by-attributes/
  5. * Description: List WooCommerce products by attributes using a shortcode, ex: [woo_products_by_attributes attribute="colour" values="red,black" per_page="5"]
  6. * Version: 1.0
  7. * Author: Remi Corson
  8. * Author URI: http://remicorson.com
  9. * Requires at least: 3.5
  10. * Tested up to: 3.5
  11. *
  12. * Text Domain: -
  13. * Domain Path: -
  14. *
  15. */
  16.  
  17. /*
  18. * List WooCommerce Products by attributes
  19. *
  20. * ex: [woo_products_by_attributes attribute="colour" values="red,black" per_page="5"]
  21. */
  22. function woo_products_by_attributes_shortcode( $atts, $content = null ) {
  23. global $woocommerce, $woocommerce_loop;
  24. // Get attribuets
  25. extract(shortcode_atts(array(
  26. 'attribute' => '',
  27. 'values' => '',
  28. 'per_page' => '12',
  29. 'columns' => '4',
  30. 'orderby' => 'title',
  31. 'order' => 'desc',
  32. ), $atts));
  33.  
  34. if ( ! $attribute ) return;
  35.  
  36. // Default ordering args
  37. $ordering_args = $woocommerce->query->get_catalog_ordering_args( $orderby, $order );
  38.  
  39. // Define Query Arguments
  40. $args = array(
  41. 'post_type' => 'product',
  42. 'post_status' => 'publish',
  43. 'ignore_sticky_posts' => 1,
  44. 'orderby' => $ordering_args['orderby'],
  45. 'order' => $ordering_args['order'],
  46. 'posts_per_page' => $per_page,
  47. 'meta_query' => array(
  48. array(
  49. 'key' => '_visibility',
  50. 'value' => array('catalog', 'visible'),
  51. 'compare' => 'IN'
  52. )
  53. ),
  54. 'tax_query' => array(
  55. array(
  56. 'taxonomy' => 'pa_' . $attribute,
  57. 'terms' => explode(",",$values),
  58. 'field' => 'slug',
  59. 'operator' => 'IN'
  60. )
  61. )
  62. );
  63.  
  64. ob_start();
  65.  
  66. $products = new WP_Query( $args );
  67. $woocommerce_loop['columns'] = $columns;
  68. if ( $products->have_posts() ) : ?>
  69.  
  70. <?php woocommerce_product_loop_start(); ?>
  71.  
  72. <?php while ( $products->have_posts() ) : $products->the_post(); ?>
  73.  
  74. <?php woocommerce_get_template_part( 'content', 'product' ); ?>
  75.  
  76. <?php endwhile; // end of the loop. ?>
  77.  
  78. <?php woocommerce_product_loop_end(); ?>
  79.  
  80. <?php endif;
  81. wp_reset_postdata();
  82. return '<div class="woocommerce">' . ob_get_clean() . '</div>';
  83.  
  84. }
  85.  
  86. add_shortcode("woo_products_by_attributes", "woo_products_by_attributes_shortcode");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement