HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: /var/www/html/CW-techs/wp-content/themes/cw-techs/page-favorites.php
<?php
/* Template Name: Favorites */
get_header();

if (!is_user_logged_in()) {
    echo '<p>Please <a href="/login">login</a> to view your favorites.</p>';
    get_footer();
    exit;
}

$user_id = get_current_user_id();
$favorites = get_user_meta($user_id, 'favorite_products', true);
$favorites = is_array($favorites) ? $favorites : [];
$fav_count = count($favorites);
?>

<div class="favourites-outer">
    <div class="container custom-container">
        <div class="cart-header">
            <a href="<?php echo home_url(); ?>" class="link-text"><img src="<?php echo get_template_directory_uri(); ?>/img/arrow1s.svg"> Back to Home</a>
            <h2 class="cart-title">My <span>Favorites</span></h2>
            <div class="text-sec"><span><?php echo intval($fav_count); ?></span> items in your cart</div>
        </div>
    </div>
</div>

<section class="product-listing product-listing-featured favrt-list px-common">
    <div class="container custom-container">
        <div class="listing-products">

            <?php
            if (!empty($favorites)) {
                $args = array(
                    'post_type' => 'product',
                    'post__in' => $favorites,
                    'posts_per_page' => -1
                );
                $loop = new WP_Query($args);

                if ($loop->have_posts()) :
                    while ($loop->have_posts()) : $loop->the_post();
                        global $product;
                        $product_id = $product->get_id();
                        $product_title = $product->get_name();
                        $product_price = $product->get_price();
                        $product_regular_price = $product->get_regular_price();
                        $product_sale_price = $product->get_sale_price();
                        $discount_percentage = ($product_regular_price > 0 && $product_sale_price > 0) ? round( ( ($product_regular_price - $product_sale_price) / $product_regular_price ) * 100 ) : 0;
                        $product_img_url = wp_get_attachment_image_url($product->get_image_id(), 'medium');
            ?>

                <a class="listing-blks" href="<?php the_permalink(); ?>">
                    <div class="products-wrp">
                        <?php if ($discount_percentage > 0): ?>
                        <div class="off-label">
                            <span class="icon"><img src="<?php echo get_template_directory_uri(); ?>/img/discount-icon.svg" alt="discount"></span>
                            <span class="txt"><?php echo $discount_percentage; ?>% OFF</span>
                        </div>
                        <?php endif; ?>

                        <div class="fav-list">
                            <img src="<?php echo get_template_directory_uri(); ?>/img/fav-list-selected.svg" alt="">
                        </div>
                        <div class="prod-img">
                            <img src="<?php echo esc_url($product_img_url); ?>" alt="<?php echo esc_attr($product_title); ?>">
                        </div>
                    </div>
                    <div class="product-desc">
                        <div class="prod-title"><?php echo esc_html($product_title); ?></div>
                        <div class="price-blk">
                            <span class="price">₹<?php echo wc_price($product_price, ['currency' => '', 'decimal_separator' => '', 'thousand_separator' => '', 'decimals' => 0]); ?></span>
                            <?php if ($product_regular_price > $product_price): ?>
                            <span class="price old">₹<?php echo wc_price($product_regular_price, ['currency' => '', 'decimal_separator' => '', 'thousand_separator' => '', 'decimals' => 0]); ?></span>
                            <?php endif; ?>
                        </div>
                        <div class="bottom-blk">
                            <?php if ($discount_percentage > 0): ?>
                            <div class="save-tag">Save - <?php echo $discount_percentage; ?>%</div>
                            <?php endif; ?>
                            <div class="rating-wrp">
                                <div class="rating">
                                    <?php
                                    $rating = round( $product->get_average_rating() ); // 0 to 5
                                    $star_img = get_template_directory_uri() . '/img/rating-star.svg';
                                    $empty_star_img = get_template_directory_uri() . '/img/rating-star.svg'; // same image, but faded via CSS

                                    for ($i = 1; $i <= 5; $i++) {
                                        $is_active = $i <= $rating ? 'active' : '';
                                        echo '<button class="rating-btn ' . $is_active . '">
                                                <img src="' . esc_url($star_img) . '" alt="star">
                                            </button>';
                                    }
                                    ?>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="move-to-cart">
                        <button class="btn move-to-cart-btn" data-product-id="<?php echo $product_id; ?>">
                            <span class="ico"><img src="<?php echo get_template_directory_uri(); ?>/img/cart1.svg"></span>
                            <span>Move to Cart</span>
                        </button>
                        <button class="btn move-to-cart1 remove-favorite" data-product-id="<?php echo $product_id; ?>">
                            <span class="ico"><img src="<?php echo get_template_directory_uri(); ?>/img/favorites1.svg" alt="favorite"></span>
                            <span>Remove favorite</span>
                        </button>
                    </div>
                </a>

            <?php
                    endwhile;
                else :
                     echo '<section class="no-data">
                                <div class="no-data-img"><img src="' . get_stylesheet_directory_uri() . '/img/no-data-img.svg" alt="no-data"></div>
                                <p class="no-msg">No favorite products found.</p>
                            </section>';
                endif;
                wp_reset_postdata();
            } else {
                 echo '<section class="no-data">
                            <div class="no-data-img"><img src="' . get_stylesheet_directory_uri() . '/img/no-data-img.svg" alt="no-data"></div>
                            <p class="no-msg">No favorite products added yet..</p>
                        </section>';
            }
            ?>

        </div>
    </div>
</section>

<?php get_footer(); ?>