Created
June 12, 2020 09:08
-
-
Save farookibrahim/3fc815700bab3dd9cfd82a896650a1c0 to your computer and use it in GitHub Desktop.
Electro - Single product page add recently viewed products after recommended products
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
/** | |
* Track product views. | |
*/ | |
if( ! function_exists( 'electro_wc_track_product_view' ) ) { | |
function electro_wc_track_product_view() { | |
if ( ! is_singular( 'product' ) ) { | |
return; | |
} | |
global $post; | |
if ( empty( $_COOKIE['electro_wc_recently_viewed'] ) ) { | |
$viewed_products = array(); | |
} else { | |
$viewed_products = (array) explode( '|', $_COOKIE['electro_wc_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( 'electro_wc_recently_viewed', implode( '|', $viewed_products ) ); | |
} | |
} | |
add_action( 'template_redirect', 'electro_wc_track_product_view', 20 ); | |
if( ! function_exists( 'electro_before_footer_products_recently_viewed' ) ) { | |
function electro_before_footer_products_recently_viewed() { | |
$viewed_products = ! empty( $_COOKIE['electro_wc_recently_viewed'] ) ? (array) explode( '|', $_COOKIE['electro_wc_recently_viewed'] ) : array(); | |
$viewed_products = array_reverse( array_filter( array_map( 'absint', $viewed_products ) ) ); | |
if ( empty( $viewed_products ) ) { | |
return; | |
} | |
$ids = implode( ',', $viewed_products ); | |
?> | |
<section class="products-recently-viewed"> | |
<div class="container"> | |
<header> | |
<h2 class="h1"><?php echo esc_html__( 'Recently Viewed Products', 'electro' ); ?></h2> | |
</header> | |
<?php echo do_shortcode( '[products columns=4 limit=8 ids='.$ids.']' ); ?> | |
</div> | |
</section> | |
<?php | |
} | |
} | |
add_action( 'woocommerce_after_single_product_summary', 'electro_before_footer_products_recently_viewed', 22 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment