Use a session cookie for recently viewed

This commit is contained in:
Mike Jolley 2013-01-12 13:03:38 +00:00
parent c2d45f49bd
commit 52ecd654b7
2 changed files with 15 additions and 19 deletions

View File

@ -64,12 +64,16 @@ class WC_Widget_Recently_Viewed extends WP_Widget {
return;
}
if ( empty( $woocommerce->session->viewed_products ) ) return;
$viewed_products = ! empty( $_COOKIE['woocommerce_recently_viewed'] ) ? (array) explode( '|', $_COOKIE['woocommerce_recently_viewed'] ) : array();
$viewed_products = array_filter( array_map( 'absint', $viewed_products ) );
if ( empty( $viewed_products ) )
return;
ob_start();
extract($args);
extract( $args );
$title = apply_filters('widget_title', empty($instance['title']) ? __('Recently viewed', 'woocommerce' ) : $instance['title'], $instance, $this->id_base);
$title = apply_filters('widget_title', empty( $instance['title'] ) ? __( 'Recently viewed', 'woocommerce' ) : $instance['title'], $instance, $this->id_base);
if ( !$number = (int) $instance['number'] )
$number = 10;
else if ( $number < 1 )
@ -77,7 +81,7 @@ class WC_Widget_Recently_Viewed extends WP_Widget {
else if ( $number > 15 )
$number = 15;
$query_args = array('posts_per_page' => $number, 'no_found_rows' => 1, 'post_status' => 'publish', 'post_type' => 'product', 'post__in' => $woocommerce->session->viewed_products, 'orderby' => 'rand');
$query_args = array('posts_per_page' => $number, 'no_found_rows' => 1, 'post_status' => 'publish', 'post_type' => 'product', 'post__in' => $viewed_products, 'orderby' => 'rand');
$query_args['meta_query'] = array();

View File

@ -1296,12 +1296,12 @@ function woocommerce_get_order_id_by_order_key( $order_key ) {
* @return void
*/
function woocommerce_track_product_view() {
global $post, $product, $woocommerce;
global $post, $product;
$viewed_products = $woocommerce->session->viewed_products;
if ( empty( $woocommerce->session->viewed_products ) )
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;
@ -1309,10 +1309,11 @@ function woocommerce_track_product_view() {
if ( sizeof( $viewed_products ) > 15 )
array_shift( $viewed_products );
$woocommerce->session->viewed_products = $viewed_products;
// Store for session only
setcookie( "woocommerce_recently_viewed", implode( '|', $viewed_products ), 0, COOKIEPATH, COOKIE_DOMAIN, false, true );
}
add_action( 'woocommerce_before_single_product', 'woocommerce_track_product_view', 10);
add_action( 'wp', 'woocommerce_track_product_view', 10 );
/**
* Layered Nav Init
@ -1468,15 +1469,6 @@ function woocommerce_price_filter_init() {
wp_register_script( 'wc-price-slider', $woocommerce->plugin_url() . '/assets/js/frontend/price-slider' . $suffix . '.js', array( 'jquery-ui-slider' ), '1.6', true );
unset( $woocommerce->session->min_price );
unset( $woocommerce->session->max_price );
if ( isset( $_GET['min_price'] ) )
$woocommerce->session->min_price = esc_attr( $_GET['min_price'] );
if ( isset( $_GET['max_price'] ) )
$woocommerce->session->max_price = esc_attr( $_GET['max_price'] );
add_filter( 'loop_shop_post_in', 'woocommerce_price_filter' );
}
}