Skip to content

Instantly share code, notes, and snippets.

@mariovalney
Last active January 20, 2020 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariovalney/47b240420bac6e506578565ddb4a3058 to your computer and use it in GitHub Desktop.
Save mariovalney/47b240420bac6e506578565ddb4a3058 to your computer and use it in GitHub Desktop.
WC Products Shortcode by User
<?php
/**
*
* Plugin Name: WC Products Shortcode by User
* Description: Ass "current_user" to [products] shortcode to show only products from current user.
* Version: 1.0.0
* Author: Mário Valney
* Author URI: https://mariovalney.com
* Text Domain: wc-products-shortcode-by-user
*
*/
add_filter( 'woocommerce_shortcode_products_query', 'wc_products_shortcode_by_user_query', 10, 3 );
function wc_products_shortcode_by_user_query( $query_args, $attributes, $type ) {
$user_id = get_current_user_id();
if ( $type === 'products_current_user' ) {
$query_args['author'] = $user_id;
}
return $query_args;
}
add_shortcode( 'products_current_user', 'wc_products_shortcode_by_user', 10, 3 );
function wc_products_shortcode_by_user( $atts ) {
$shortcode = new WC_Shortcode_Products( (array) $atts, 'products_current_user' );
return $shortcode->get_content();
}
add_action( 'woocommerce_shortcode_products_current_user_loop_no_results', 'wc_products_shortcode_by_user_no_results' );
function wc_products_shortcode_by_user_no_results() {
// Your HTML for "no results"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment