ĐĂNG TIN
logo
Online:
Visits:
Stories:
Profile image
Tác giả: fcwordpress
Trang tin cá nhân | Bài đã đăng
Lượt xem

Hiện tại:
1h trước:
24h trước:
Tổng số:
Như thế nào để làm phần sản phẩm vừa xem trong woocommerce
Saturday, January 2, 2016 1:27
% of readers think this story is Fact. Add your two cents.


Woocommerce là một plugin rất phổ biến hiện nay, nó được sử dụng để làm web shop. Có nhiều khách hàng yêu cầu là phải có phần sản phẩm vừa xem. Hôm nay tôi sẽ chia sẽ cách làm. Thực tế thì trong woocommerce thì đã có sẵn một widget làm vấn đề này rồi, tuy nhiên nếu bạn không muốn filter hay can thiệp vào trong code của woocommerce để nó có được hiển thị đúng với layout thiết kế của khách, thì có thể làm theo cách của tôi.

Đầu tiên ta cần dùng session để lưu các id vào mảng rồi chúng ta xài hook woocommerce_before_single_product để session được thực hiện. Dưới đây là toàn bộ code để làm việt này.

  • Add session để lưu mảng id product
ID; if(!isset($_SESSION['viewed_product'])){ $_SESSION['viewed_product'] = array(); } if(!isset($_SESSION['viewed_product'][$productId])){ $_SESSION['viewed_product'][$productId] = time(); } } ?>
  • Get list id sản phẩm đã xem
$list_id_product = array(); if(!empty($_SESSION['viewed_product'])): foreach($_SESSION['viewed_product'] as $key => $value): array_push($list_id_product,$key ); endforeach; endif;
  • Dùng vòng lặp truy xuất sản phẩm đã xem
 'product', 'post__in' => $list_id_product, 'posts_per_page' => $shownumber, 'post_status' => 'publish' ); $wp_query = new WP_Query( $args ); if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); //thông tin sản phẩm ở đây 

Tin nổi bật trong ngày
Tin mới nhất

Register

Newsletter

Email this story

If you really want to ban this commenter, please write down the reason:

If you really want to disable all recommended stories, click on OK button. After that, you will be redirect to your options page.