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/woocommerce/single-product.php
<?php
defined('ABSPATH') || exit;
get_header(); // Loads header.php
global $product;

// Ensure $product is a WC_Product object
if (!is_a($product, 'WC_Product')) {
    $product = wc_get_product(get_the_ID());
}

$gallery = $product->get_gallery_image_ids();
?>

<div class="common-outer">
    <section class="product-details-sec px-common">
        <div class="container container-1400">
            <div class="product-details-wrapper d-flex">
                <div class="details-left-block">
                    <div class="product-img-cover">
                        <div class="product-lg-slider">
                            <!-- Main Product Image -->
                            <div class="prod-lg-item">
                                <div class="prod-img-cover">
                                    <?php echo get_the_post_thumbnail($product->get_id(), 'large'); ?>
                                </div>
                            </div>
                            <!-- Gallery Images -->
                            <?php foreach ($gallery as $img_id): ?>
                            <div class="prod-lg-item">
                                <div class="prod-img-cover">
                                    <?php echo wp_get_attachment_image($img_id, 'large'); ?>
                                </div>
                            </div>
                            <?php endforeach; ?>
                        </div>

                        <div class="product-thumb-slider">
                            <!-- Main thumbnail -->
                            <div class="thumb-item">
                                <div class="thumb-img-block">
                                    <?php echo get_the_post_thumbnail($product->get_id(), 'thumbnail'); ?>
                                </div>
                            </div>
                            <!-- Thumbnails -->
                            <?php foreach ($gallery as $img_id): ?>
                            <div class="thumb-item">
                                <div class="thumb-img-block">
                                    <?php echo wp_get_attachment_image($img_id, 'thumbnail'); ?>
                                </div>
                            </div>
                            <?php endforeach; ?>
                        </div>
                    </div>
                </div>

                <div class="details-right-block">
                    <div class="det-block-top">
                        <div class="top-block-1">
                            <?php
                            $rating = floatval( $product->get_average_rating() ); // e.g., 4.5
                            $full_stars = floor( $rating ); // 4
                            $half_star = ( $rating - $full_stars ) >= 0.5 ? true : false;
                            $empty_stars = 5 - $full_stars - ( $half_star ? 1 : 0 );
                            $review_count = $product->get_review_count(); // For WooCommerce products
                            ?>

                            <div class="rating-block">
                                <?php
                                // Full orange stars
                                for ( $i = 0; $i < $full_stars; $i++ ) {
                                    echo '<img class="str" src="' . get_template_directory_uri() . '/img/str-orng.svg" alt="star">';
                                }

                                // Half star (optional: use a separate icon like str-half.svg)
                                if ( $half_star ) {
                                    echo '<img class="str" src="' . get_template_directory_uri() . '/img/str-half.svg" alt="half-star">';
                                }

                                // Gray (empty) stars
                                for ( $i = 0; $i < $empty_stars; $i++ ) {
                                    echo '<img class="str" src="' . get_template_directory_uri() . '/img/str-gray.svg" alt="gray-star">';
                                }
                                ?>

                                <span class="rating-num"><?php echo number_format( $rating, 1 ); ?></span>
                                <?php if ( isset($review_count) && $review_count > 0 ) : ?>
                                    <span class="rating-num">(<?php echo intval($review_count); ?> reviews)</span>
                                <?php endif; ?>
                            </div>


                            <h2 class="prod-name"><?php the_title(); ?></h2>

                            <div class="brand-det-wrapper">
                                <div class="brand-det-col">
                                    <div class="brand-det-card">
                                        <span class="data-sm-label">SKU :</span>
                                        <span class="data-sm-val"><?php echo $product->get_sku(); ?></span>
                                    </div>
                                    <div class="brand-det-card">
                                        <span class="data-sm-label">ID :</span>
                                        <span class="data-sm-val">#<?php echo $product->get_id(); ?></span>
                                    </div>
                                </div>
                                <div class="brand-det-col">
                                    <div class="brand-det-card">
                                        <span class="data-sm-label">Availability :</span>
                                        <span class="data-sm-val val-grn">
                                            <?php echo $product->is_in_stock() ? 'In Stock' : 'Out of Stock'; ?>
                                        </span>
                                    </div>
                                    <div class="brand-det-card">
                                        <span class="data-sm-label">Categories :</span>
                                        <span class="data-sm-val">
                                            <?php echo wc_get_product_category_list($product->get_id()); ?>
                                        </span>
                                    </div>
                                </div>
                            </div>

                            <p class="special-price-label">Special Offer Price</p>
                            <div class="price-det">
                                <?php
                                $regular_price = (float) $product->get_regular_price();
                                $sale_price    = (float) $product->get_sale_price();
                                $discount      = $regular_price > 0 && $sale_price ? round( ( ($regular_price - $sale_price) / $regular_price ) * 100 ) : 0;
                                ?>

                                <?php if ( $sale_price && $sale_price < $regular_price ) : ?>
                                    <span class="price"><?php echo wc_price($sale_price); ?></span>
                                    <span class="prod-price"><?php echo wc_price($regular_price); ?></span>
                                    <span class="off-data">
                                        <img src="<?php echo get_template_directory_uri(); ?>/img/offr.svg" alt=""> <?php echo $discount; ?>% OFF
                                    </span>
                                <?php else : ?>
                                    <span class="prod-price"><?php echo wc_price($product->get_price()); ?></span>
                                <?php endif; ?>
                            </div>

                        </div>

                        <div class="cart-btn-block">
                            <!-- <button class="cart-btn"><img src="<?php echo get_template_directory_uri(); ?>/img/cart-det.svg" alt=""> Add To Cart</button>
                            <a href="#" class="cart-btn buy-now-btn"><img src="<?php echo get_template_directory_uri(); ?>/img/buy-now-lgt.svg" alt=""> Buy Now</a> -->
                            <form class="cart" method="post" enctype="multipart/form-data" action="<?php echo esc_url( apply_filters( 'woocommerce_add_to_cart_form_action', $product->get_permalink() ) ); ?>">
                                <?php
                                do_action( 'woocommerce_before_add_to_cart_button' );

                                // woocommerce_quantity_input( array(
                                //     'min_value'   => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
                                //     'max_value'   => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
                                //     'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( wp_unslash( $_POST['quantity'] ) ) : 1,
                                // ) );
                                ?>
                                <input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>" />
                                <button type="submit" class="single_add_to_cart_button cart-btn alt">
                                    <img src="<?php echo get_template_directory_uri(); ?>/img/cart-det.svg" alt=""> Add to Cart
                                </button>

                                <?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
                            </form>
                            <form method="post" action="<?php echo esc_url( wc_get_checkout_url() ); ?>">
                            <input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>">
                            <input type="hidden" name="quantity" value="1">
                            <button type="submit" class="cart-btn buy-now-btn">
                                <img src="<?php echo get_template_directory_uri(); ?>/img/buy-now-lgt.svg" alt=""> Buy Now
                            </button>
                        </form>


                        </div>
                    </div>

                   <?php
                        global $post;

                        // Get all features
                        $features = [];
                        for ($i = 1; $i <= 10; $i++) {
                            $value = get_post_meta($post->ID, 'feature_' . $i, true);
                            if (!empty($value)) {
                                $features[] = $value;
                            }
                        }

                        // Get shipping data
                        $courier = get_post_meta($post->ID, 'courier_shipping', true);
                        $ups = get_post_meta($post->ID, 'ups_shipping', true);
                        ?>

                        <?php if (!empty($features) || !empty($courier) || !empty($ups)) : ?>
                            <div class="features-sec d-flex">
                                <?php if (!empty($features)) : ?>
                                    <div class="features-block">
                                        <h4 class="block-title-sm">Features</h4>
                                        <?php
                                        $icon_index = 1;
                                        foreach ($features as $feature) :
                                            $icon_path = get_template_directory_uri() . "/img/fet-$icon_index.svg";
                                            ?>
                                            <div class="feat-item">
                                                <span class="icon">
                                                    <img src="<?php echo esc_url($icon_path); ?>" alt="">
                                                </span>
                                                <?php echo esc_html($feature); ?>
                                            </div>
                                            <?php $icon_index++; ?>
                                        <?php endforeach; ?>
                                    </div>
                                <?php endif; ?>

                                <?php if (!empty($courier) || !empty($ups)) : ?>
                                    <div class="features-block shipping-block">
                                        <h4 class="block-title-sm">Shipping Information</h4>
                                        <?php if (!empty($courier)) : ?>
                                            <div class="feat-item">Courier: <span class="txt-off"><?php echo esc_html($courier); ?></span></div>
                                        <?php endif; ?>
                                        <?php if (!empty($ups)) : ?>
                                            <div class="feat-item">UPS: <span class="txt-off"><?php echo esc_html($ups); ?></span></div>
                                        <?php endif; ?>
                                    </div>
                                <?php endif; ?>
                            </div>
                        <?php endif; ?>


                    <div class="details-btm-block">
                        <h4 class="block-title-sm">Details</h4>
                        <?php the_content(); ?>
                    </div>
                    <div>
                        <?php if ( is_user_logged_in() ) : ?>
                            <form method="post" id="custom-review-form" class="custom-review-form" data-parsley-validate>
                                <h4>Write a Review</h4>
                                <div class="form-grp">
                                    <label for="rating">Your Rating <span class="star">*</span></label>
                                    <select name="rating" id="rating_cust" required data-parsley-required-message="Select your rating.">
                                        <option value="" disabled selected>-- Select your rating --</option>
                                        <option value="5">★★★★★ – Excellent</option>
                                        <option value="4">★★★★☆ – Good</option>
                                        <option value="3">★★★☆☆ – Average</option>
                                        <option value="2">★★☆☆☆ – Poor</option>
                                        <option value="1">★☆☆☆☆ – Terrible</option>
                                    </select>
                                </div>
                                <div class="form-grp">
                                    <label for="review">Your Review <span class="star">*</span></label>
                                    <textarea name="review" id="review" rows="5" required 
                                            data-parsley-minlength="10"
                                            data-parsley-required-message="Your review is required."
                                            data-parsley-minlength-message="Review must be at least 10 characters long." placeholder="Enter your review here"></textarea>
                                </div>
                                <input type="hidden" name="product_id" value="<?php echo esc_attr(get_the_ID()); ?>">
                                <button type="submit" id="review-btn">Submit Review</button>
                            </form>
                        <?php else : ?>
                            <div class="noreview">
                                <p>You must <a href="<?php echo esc_url(wp_login_url()); ?>">log in</a> to leave a review.</p>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </section>
<?php include get_template_directory() . '/templates/above-footer.php'; ?>
</div>
<?php
get_footer(); // Loads footer.php
?>