[php] WooCommerce return product object by id

I am creating a custom theme for woocommerce and I need to be able to create a mini product display. I am having problems finding documentation on the woocommerce api. I have a comma delimited list of product IDs that I need to iterate through and display a custom mini product display for each in sequence.

$key_values = get_post_custom_values('rel_products_ids');
//get comma delimited list from product

$rel_product_ids = explode(",", trim($key_values, ",")); 
// create array of just the product ids

foreach ( $rel_product_ids as $pid ) { 
    //sequentially get each id and do something with it

    $loop = new WP_Query( array( 'post__in' => $pid ) );
    // also tried ...
    //$loop = new WP_Query( array( 'ID' => $pid ) );

    while ( $loop->have_posts() ) : $loop->the_post(); $_product = &new WC_Product( $loop->post->ID );
        //do stuff here I have stripped the html in favor of getting to the meat of the issue
        woocommerce_show_product_sale_flash( $post, $_product );
        if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_single');
        get_permalink( $loop->post->ID );
        the_title(); 
        $_product->get_price_html();
    endwhile;
}

Any help would be appreciated.

Thank you,

Tim

This question is related to php wordpress woocommerce

The answer is


global $woocommerce;
var_dump($woocommerce->customer->get_country());
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    $product = new WC_product($cart_item['product_id']);
    var_dump($product);
}

Alright, I deserve to be throttled. definitely an RTM but not for WooCommerce, for Wordpress. Solution found due to a JOLT cola (all hail JOLT cola).

TASK: Field named 'related_product_ids' added to a custom post type. So when that post is displayed mini product displays can be displayed with it.

PROBLEM: Was having a problem getting the multiple ids returned via WP_Query.

SOLUTION:

$related_id_list          = get_post_custom_values('related_product_ids');
    // Get comma delimited list from current post
$related_product_ids      = explode(",", trim($related_id_list[0],','));
    // Return an array of the IDs ensure no empty array elements from extra commas
$related_product_post_ids = array( 'post_type' => 'product', 
                                   'post__in'  => $related_product_ids,
                                   'meta_query'=> array( 
                                        array( 'key'    => '_visibility',
                                               'value'  => array('catalog', 'visible'),'compare' => 'IN'
                                        )
                            ) 
);      
    // Query to get all product posts matching given IDs provided it is a published post
$loop = new WP_Query( $related_posts );
    // Execute query
while ( $loop->have_posts() ) : $loop->the_post(); $_product = get_product( $loop->post->ID );
    // Do stuff here to display your products 
endwhile;

Thank you for anyone who may have spent some time on this.

Tim


Use this method:

$_product = wc_get_product( $id );

Official API-docs: wc_get_product


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to wordpress

#1273 – Unknown collation: ‘utf8mb4_unicode_520_ci’ How to get WooCommerce order details Wordpress plugin install: Could not create directory WooCommerce: Finding the products in database How to get post slug from post in WordPress? How to get featured image of a product in woocommerce Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\wordpress\wp-includes\class-http.php on line 1610 Use .htaccess to redirect HTTP to HTTPs Load More Posts Ajax Button in WordPress How to decode encrypted wordpress admin password?

Examples related to woocommerce

How to get WooCommerce order details WooCommerce: Finding the products in database How to get featured image of a product in woocommerce How to display Woocommerce product price by ID number on a custom page? Get cart item name, quantity all details woocommerce Woocommerce, get current product id How to modify WooCommerce cart, checkout pages (main theme portion) How can I get customer details from an order in WooCommerce? How can I get the order ID in WooCommerce? Woocommerce get products