Displaying most recently viewed items on page

ok i want to show the selected products the customer has placed in his cart as recently viewed products is there a way I can do this

more like etsy when you select a product it shows up as recently viewed !!!

Is there a way i can pull the images description and price from the shopping cart

regards

  • Adam Czajczyk

    Hello antony,

    I hope you're well today and thank you for your question!

    That would be possible but would require some custom coding. The first step would be to retrieve card content (only product IDs are stored there) and this code could help you do this:

    $cart_items	= mp_cart()->get_all_items();
    foreach ($cart_items as $cart_item) {
    	foreach ($cart_item as $key=>$value) {
    		/*
    		The $key variable includes an ID of a product in cart;
    		See note #1 below
    		*/
    	}
    }

    This code could be loaded e.g. in page footer header or triggered by some "general" hook and would retrieve the ID's of the products that are added to the cart.

    Note #1:

    We loop through items in the cart here to get their IDs. I think it would be best to save these ID's along with user ID to the database for further use

    Note #2:

    Having aforementioned data saved into database, you could then e.g. create a shortcode that would fetch those saved those IDs associated with a user and based on that ID products could be displayed using MarketPress built-in template tags:

    mp_product_title();
    mp_product_description();
    mp_product_meta()

    and some others. For available template tags (and attributes descriptions) see the

    /wp-content/plugins/marketpress/includes/common/template-functions.php

    file on your server.

    if you have additional questions on this, let me know please.

    Best regards,
    Adam

  • Adam Czajczyk

    Hello antony!

    I have expanded that code for you, it should be fully usable/functional now. In order to implement it on your site follow these steps:

    1. create an empty file with a .php extension (e.g. "marketpress-recently-viewed-shortcode.php")

    2. put following code inside:

    <?php
    function wpmu_mp_recently_viewed_save_cart() {
    
    	// fetch items from the cart
    	$cart_items	= mp_cart()->get_all_items();
    	// proceed only if there are items in cart, otherwise skip
    	if (!empty($cart_items)) {
    
    	$user_id = get_current_user_id(); // get an ID of a currently logged in user
    
    	foreach ($cart_items as $cart_item) {
    		foreach ($cart_item as $key=>$value) { // loop through cart items
    			add_user_meta($user_id, 'mp_recent_viewed_cart_product', $key, false); // save those products to database as user_meta data
    		}
    	}
    	}
    
    }
    
    add_action('wp_footer','wpmu_mp_recently_viewed_save_cart');
    
    # Shortcode for showing recently viewed products
    #
    # usage:
    # [mp-recently-viewed]
    #
    # optional: [mp-recently-viewed num="10"] where 10 is a number of products to display
    # by default it will show up to 5 products
    
    function wpmu_mp_recently_viewed_shortcode($atts) {
    
    	// set default parameters
    	$atts = shortcode_atts( array(
    			'num' => 5
    		), $atts, 'mp-recently-viewed');
    
    	$user_id = get_current_user_id(); // get an ID of a currently logged in user
    	$recent_items_ids = get_user_meta($user_id, 'mp_recent_viewed_cart_product');
    
    	// if there are recent items saved proceed
    	if (!empty($recent_items_ids)) {
    
    		// if there's more than "num" items returned, shorten the list
    
    		if (count($recent_items_ids) > $atts['num'])
    			$recent_items_ids = array_slice($recent_items_ids, 0, $atts['num']);
    
    		// fetch and display products based on saved IDs
    		$html = '<ul class="mp-recently-viewed-products">';
    		$context = 'context';
    		foreach ($recent_items_ids as $recent_item_id) {
    			$html .= '<li>'; 
    
    			$html .= mp_product_title( $recent_item_id, false, true);
    			$html .= get_the_post_thumbnail($recent_item_id);
    			$html .= mp_product_description( $recent_item_id, false);
    			$html .= mp_product_price( false, $recent_item_id, true);
    
    			$html .= '</li>';
    		}
    		$html .= '</ul>';
    		return $html;
    	}
    }
    add_shortcode('mp-recently-viewed','wpmu_mp_recently_viewed_shortcode');

    3. Upload the file to the "/wp-content/mu-plugins" folder of your site; if there's no "mu-plugins" folder inside "wp-content", create it.

    4. Add following shortcode to your site where you wish to display those "recently viewed" products:

    [mp-recently-viewed]

    or alternatively

    [mp-recently-viewed num="7"]

    where the number is a maximum number of products to be displayed (default is 5).

    That seems to be working on my end. You would most likely need to style the output of the shortcode though so it would match the design of your site. The side-effect of the code above is that it actually creates a full history of everything that a user has ever added to the cart. That could be a powerful info, I think :slight_smile: The code could be then expanded to make use of it.

    Please note though: we do not provide custom development so what I shared is pretty much as far as I could go with this. In case you needed some assistance with a simple CSS styling of that, let me know and I'll be happy to help. If you need to further enhance/develop the code, please post a question on our "Jobs & Pros' job board (please note: no WPMU DEV staff involved there!) here:

    https://premium.wpmudev.org/wordpress-development/

    I hope that helps!

    Best regards,
    Adam

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.