Created
May 1, 2017 05:18
-
-
Save dasbairagya/f11427e62d219da2c4f978517f99c9a4 to your computer and use it in GitHub Desktop.
short code to get the woocommerce recently viewed products
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
//short code to get the woocommerce recently viewed products | |
<?php function custom_track_product_view() { | |
if ( ! is_singular( 'product' ) ) { | |
return; | |
} | |
global $post; | |
if ( empty( $_COOKIE['woocommerce_recently_viewed'] ) ) | |
$viewed_products = array(); | |
else | |
$viewed_products = (array) explode( '|', $_COOKIE['woocommerce_recently_viewed'] ); | |
if ( ! in_array( $post->ID, $viewed_products ) ) { | |
$viewed_products[] = $post->ID; | |
} | |
if ( sizeof( $viewed_products ) > 15 ) { | |
array_shift( $viewed_products ); | |
} | |
// Store for session only | |
wc_setcookie( 'woocommerce_recently_viewed', implode( '|', $viewed_products ) ); | |
} | |
add_action( 'template_redirect', 'custom_track_product_view', 20 ); | |
function rc_woocommerce_recently_viewed_products( $atts, $content = null ) { | |
// Get shortcode parameters | |
extract(shortcode_atts(array( | |
"per_page" => '5' | |
), $atts)); | |
// Get WooCommerce Global | |
global $woocommerce; | |
// Get recently viewed product cookies data | |
$viewed_products = ! empty( $_COOKIE['woocommerce_recently_viewed'] ) ? (array) explode( '|', $_COOKIE['woocommerce_recently_viewed'] ) : array(); | |
$viewed_products = array_filter( array_map( 'absint', $viewed_products ) ); | |
// If no data, quit | |
if ( empty( $viewed_products ) ) | |
return __( 'You have not viewed any product yet!', 'rc_wc_rvp' ); | |
// Create the object | |
ob_start(); | |
// Get products per page | |
if( !isset( $per_page ) ? $number = 5 : $number = $per_page ) | |
// Create query arguments array | |
$query_args = array( | |
'posts_per_page' => $number, | |
'no_found_rows' => 1, | |
'post_status' => 'publish', | |
'post_type' => 'product', | |
'post__in' => $viewed_products, | |
'orderby' => 'rand' | |
); | |
// Add meta_query to query args | |
$query_args['meta_query'] = array(); | |
// Check products stock status | |
$query_args['meta_query'][] = $woocommerce->query->stock_status_meta_query(); | |
// Create a new query | |
$r = new WP_Query($query_args); | |
// ---- | |
if (empty($r)) { | |
return __( 'You have not viewed any product yet!', 'rc_wc_rvp' ); | |
}?> | |
<?php while ( $r->have_posts() ) : $r->the_post(); | |
$url= wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); | |
?> | |
<!-- //put your theme html loop hare --> | |
<li > | |
<a class="product-picture" href="<?php echo get_post_permalink(); ?>" title="Show details for Watches"> | |
<img alt="Picture of Watches" src="<?php echo $url;?>" title="Show details for Watches" /> | |
</a> | |
<a class="product-name" href="<?php echo get_post_permalink(); ?>"><?php the_title()?></a> | |
</li> | |
<!-- end html loop --> | |
<?php endwhile; ?> | |
<?php wp_reset_postdata(); | |
return '<div class="woocommerce columns-5 facetwp-template">' . ob_get_clean() . '</div>'; | |
// ---- | |
// Get clean object | |
$content .= ob_get_clean(); | |
// Return whole content | |
return $content; | |
} | |
// Register the shortcode | |
add_shortcode("woocommerce_recently_viewed_products", "rc_woocommerce_recently_viewed_products"); | |
?> |
Only display product in cart , how to display them without in cart, please
Hello,
Can you confirm that it does now work if we do not have the recently viewed widgets on the shop ?
how to do without this recently viewed widget ?
thank you.
Hi there!
Please contact me on 8509848755 (whatsapp)
Regards,
Gopal Dasbairagya
7585888052
…On Thu, Sep 26, 2019 at 11:29 PM makeonlineshop ***@***.***> wrote:
Hello, sorry, can you explain me how to use ?
Can I display the recently viewed products on a single page as Amazon is
doing ?
thank you.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://gist.github.com/f11427e62d219da2c4f978517f99c9a4?email_source=notifications&email_token=AELB5UOGOK2QSHC2EOVDE3LQLTZ7PA5CNFSM4I25GWE2YY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAFZOO4#gistcomment-3038446>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AELB5UMGRKII6LGPJEGVZ3DQLTZ7PANCNFSM4I25GWEQ>
.
Hi there!
thank you.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Put this code where you want to show the recently viewed products.
Remember you have to put this code in place of your html loop.
Look at line no. 70. Here my html loop is written to show the products like html does.