I need to be able to show the products that the logged in user has created, for this I have made this function that generates a ShortCode:
function productosUsuario() {
global $current_user, $user_login;
if ($user_login) {
$product = wc_get_product( '1149' );
$html = '<section id="my-products">
<div class="item">
<figure>'. $product->get_image() .'</figure>
<div class="info">
<span class="sku">'. $product->get_sku() .'</span>
<h2 class="name">'. $product->get_name() .'</h2>
<span class="visits">3 Visits | 1 Contact</span>
</div>
<div class="price">
<h2>us$'. $product->get_price() .'</h2>
</div>
<div class="quality">
<span class="number">85%</span>
<label>Standar Quality</label>
</div>
<div class="actions">
<ul>
<li><a href="/edit-product/?product_id='.
$product->get_id() .'">Edit Product</a></li>
<li><a href="../product/'.
$product->get_slug() .
'">View you Product</a></li>
<li>Pause</li>
<li><a href="/frequent-questions/">I need Help!</a></li>
</ul>
</div>
</div>
</section>';
echo $html;
}
}
add_shortcode( 'productosUsuario', 'productosUsuario' );
The function only shows a specific product but I need it to show the products of the user who is logged in, how do I do that filter and/or condition?
To get the id of the current user we use
get_current_user_id()
:It results in a
(int)
which is0
if there is no user logged in, there is no need to access the global.To get the list of products created by a particular user we use
wc_get_products
, passing theuser_id
asauthor
:Within a shortcode, we define some defaults such as to limit the number of products or choose the author by id, we check that the woocommerce function is available, we buffer the output (inside a theme or plugin you can use get template parts there with the html and give the possibility of overrides), everything together is more or less like this: