Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Does what the standard WooCommerce product_catgeory shortcode does
- // but also takes an author parameter, ie the author id
- // example: [product_category category="kits" author="2"]
- // code goes in functions.php for your child theme
- add_shortcode('custom_product_category', 'custom_product_category');
- function custom_product_category( $atts ) {
- global $woocommerce_loop;
- $atts = shortcode_atts( array(
- 'per_page' => '12',
- 'columns' => '4',
- 'orderby' => 'title',
- 'order' => 'desc',
- 'category' => '', // Slugs
- 'operator' => 'IN', // Possible values are 'IN', 'NOT IN', 'AND'.
- 'author' => '' // defaults to all authors
- ), $atts );
- if ( ! $atts['category'] ) {
- return '';
- }
- // Default ordering args
- $ordering_args = WC()->query->get_catalog_ordering_args( $atts['orderby'], $atts['order'] );
- $meta_query = WC()->query->get_meta_query();
- $args = array(
- 'post_type' => 'product',
- 'author' => $atts['author'],
- 'post_status' => 'publish',
- 'ignore_sticky_posts' => 1,
- 'orderby' => $ordering_args['orderby'],
- 'order' => $ordering_args['order'],
- 'posts_per_page' => $atts['per_page'],
- 'meta_query' => $meta_query,
- 'tax_query' => array(
- array(
- 'taxonomy' => 'product_cat',
- 'terms' => array_map( 'sanitize_title', explode( ',', $atts['category'] ) ),
- 'field' => 'slug',
- 'operator' => $atts['operator']
- ),
- )
- );
- if ( isset( $ordering_args['meta_key'] ) ) {
- $args['meta_key'] = $ordering_args['meta_key'];
- }
- ob_start();
- $products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $args, $atts ) );
- $columns = absint( $atts['columns'] );
- $woocommerce_loop['columns'] = $columns;
- if ( $products->have_posts() ) : ?>
- <?php do_action( 'woocommerce_shortcode_before_product_cat_loop' ); ?>
- <?php woocommerce_product_loop_start(); ?>
- <?php while ( $products->have_posts() ) : $products->the_post(); ?>
- <?php wc_get_template_part( 'content', 'product' ); ?>
- <?php endwhile; // end of the loop. ?>
- <?php woocommerce_product_loop_end(); ?>
- <?php do_action( 'woocommerce_shortcode_after_product_cat_loop' ); ?>
- <?php endif;
- woocommerce_reset_loop();
- wp_reset_postdata();
- $return = '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
- // Remove ordering query arguments
- WC()->query->remove_ordering_args();
- return $return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement