Skip to content

Instantly share code, notes, and snippets.

@jackbillstrom
Created May 25, 2020 22:35
Show Gist options
  • Save jackbillstrom/d21e491f2ab878449aaee9c049c304e8 to your computer and use it in GitHub Desktop.
Save jackbillstrom/d21e491f2ab878449aaee9c049c304e8 to your computer and use it in GitHub Desktop.
[WooCommerce] Recently Viewed Products
/**
* goes inside your beloved functions.php
*/
add_shortcode( 'recently_viewed_products', 'code_recently_viewed_products' );
function code_recently_viewed_products() {
$viewed_products = ! empty( $_COOKIE['woocommerce_recently_viewed'] ) ? (array) explode( '|', wp_unslash( $_COOKIE['woocommerce_recently_viewed'] ) ) : array();
$viewed_products = array_reverse( array_filter( array_map( 'absint', $viewed_products ) ) );
if ( empty( $viewed_products ) ) return;
$title = '<h3>Andra har även tittat på</h3>';
$product_ids = implode( ",", $viewed_products );
return $title . do_shortcode("[products ids='$product_ids']");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment