Skip to content

Instantly share code, notes, and snippets.

@itzmekhokan
Last active August 27, 2019 08:05
Show Gist options
  • Save itzmekhokan/5529c7a642b26db901c4cab39b941f67 to your computer and use it in GitHub Desktop.
Save itzmekhokan/5529c7a642b26db901c4cab39b941f67 to your computer and use it in GitHub Desktop.
Show recently viewed products using shortcode in woocommerce
<?php
function recently_viewed_products_shortcode( $atts ) {
$viewed_products = ! empty( $_COOKIE['woocommerce_recently_viewed'] ) ? (array) explode( '|', wp_unslash( $_COOKIE['woocommerce_recently_viewed'] ) ) : array(); // @codingStandardsIgnoreLine
$viewed_products = array_reverse( array_filter( array_map( 'absint', $viewed_products ) ) );
if ( empty( $viewed_products ) ) return;
$product_ids = implode( ',', $viewed_products );
// if have some attributes
$attr = '';
if( $atts ){
foreach ( $atts as $key => $value ) {
$attr .= $key ."='" . $value . "' ";
}
}
return do_shortcode( "[products ids='$product_ids' $attr]" );
}
function callback_woocommerce_init() {
add_shortcode( 'recently_viewed_products', 'recently_viewed_products_shortcode' );
}
add_action( 'woocommerce_init', 'callback_woocommerce_init' );
@camalex
Copy link

camalex commented Aug 27, 2019

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment