Woocommerce 最近查看的产品 [英] Woocommerce recently viewed Products

查看:37
本文介绍了Woocommerce 最近查看的产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个最近查看的脚本,该脚本生成了一个短代码,然后我将其插入到我的主页中.

I have created a recently viewed script which generated a shortcode which I then inserted into my home page.

该脚本的设计目的是让可能访问过我的网站然后离开的人在回来后可以立即看到他们上次访问时浏览过的产品.

The script is designed so that people who may have visited my website and left, once they come back can see instantly what products they had been viewing on their last visit.

我已经放置了短代码 [woocommerce_recently_viewed_products]

并使用以下脚本生成了短代码:

and have generated the shortcode using the following script:

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();

wc_setcookie( 'woocommerce_recently_viewed', implode( '|', $viewed_products ) );
}

// Get products per page
if( !isset( $per_page ) ? $number = 4 : $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");

一切似乎都已注册.但是,当我自己测试时.我查看了一些产品,回到注册短代码的主页,我看到了文字

Everything seems to have registered. However,when I test this myself. I view a few products, go back to the homepage where the shortcode is registered and I see the text

您还没有浏览过任何产品!

You have not viewed any product yet!

我无法弄清楚为了注册和展示我或潜在客户可能已经查看过的产品可能缺少什么.

I can not figure out what might be missing in order to register and show the products which I or a potential customer may have viewed.

推荐答案

Woocommerce 仅保存最近查看的 cookie 如果 woocommerce_recently_viewed_products WIDGET 处于活动状态!请参阅 wc-product-functions.php wc_track_product_view() 函数中的代码.

Woocommerce only save the recently viewed cookie IF woocommerce_recently_viewed_products WIDGET is ACTIVE! See code in wc-product-functions.php wc_track_product_view() function.

用于在functions.php中始终保存cookie的代码:

Code to save the cookie always in functions.php:

/**
 * Track product views. Always.
 */
function wc_track_product_view_always() {
    if ( ! is_singular( 'product' ) /* xnagyg: remove this condition to run: || ! is_active_widget( false, false, 'woocommerce_recently_viewed_products', true )*/ ) {
        return;
    }

    global $post;

    if ( empty( $_COOKIE['woocommerce_recently_viewed'] ) ) { // @codingStandardsIgnoreLine.
        $viewed_products = array();
    } else {
        $viewed_products = wp_parse_id_list( (array) explode( '|', wp_unslash( $_COOKIE['woocommerce_recently_viewed'] ) ) ); // @codingStandardsIgnoreLine.
    }

    // Unset if already in viewed products list.
    $keys = array_flip( $viewed_products );

    if ( isset( $keys[ $post->ID ] ) ) {
        unset( $viewed_products[ $keys[ $post->ID ] ] );
    }

    $viewed_products[] = $post->ID;

    if ( count( $viewed_products ) > 15 ) {
        array_shift( $viewed_products );
    }

    // Store for session only.
    wc_setcookie( 'woocommerce_recently_viewed', implode( '|', $viewed_products ) );
}

remove_action('template_redirect', 'wc_track_product_view', 20);
add_action( 'template_redirect', 'wc_track_product_view_always', 20 );

这篇关于Woocommerce 最近查看的产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆