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 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' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just pass extra attribute
order="DESC"
in your shortcode.