php 通过访问单个产品页面更新WooCommerce产品元数据

xpcnnkqh  于 12个月前  发布在  PHP
关注(0)|答案(1)|浏览(142)

我试图计算一个产品页面的浏览量,但我做不到。我会很高兴在解决我的问题的任何帮助.

add_action('template_redirect', 'update_product_data');
function update_product_data(){
     if (class_exists('WooCommerce') && is_product()) {
        $product = wc_get_product( get_the_ID() );
        $product_id = $product->get_id();
        $count   = (int) $product->get_meta('_click_counter');

        $count++;
        $product->update_meta_data('_click_counter', $count);
        $product->save();
    }
}

字符串

vs3odd8k

vs3odd8k1#

根据这个答案线索,尝试以下操作:

// Utility function: Updating product views count
function single_product_views_count_update( $product ) {
    if( ! is_a($product, 'WC_Product') ) return;

    $count = (int) $product->get_meta('_views_counter');
    $product->update_meta_data('_views_counter', $count + 1);
    $product->save();
}

// Count single product views
add_action( 'woocommerce_single_product_summary', 'count_single_product_views' );
function count_single_product_views(){
    global $product;
    single_product_views_count_update( $product );
}

字符串
代码放在你的子主题的functions.php文件中(或插件中)。测试和工作。
现在,如果你想避免来自同一用户的重复视图计数,你应该使用以下方法:

// Force non logged users WC Session cookie
add_action( 'woocommerce_init', 'force_non_logged_user_wc_session' );
function force_non_logged_user_wc_session(){ 
    if( is_user_logged_in() || is_admin() )
       return;

    if ( isset(WC()->session) && ! WC()->session->has_session() ) {
       WC()->session->set_customer_session_cookie( true ); 
    }
}

// Utility function: Get user viewed products
function get_user_viewed_products() {
    if ( is_user_logged_in() ) {
        $viewed_products = (array) get_user_meta( get_current_user_id(), 'viewed_products', true);
    } else {
        $viewed_products = (array) WC()->session->get('viewed_products');
    }
    return $viewed_products;
}

// Utility function: Updating user viewed products 
function update_user_viewed_products( $product_id ) {
    if ( is_user_logged_in() ) {
        $user_id    = get_current_user_id();
        $products   = get_user_meta( $user_id, 'viewed_products', true );
        if ( empty($products) ) {
            $products = array();
        }
        $products[] = $product_id;
        update_user_meta( $user_id, 'viewed_products', $products);
    } else {
        $products   = WC()->session->get('viewed_products');
        if ( empty($products) ) {
            $products = array();
        }
        $products[] = $product_id;
        WC()->session->set('viewed_products', $products);
    }
}

// Utility function: Updating product views count
function single_product_views_count_update( $product ) {
    if( ! is_a($product, 'WC_Product') ) return;

    if ( in_array( $product->get_id(), get_user_viewed_products() ) ) {
        return;
    } else {
        update_user_viewed_products( $product->get_id() );
    }

    $count = (int) $product->get_meta('_views_counter');
    $product->update_meta_data('_views_counter', $count + 1 );
    $product->save();
}

// Count single product views
add_action( 'woocommerce_single_product_summary', 'count_single_product_views' );
function count_single_product_views(){
    global $product;
    single_product_views_count_update( $product );
}

**OP添加:**在管理员产品列表中显示产品视图

// Add a custom column to Admin product list
add_filter('manage_edit-product_columns', 'admin_product_views_counter_column');
function admin_product_views_counter_column($columns) {
    $columns['views_count'] = __('View', 'woocommerce');
    return $columns;
}

// Display the views counter for each product in the "View" column  
add_action('manage_product_posts_custom_column', 'admin_product_views_counter_column_content', 10, 2);
function admin_product_views_counter_column_content($column, $post_id) {
    if ($column === 'views_count') {
        global $product;

        echo (int) $product->get_meta('_views_counter');
    }
}

相关问题