Last active
August 27, 2019 08:05
-
-
Save itzmekhokan/5529c7a642b26db901c4cab39b941f67 to your computer and use it in GitHub Desktop.
Show recently viewed products using shortcode in woocommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); |
Thanks. And you just gave me a good point to update my above codes and that is done. Now you can pass extra supported attributes on this shortcode like - [recently_viewed_products limit="4"]
Thanks. And you just gave me a good point to update my above codes and that is done. Now you can pass extra supported attributes on this shortcode like -
[recently_viewed_products limit="4"]
Thanks, now i can display only 4 products, but the snippets display only the first products 4 that I saw. After that when I see another product or 10 products is no longer updated. There remain the same 4 products first seen.
Just pass extra attribute order="DESC"
in your shortcode.
Thanks a lot!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great snippet! I tried to limit the number of products to 4, but I didn’t succeed.
Can you help me?