'How to get productlist by "category slug" in wordpress

I am working in wordpress and i am trying to get "productlist" by "category slug",So for this i am using following code

$terms = get_terms('product_cat', array('hide_empty' => 0, 'number' => $limit,'offset' => $offset, 'parent' => 5583 ));
  1. i want to use slug instead of id
  2. How can i get images also "brand_image","name","slug","image" Thank you in advance


Solution 1:[1]

Correct Solution

According to the WooCommerce documentation the linked solution from mujuonly isn't recommended.

Please Check this recommended solution

If you get the products with wc_get_products($args); (by slug or id) according to the above linked solution, you can display the product with image(s) as follows:

Display Product with main image:

<?php
//specify $args according to above linked solution
$products = wc_get_products($args);
foreach ($products as $product) :

    $product_id = $product->get_id();
    $product_type = $product->get_type();
    $product_title = $product->get_title();
    $product_permalink = $product->get_permalink();
    $product_regular_price = $product->get_regular_price();
    $product_sale_price = $product->get_sale_price();
    $product_short_desc = $product->get_short_description();
    $product_categories = $product->get_categories();     

    $product_main_image_id = $product->get_image_id();
    if ($product_main_image_id) :
        $product_main_image = wp_get_attachment_image_src($product_main_image_id, 'full');
        $product_main_image_alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', TRUE);
    endif;
?>
    <div class="custom-product custom-product-<?php echo $product_id ?>">
        <a href="<?php echo $product_permalink ?>">
            <?php if ($product_main_image) : ?>
                <img src="<?php echo $product_main_image[0]; ?>" alt="<?php echo $product_main_image_alt ?>">
            <?php endif; ?>
            <h1 class="custom-product-title"><?php echo $product_title ?></h1>
            <!-- display more product infos -->
        </a>
    </div>
<?php endforeach; ?>

Display Product with product gallery images:

<?php
//specify $args according to above linked solution
$products = wc_get_products($args);
foreach ($products as $product) :

    $product_id = $product->get_id();
    $product_type = $product->get_type();
    $product_title = $product->get_title();
    $product_permalink = $product->get_permalink();
    $product_regular_price = $product->get_regular_price();
    $product_sale_price = $product->get_sale_price();
    $product_short_desc = $product->get_short_description();
    $product_categories = $product->get_categories();

    $product_gallery_image_ids = $product->get_gallery_image_ids();
?>
    <div class="custom-product custom-product-<?php echo $product_id; ?>">
        <a href="<?php echo $product_permalink; ?>">
            <?php if ($product_gallery_image_ids) : ?>
                <div class="custom-product-gallery grid">
                    <?php foreach ($product_gallery_image_ids as $product_gallery_image_id) :
                        $product_gallery_image = wp_get_attachment_image_src($product_gallery_image_id, 'full');
                        $product_gallery_image_alt = get_post_meta($product_gallery_image_id, '_wp_attachment_image_alt', TRUE);
                    ?>
                        <img class="custom-product-gallery-image grid-item" src="<?php echo $product_gallery_image[0]; ?>" alt="<?php echo $product_gallery_image_alt; ?>">
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
            <h1 class="custom-product-title"><?php echo $product_title; ?></h1>
            <!-- display more product infos -->
        </a>
    </div>
<?php endforeach; ?>

Notes:
WooCommerce Docs: product data
Wordpress Docs: get attachement image by id

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1