WooCommerce 的cookie使用:
- woocommerce_cart_hash — 处理决定购物车何时更新或被刷新的帮助器。必须从缓存中排除,以确保购物车反映正确的项目。
- woocommerce_items_in_cart — 另一个帮助器,帮助WooCommerce确定购物车中的正确数据。必须从缓存中排除,以确保购物车反映正确的项目。
- wp_woocommerce_session — 这个cookie包含了访客的唯一代码,分配给每个客户,它帮助WooCommerce在数据库中为每个客户找到购物车数据。再次强调,你必须将此排除在你的购物车之外。
- woocommerce_recently_viewed — 最近浏览的小工具,同样,你必须从缓存中排除这个cookies,以确保最近浏览的小工具对每个访客都能正确显示。
当清理页面缓存状态的时候,记得清理相关cookies:
//最近浏览的商品 ,利用了woocommerce自带cookie
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 query return results
if ($r->have_posts()) {
$content = '<ul class="rc_wc_rvp_product_list_widget">';
// Start the loop
while ($r->have_posts()) {
$r->the_post();
global $product;
$content .= '<li>
<a href="' . get_permalink() . '">
' . (has_post_thumbnail() ? get_the_post_thumbnail($r->post->ID, 'shop_thumbnail') : woocommerce_placeholder_img('shop_thumbnail')) . ' ' . get_the_title() . '
</a> ' . $product->get_price_html() . '
</li>';
}
$content .= '</ul>';
}
// 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");