Last active
December 11, 2022 07:22
-
-
Save digitalchild/3ae1539f5ee8fb69711f to your computer and use it in GitHub Desktop.
Product Vendor Recent Products
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
The following code will create a pv_recent_products short code that is a duplicate of the internal woocommerce | |
shortcode for recent products (See : http://docs.woothemes.com/document/woocommerce-shortcodes/) but provides a | |
new attribute 'login'. This will filter the results by author allowing you to have recent products per vendor. | |
Please Note: This code relies on a 3rd party plugin from Matt Gates. (http://shop.mgates.me/shop/wc-marketing/wc-product-vendor/) | |
Usage: [pv_recent_products per_page="5" login="usernicename" ] | |
All attributes are documentent on the woocommerce docs page above. | |
Code: Insert this code into your functions.php in your wordpress theme. | |
Author: Jamie Madden (https://github.com/digitalchild) | |
*/ | |
if (class_exists('PV_Vendors')) { | |
function pv_recent_products( $atts ) { | |
global $woocommerce_loop; | |
extract( shortcode_atts( array( | |
'per_page' => '12', | |
'login' => '', | |
'columns' => '4', | |
'orderby' => 'date', | |
'order' => 'desc' | |
), $atts ) ); | |
$meta_query = WC()->query->get_meta_query(); | |
$user = get_user_by('slug', $login); | |
if (!empty($user)) { | |
$author = $user->ID; | |
} else $author = ''; | |
$args = array( | |
'post_type' => 'product', | |
'post_status' => 'publish', | |
'author' => $author, | |
'ignore_sticky_posts' => 1, | |
'posts_per_page' => $per_page, | |
'orderby' => $orderby, | |
'order' => $order, | |
'meta_query' => $meta_query | |
); | |
ob_start(); | |
$products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $args, $atts ) ); | |
$woocommerce_loop['columns'] = $columns; | |
if ( $products->have_posts() ) : ?> | |
<?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 endif; | |
wp_reset_postdata(); | |
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>'; | |
} | |
add_shortcode( 'pv_recent_products', 'pv_recent_products' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment