Recently Viewed Products hiển thị sản phẩm đã xem trong Woocomerce

Mình hay lấy code của bọn bloomer này rất hay để lại nguyên nguồn cho anh em nào thích tìm hiểu thì vào xem thêm nhé.

1. Tạo shortcode Recently Viewed Products

Cái này cũng dễ hiểu anh em có thể đọc hiểu nhé 1 cái function lấy list ID theo cookie còn 1 function là hiển thị cái list ID đấy thôi.

/**
 * @snippet       [recently_viewed_products] Shortcode - WooCommerce
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 8
 * @community     https://businessbloomer.com/club/
 */
 
add_action( 'template_redirect', 'bbloomer_track_product_view', 9999 );
 
function bbloomer_track_product_view() {
   if ( ! is_singular( 'product' ) ) return;
   global $post;
   if ( empty( $_COOKIE['bbloomer_recently_viewed'] ) ) {
      $viewed_products = array();
   } else {
      $viewed_products = wp_parse_id_list( (array) explode( '|', wp_unslash( $_COOKIE['bbloomer_recently_viewed'] ) ) );
   }
   $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 );
   }
   wc_setcookie( 'bbloomer_recently_viewed', implode( '|', $viewed_products ) );
}
 
add_shortcode( 'recently_viewed_products', 'bbloomer_recently_viewed_shortcode' );
  
function bbloomer_recently_viewed_shortcode() {
   $viewed_products = ! empty( $_COOKIE['bbloomer_recently_viewed'] ) ? (array) explode( '|', wp_unslash( $_COOKIE['bbloomer_recently_viewed'] ) ) : array();
   $viewed_products = array_reverse( array_filter( array_map( 'absint', $viewed_products ) ) );
   if ( empty( $viewed_products ) ) return;
   $title = '<div class="row sanphamdaxem"><h3>/ SẢN PHẨM ĐÃ XEM /</h3></div>';
   $product_ids = implode( ",", $viewed_products );
   return $title . do_shortcode("[products ids='$product_ids']");
}

2. Sử dụng shortcode

Sau khi có quả shortcode thần thánh rồi thì tìm đúng vị trí hook mà dí vào thôi là xong.

function ux_sanphamdaxem() {
    echo do_shortcode('[recently_viewed_products]');
}
add_action('flatsome_after_product_page', 'ux_sanphamdaxem');

Ở đây mình dí nó vào hook flatsome_after_product_page tận hưởng thành quả anh em nhé.