Woocommerce. Get a list of products in the current category on the archive page and product page

I'm trying to create a small code that will display a list of discounted products that belong only to the category of products that the user is on, that is, if the user is on the clothing category page, then the code should display only discounted products from the clothing category, I also want the code to work on the product page. For now, here's what I got.

$args = array(
    'post_type'      = 'product',
    'posts_per_page' = '5',
    'product_cat'    = 'clothes',
     );
                            
$args['order'] = 'ASC';
$args['orderby'] = 'popularity';
$products = wc_get_products( $args );
$product-get_id();
$loop = new WP_Query( $args );
                            
while ( $loop-have_posts() ) : $loop-the_post();
       global $product;
       echo 'div class=image_product_lista class=image_product_list href='.get_permalink().'' . woocommerce_get_product_thumbnail().'/a/div';
       echo 'br /a class=product_title_list href='.get_permalink().'' .get_the_title().'/a';
       echo 'br /a class=product_price_list href='.get_permalink().''. $product-get_price_html().'/a';
endwhile;
                            
wp_reset_query();

Topic woocommerce-offtopic php categories archives Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.