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
Tuesday, August 11, 2015 8:42
% of readers think this story is Fact. Add your two cents.
0

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. Anh/chị chỉ cần copy vào functions.php là được

ID; if(!isset($_SESSION['viewed_product'])){ $_SESSION['viewed_product'] = array(); } if(!isset($_SESSION['viewed_product'][$productId])){ $_SESSION['viewed_product'][$productId] = time(); } } ?>

Chúng ta cần lấy id của product cho vào mảng để phục vục cho truy vấn

 $value): array_push($list_id_product,$key ); endforeach; endif; ?>

Cuối cùng là câu lên WP_Query

 'product', 'post__in' => $list_id_product, 'posts_per_page' =>-1, 'post_status' => 'publish' ); $wp_query = new WP_Query( $args ); if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?> 

BÌNH LUẬN

Lời bình của bạn
Question   Razz  Sad   Evil  Exclaim  Smile  Redface  Biggrin  Surprised  Eek   Confused   Cool  LOL   Mad   Twisted  Rolleyes   Wink  Idea  Arrow  Neutral  Cry   Mr. Green

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

Register

Newsletter

Email this story
Share This Story:
Print this story
Email this story
Digg
Reddit
StumbleUpon
Share on Tumblr
GET ALERTS:

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.