Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Cart products id on checkout WooCommerce page, to display product images

I'm using woocommerce on a site I'm working on and I want to display the current product thumbnail at the top of the checkout page, so the user could take a look at what his going to buy.

However I can't find any way to do so.

The closest I got, is to use WC::cart->get_cart(), but this outputs a list of all products.

How can I achieve this?

Thanks

like image 321
Avishay28 Avatar asked Aug 05 '16 23:08

Avishay28


People also ask

How do I get the product details in checkout page WooCommerce?

To get the product ID on checkout page or order page you need to first get the order ID and then extract the details about the order that includes the items that have been added to the cart and are on the order.

How do I find my WooCommerce cart product ID?

You can use directly $product_id variable of the first item in cart. 2) Using an array of product IDs (one for each item in cart). To get the 1st item product ID: $products_ids_array[0]; To get the 2nd item product ID: $products_ids_array[1]; etc…

How do I get cart item data in WooCommerce?

There are a few different ways that you can get cart item data in WooCommerce. The first way is to use the WC()->cart->get_cart_contents() method. If you just need a specific piece of data for each item, you can loop through the array and use WC()->cart->get_item_data().

How do I find the order ID in WooCommerce checkout?

You have access to $order_id variable If you have access to the order ID (once again, usually the do_action or apply_filters might give you this), you have to get the order object first. Then do the exact same things as above. $order = wc_get_order( $order_id ); // Now you have access to (see above)...


1 Answers

Yes it's possible writing a custom function.

To display those images at the beginning of checkout page just after your header's theme, use this code:

add_action('woocommerce_before_checkout_form', 'displays_cart_products_feature_image');
function displays_cart_products_feature_image() {
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        $product = $cart_item['data'];
        if(!empty($product)){
            // $image = wp_get_attachment_image_src( get_post_thumbnail_id( $product->ID ), 'single-post-thumbnail' );
            echo $product->get_image();

            // to display only the first product image uncomment the line below
            // break;
        }
    }
}

This code snippet goes on function.php file of your active child theme or theme

You can change the images properties adding some options in get_image() function.

This code is tested and fully functional


OTHER USAGES - You can also use it:

1). With the following others checkout WooCommerce hooks (replacing the first line in the snippet code with one of this):

• Before customer details:

add_action('woocommerce_checkout_before_customer_details', 'displays_cart_products_feature_image');

• After customer details:

add_action('woocommerce_checkout_after_customer_details', 'displays_cart_products_feature_image');

• Before order review:

add_action('woocommerce_checkout_before_order_review', 'displays_cart_products_feature_image');

2). Directly inside your woocommerce templates (this snippet code goes on function.php file of your active child theme or theme):

function displays_cart_products_feature_image() {
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        $product = $cart_item['data'];
        if(!empty($product)){
            // $image = wp_get_attachment_image_src( get_post_thumbnail_id( $product->ID ), 'single-post-thumbnail' );
            echo $product->get_image();

            // to display only the first product image uncomment the line below
            // break;
        }
    }
}

Then you will just paste one of this inside the template file:

  • inside HTML code: <?php displays_cart_products_feature_image(); ?>
  • inside PHP code: displays_cart_products_feature_image();

Reference:

  • Class WC_Cart | WooCommerce 2.1.x Code Reference
  • Class WC_Product | WooCommerce 3.x Code Reference
  • Template Structure + Overriding Templates via a Theme
like image 95
LoicTheAztec Avatar answered Oct 07 '22 21:10

LoicTheAztec