Woocommerce Get Products

Woocommerce get products

<?php  
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
'product_cat' => 'hoodies'
);

$loop = new WP_Query( $args );

while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<br /><a href="'.get_permalink().'">' . woocommerce_get_product_thumbnail().' '.get_the_title().'</a>';
endwhile;

wp_reset_query();
?>

This will list all product thumbnails and names along with their links to product page. change the category name and posts_per_page as per your requirement.

How to get all products from current WooCommerce product category?

Updated

On product category archive pages, to get the current product category term you will use:

  • Wordpress function get_queried_object() (to get the WP_Term Oject).
  • or Wordpress function get_queried_object_id() (to get the term Id).

Using directly the taxonomy parameter in a WP_Query is deprecated since WordPress 3.1. Instead you will use a tax query as follow:

<?php
// Get The queried object ( a WP_Term or a WP_Post Object)
$term = get_queried_object();

// To be sure that is a WP_Term Object to avoid errors
if( is_a($term, 'WP_Term') ) :

// Setup your custom query
$loop = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'tax_query' => array( array(
'taxonomy' => 'product_cat', // The taxonomy name
'field' => 'term_id', // Type of field ('term_id', 'slug', 'name' or 'term_taxonomy_id')
'terms' => $term->term_id, // can be an integer, a string or an array
) ),
) );

if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div style="margin:8px; text-align:center;">
<a href="'.get_the_permalink().'">';
the_post_thumbnail();
the_title();
echo '</a></div>';
endwhile;
wp_reset_postdata(); // Remember to reset
endif; endif;
?>

Tested and works.

Documentation: WP_Query and Taxonomy Parameters

Get products from last orders in WooCommerce

You can indeed use wc_get_orders(), where you will retrieve the orders based on arguments, such as the user ID and order by date.

Also note that a limit of 10 is used. This is because we can assume that every order contains at least 1 product. So if you want to collect 10 products the limit will never be more than 10 orders:

// Args
$args = array(
'customer_id' => get_current_user_id(),
'limit' => 10,
'orderby' => 'date',
'order' => 'DESC',
'status' => array( 'wc-on-hold','wc-processing','wc-completed' ),
);

// Get orders
$orders = wc_get_orders( $args );

// NOT empty
if ( ! empty ( $orders ) ) {
// Initalize
$product_ids = array();

foreach ( $orders as $order ) {
// Loop through order items
foreach ( $order->get_items() as $item ) {
// Get product ID
$product_id = $item->get_product_id();

// Limit of 10 products
if ( count( $product_ids ) < 10 ) {
// Push to array
$product_ids[] = $product_id;
} else {
break;
}
}
}

// The output
echo '<pre>', print_r( $product_ids, 1 ), '</pre>';
}

A much lighter and faster solution is to use a custom SQL query:

global $wpdb;

$current_user = wp_get_current_user();
$customer_email = $current_user->user_email;

$result = $wpdb->get_col( "
SELECT oim.meta_value FROM {$wpdb->prefix}posts AS p
INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
INNER JOIN {$wpdb->prefix}woocommerce_order_items AS oi ON p.ID = oi.order_id
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS oim ON oi.order_item_id = oim.order_item_id
WHERE p.post_status IN ( 'wc-on-hold','wc-processing','wc-completed' )
AND pm.meta_key = '_billing_email'
AND pm.meta_value = '$customer_email'
AND oim.meta_key = '_product_id'
AND oim.meta_value != 0
ORDER BY p.post_date DESC LIMIT 0, 10
" );

// The raw output
echo '<pre>', print_r( $result, 1 ), '</pre>';

Whichever option you prefer. These can in any case be expanded with, for example, the variants, of variable products or on the basis of multiple order statuses. Excluding duplicate products, etc.. So it depends on your needs

Get all WooCommerce products within own plugin

Try this

if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
// Put your plugin code here
add_action( 'woocommerce_loaded', 'generate_product_qr_codes' );
}

function generate_product_qr_codes() {

$args = array( 'post_type' => 'product', 'posts_per_page' => -1 );
$products = get_posts( $args );
echo '<pre>$products:-';
print_r( $products );
echo '</pre>';
}

Get last product from an order in WooCommerce

To get the latest product from a WC_Order Object, you can use end() function from order items array, like:

$order_items = $order->get_items(); // Get order items
$last_item = end($order_items); // Latest WC_Order_Item_Product Object instance
$product = $last_item->get_product(); // Latest WC_Product Object instance

Related: Get Order items and WC_Order_Item_Product in WooCommerce 3

WooCommerce Get the product object from the product title

With get_page_by_title() WordPress function, you will not get the WC_Product objet but if it's working you will get the WP_Post object.

So here is a custom built function that will output the WC_Product object, if the title matches with a real product title:

function get_wc_product_by_title( $title ){
global $wpdb;

$post_title = strval($title);

$post_table = $wpdb->prefix . "posts";
$result = $wpdb->get_col("
SELECT ID
FROM $post_table
WHERE post_title LIKE '$post_title'
AND post_type LIKE 'product'
");

// We exit if title doesn't match
if( empty( $result[0] ) )
return;
else
return wc_get_product( intval( $result[0] ) );
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

EXAMPLE USAGE:

// Your title set in a variable
$title = "200x Slopupovací pleťová Rusk";

// Using our custom function to get an instance of the WC_Product object
$product_obj = get_wc_product_by_title( $title );

// Testing the output
print_r($product_obj);

This code is tested on WooCommerce 3+ and works.



Related Topics



Leave a reply



Submit