/*
Theme Name: Flatsome Child
Description: This is a child theme for Flatsome Theme
Author: UX Themes
Template: flatsome
Version: 3.0
*/

/*************** ADD CUSTOM CSS HERE.   ***************/


@media only screen and (max-width: 48em) {
/*************** ADD MOBILE ONLY CSS HERE  ***************/


/* Khối checkbox chính sách */
.kn-policy-wrap {
  margin: 14px 0 18px;
  width: 100%;
}

.kn-policy-label {
  display: flex !important;
  align-items: flex-start !important;
  gap: 10px !important;
  width: 100% !important;
  line-height: 1.7 !important;
  font-size: 14px !important;
  color: #333 !important;
  white-space: normal !important;
}

.kn-policy-label input[type="checkbox"] {
  margin: 4px 0 0 0 !important;
  flex: 0 0 16px !important;
  width: 16px !important;
  height: 16px !important;
}

.kn-policy-label span {
  display: block !important;
  flex: 1 1 auto !important;
  min-width: 0 !important;
  white-space: normal !important;
  word-break: normal !important;
}

.kn-policy-label a {
  display: inline !important;
  width: auto !important;
  max-width: none !important;
  margin: 0 !important;
  padding: 0 !important;
  color: #4f6f1f !important;
  text-decoration: none !important;
  font-weight: 500 !important;
  white-space: normal !important;
}
.woocommerce-cart .cart_totals .shipping-destination {
  display: none !important;
}

	/* Cho khối text của card thành flex dọc */
.woocommerce ul.products li.product .box-text,
.woocommerce-page ul.products li.product .box-text,
.product-small .box-text {
    display: flex;
    flex-direction: column;
    height: 100%;
}

/* Card sản phẩm */
.woocommerce ul.products li.product,
.woocommerce-page ul.products li.product {
    display: flex;
    flex-direction: column;
}

/* Khối text của Flatsome */
.woocommerce ul.products li.product .box-text,
.product-small .box-text {
    display: flex;
    flex-direction: column;
    flex: 1;
}

/* Tên sản phẩm đều chiều cao hơn */
.woocommerce ul.products li.product .name,
.woocommerce ul.products li.product .woocommerce-loop-product__title,
.product-small .name {
    min-height: 56px;
}

/* Block cuối card */
.cocoon-product-bottom {
    margin-top: auto;
    padding-top: 10px;
}

/* 1 hàng: sao + số đánh giá + đã bán */
.cocoon-loop-meta {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
    margin-bottom: 12px;
}

.cocoon-rating-inline {
    display: flex;
    align-items: center;
    gap: 6px;
}

.cocoon-rating-inline .star-rating,
.woocommerce .cocoon-rating-inline .star-rating {
    margin: 0 !important;
    float: none !important;
    display: inline-block !important;
    font-size: 14px;
    line-height: 1;
}

.woocommerce .cocoon-rating-inline .star-rating::before {
    color: #ddd;
}

.woocommerce .cocoon-rating-inline .star-rating span::before {
    color: #d67a45;
}

.cocoon-rating-inline .rating-count,
.cocoon-sold-inline {
    font-size: 14px;
    color: #666;
    line-height: 1;
    margin: 0;
}



<?php
/* Ẩn rating mặc định để khỏi bị lặp */
remove_action('woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5);

/* Hiển thị meta tự động: sao, số review, đã bán, tồn kho */
add_action('woocommerce_after_shop_loop_item_title', 'cocoon_auto_product_meta', 20);
function cocoon_auto_product_meta() {
    global $product;

    if (!$product || !is_a($product, 'WC_Product')) {
        return;
    }

    $rating       = $product->get_average_rating();   // điểm trung bình
    $review_count = $product->get_review_count();     // số người đánh giá
    $sold_count   = $product->get_total_sales();      // số đã bán

    // Tồn kho
    $stock_qty = $product->get_stock_quantity();
    $manage_stock = $product->managing_stock();
    $stock_status = $product->get_stock_status();

    echo '<div class="cocoon-card-meta-wrap">';

        echo '<div class="cocoon-card-meta-row">';

            // Rating thật
            echo '<div class="cocoon-rating-box">';
                if ($review_count > 0 && $rating > 0) {
                    echo '<span class="cocoon-rating-badge">' . esc_html($rating) . '★</span>';
                    echo '<span class="cocoon-review-count">(' . intval($review_count) . ')</span>';
                } else {
                    echo '<span class="cocoon-rating-badge">0★</span>';
                    echo '<span class="cocoon-review-count">(0)</span>';
                }
            echo '</div>';

            // Đã bán thật
            echo '<div class="cocoon-sold-box">';
                echo '<span class="cocoon-cart-icon">🛒</span>';
                echo '<span class="cocoon-sold-count">' . intval($sold_count) . '</span>';
            echo '</div>';

        echo '</div>';

        // Tồn kho thật
        echo '<div class="cocoon-stock-row">';
            if ($stock_status === 'outofstock') {
                echo '<span class="cocoon-stock out">Hết hàng</span>';
            } elseif ($manage_stock && $stock_qty !== null) {
                echo '<span class="cocoon-stock in">Còn lại ' . intval($stock_qty) . ' sản phẩm</span>';
            } else {
                echo '<span class="cocoon-stock in">Còn hàng</span>';
            }
        echo '</div>';

        // Nút thêm vào giỏ hàng
        echo '<div class="cocoon-add-to-cart-row">';
            woocommerce_template_loop_add_to_cart();
        echo '</div>';

    echo '</div>';
}
	

