How to get woocommerce product

How to get woocommerce product

 

WooCommerce marks a product as "featured" using a custom taxonomy (product_visibility) 
with the term featured

$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
'tax_query' => array(
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN',
),
),
);

$featured_query = new WP_Query($args);

if ($featured_query->have_posts()) {
while ($featured_query->have_posts()) {
$featured_query->the_post();
global $product;
echo '<h2>' . get_the_title() . '</h2>';
echo '<p>' . $product->get_price_html() . '</p>';
}
wp_reset_postdata();
} else {
echo 'No featured products found.';
}
// Product fetch based on category

$args = array(
'post_type' => 'product',
'posts_per_page' => -1, // or any number
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug', // or 'id' or 'name'
'terms' => $term->slug, // replace with your category slug
),
),
);

$loop = new WP_Query( $args );

if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<h2>' . get_the_title() . '</h2>';
echo $product->get_price_html();
endwhile;
} else {
echo 'No products found';
}

wp_reset_postdata();


// Get Products Category Name

$term = get_queried_object();
if ( is_tax('product_cat') && $term ) {
  echo $term->name;
  echo $term->slug;
}
// Count products

$args = array(
'post_type' => 'product',
'posts_per_page' => -1, // fetch all products
);

$query = new WP_Query($args);

// Count number of products (rows)
$product_count = $query->post_count;

echo 'Number of products: ' . $product_count;
// get products

function get_product_data(){

?>

<div class="product-listing">

<?php
// The product query
$args = array(
'post_type' => 'product', // WooCommerce products post type
'posts_per_page' => -1, // Fetch all products
// 'posts_per_page' => 10, // set limit
'orderby' => 'date', // Sort by date, you can customize this
'order' => 'ASC', // Order in ascending order, change as needed
'product_cat' => 'natural-stone-granite', // category slug if product fetch based on categories
);

$query = new WP_Query($args);

if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post();
// Get product ID
$product_id = get_the_ID();
$product = wc_get_product($product_id); // Get the product object

// Product details
$product_name = get_the_title();
$product_price = $product->get_price();
$product_description = $product->get_description();
$product_permalink = get_permalink();
$product_image = wp_get_attachment_image_url($product->get_image_id(), 'full'); // Get the product image

// Output the product details
?>
<div class="product-item">
<a href="<?php echo esc_url($product_permalink); ?>">
<img src="<?php echo esc_url($product_image); ?>" alt="<?php echo esc_attr($product_name); ?>" />
</a>
<h3><?php echo esc_html($product_name); ?></h3>
<p class="product-price"><?php echo wc_price($product_price); ?></p>
<p class="product-description"><?php echo wp_trim_words($product_description, 20, '...'); ?></p>
<a href="<?php echo esc_url($product_permalink); ?>" class="btn-view-product">View Product</a>
</div>
<?php
endwhile;
else :
echo '<p>No products found.</p>';
endif;

wp_reset_postdata();
?>

</div>

<?php

}


add_shortcode('get_product', 'get_product_data');

 

 

 

 

 

 

 

Leave a Reply