Woocommerce最近浏览过产品

时间:2017-11-11 20:24:38

标签: wordpress woocommerce shortcode

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

该脚本旨在让那些可能访问过我的网站并离开的人一旦回来就可以立即看到他们上次访问时看到的产品。

我已放置短代码[woocommerce_recently_viewed_products]

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

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

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

  

您尚未查看任何产品!

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

3 个答案:

答案 0 :(得分:0)

您需要在查看单个产品页面时设置cookie,因此请使用类似这样的内容,我将cookie设置为等于我刚看过的产品ID。在您的情况下,如果存在cookie值,您需要获取cookie值,然后将新产品附加到产品列表中。

function set_user_visited_product_cookie() {
    global $post;

    if ( is_product() ){
        // manipulate your cookie string here, explode, implode functions
        wc_setcookie( 'woocommerce_recently_viewed', $post->ID );
    }
}

add_action( 'wp', 'set_user_visited_product_cookie' );

答案 1 :(得分:0)

以下代码设置Cookie&#39; woocommerce_recently_viewed&#39;为我工作。希望它有助于其他

$Existing_product_id = $_COOKIE['woocommerce_recently_viewed'];
if ( is_product() )
{ 
   $updated_product_id = $Existing_product_id.'|'.$post->ID;
   wc_setcookie( 'woocommerce_recently_viewed',  $updated_product_id ); 
}

答案 2 :(得分:0)

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

始终将cookie保存在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 );