Advertisement
lorro

WooCommerce - product_category shortcode with author_id

Aug 12th, 2015
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.71 KB | None | 0 0
  1. <?php
  2.   // Does what the standard WooCommerce product_catgeory shortcode does
  3.   // but also takes an author parameter, ie the author id
  4.   // example: [product_category category="kits" author="2"]
  5.   // code goes in functions.php for your child theme
  6.    
  7.   add_shortcode('custom_product_category', 'custom_product_category');
  8.   function custom_product_category( $atts ) {
  9.     global $woocommerce_loop;
  10.  
  11.     $atts = shortcode_atts( array(
  12.       'per_page' => '12',
  13.       'columns'  => '4',
  14.       'orderby'  => 'title',
  15.       'order'    => 'desc',
  16.       'category' => '',  // Slugs
  17.       'operator' => 'IN', // Possible values are 'IN', 'NOT IN', 'AND'.
  18.       'author' => '' // defaults to all authors
  19.     ), $atts );
  20.  
  21.     if ( ! $atts['category'] ) {
  22.       return '';
  23.     }
  24.  
  25.     // Default ordering args
  26.     $ordering_args = WC()->query->get_catalog_ordering_args( $atts['orderby'], $atts['order'] );
  27.     $meta_query    = WC()->query->get_meta_query();
  28.    
  29.     $args = array(
  30.       'post_type'       => 'product',
  31.       'author'          => $atts['author'],
  32.       'post_status'     => 'publish',
  33.       'ignore_sticky_posts'  => 1,
  34.       'orderby'         => $ordering_args['orderby'],
  35.       'order'           => $ordering_args['order'],
  36.       'posts_per_page'  => $atts['per_page'],
  37.       'meta_query'      => $meta_query,
  38.       'tax_query'       => array(
  39.         array(
  40.           'taxonomy'    => 'product_cat',
  41.           'terms'       => array_map( 'sanitize_title', explode( ',', $atts['category'] ) ),
  42.           'field'       => 'slug',
  43.           'operator'    => $atts['operator']
  44.         ),
  45.       )
  46.     );
  47.  
  48.     if ( isset( $ordering_args['meta_key'] ) ) {
  49.       $args['meta_key'] = $ordering_args['meta_key'];
  50.     }
  51.  
  52.     ob_start();
  53.  
  54.     $products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $args, $atts ) );
  55.  
  56.     $columns = absint( $atts['columns'] );
  57.     $woocommerce_loop['columns'] = $columns;
  58.  
  59.     if ( $products->have_posts() ) : ?>
  60.  
  61.       <?php do_action( 'woocommerce_shortcode_before_product_cat_loop' ); ?>
  62.  
  63.       <?php woocommerce_product_loop_start(); ?>
  64.  
  65.         <?php while ( $products->have_posts() ) : $products->the_post(); ?>
  66.  
  67.           <?php wc_get_template_part( 'content', 'product' ); ?>
  68.  
  69.         <?php endwhile; // end of the loop. ?>
  70.  
  71.       <?php woocommerce_product_loop_end(); ?>
  72.  
  73.       <?php do_action( 'woocommerce_shortcode_after_product_cat_loop' ); ?>
  74.  
  75.     <?php endif;
  76.  
  77.     woocommerce_reset_loop();
  78.     wp_reset_postdata();
  79.  
  80.     $return = '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
  81.  
  82.     // Remove ordering query arguments
  83.     WC()->query->remove_ordering_args();
  84.  
  85.     return $return;
  86.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement