Skip Navigation

[Resolved] Filter products by the ones which are currently in the cart

This thread is resolved. Here is a description of the problem and solution.

Problem:

The user wanted to display a view of products according to the ones which are currently in the cart on the checkout page. Although not possible using native Toolset tools, custom PHP code can be used to achieve this.

Solution:

- Utilize WooCommerce hooks to get the list of products currently in the cart.
- Use the following custom PHP code and add it to your website:

function wpc_elementor_shortcode( $atts ) {
 
    // Get full cart object
    $items = WC()->cart->get_cart();  
 
    // Initialize array to store product IDs
    $product_ids = array();
 
    // Loop through cart items
    foreach( $items as $item => $values ) {
 
        // Load product object
        $product = wc_get_product( $values['data']->get_id() );
 
        // Access product data using product object
        $product_id = $product->get_id();
 
        // Add product ID to array
        $product_ids[] = $product_id;
 
    }
 
    // Join product IDs into comma-separated string
    $product_ids_string = implode( ', ', $product_ids );
 
    // Output comma-separated list of product IDs
    echo $product_ids_string;
 
}
 
add_shortcode( 'my_elementor_php_output', 'wpc_elementor_shortcode');

- Use the shortcode [my_elementor_php_output] to output the comma-separated list of product IDs currently in the cart.

Relevant Documentation:

WooCommerce - Get Products in Cart: https://wpdavies.dev/woocommerce-get-products-in-cart/

Toolset - Adding Custom Code: https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

PHP Implode function: https://www.php.net/manual/en/function.implode.php

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

This topic contains 4 replies, has 2 voices.

Last updated by Ido Angel 1 year, 1 month ago.

Assisted by: Christopher Amirian.

Author
Posts
#2570117

Hey, long time no support request 🙂
Is it possible in anyway to display a view of products according to the ones which are currently in the cart?
Basically the idea is to display product details (images, custom fields, maybe video) on the checkout page.
thanks!
Ido

#2571377

Christopher Amirian
Supporter

Languages: English (English )

Hi there and welcome back 🙂

It is not possible with native Toolset tools, but you can use custom PHP coding to utilize Woocommerce hooks to get the list of the products that are currently in the cart.

I googled it and found this that might help you:

hidden link

You can add the PHP code here:

https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

But please consider that this will be just a starting point as if the Cart you use is updated without reloading the page using Ajax javascript, then the number in the checkout page will not be the same, unless you reload the page.

You are always welcome to use expert developers services if needed:

https://toolset.com/contractors/

Thanks.

#2571397

Thanks!

I tried this:

// Shortcode to output custom PHP in Elementor
function wpc_elementor_shortcode( $atts ) {

// Get full cart object
$items = WC()->cart->get_cart();

// Loop through cart items
foreach( $items as $item => $values ) {

    // Load product object
    $product = wc_get_product( $values['data']->get_id() );
	
	// Access product data using product object
    $product_id = $product->get_id();
	
	echo $product_id;

}
	
}
add_shortcode( 'my_elementor_php_output', 'wpc_elementor_shortcode');

But I'm getting a string which is not comma separated. If you could just help me turn this into a comma separated list, that would be great!

#2571813

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

You can use the PHP Implode function to create a comma-separated list. I think it should be something like this code:

function wpc_elementor_shortcode( $atts ) {

    // Get full cart object
    $items = WC()->cart->get_cart();  

    // Initialize array to store product IDs
    $product_ids = array();

    // Loop through cart items
    foreach( $items as $item => $values ) {

        // Load product object
        $product = wc_get_product( $values['data']->get_id() );

        // Access product data using product object
        $product_id = $product->get_id();

        // Add product ID to array
        $product_ids[] = $product_id;

    }

    // Join product IDs into comma-separated string
    $product_ids_string = implode( ', ', $product_ids );

    // Output comma-separated list of product IDs
    echo $product_ids_string;

}

add_shortcode( 'my_elementor_php_output', 'wpc_elementor_shortcode');

Here is more information about Implode function:

hidden link

Thanks.

#2571821

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.