| @@ -5,9 +5,8 @@ | ||
| 5 | 5 | use FluentCart\Api\ModuleSettings; |
| 6 | 6 | use FluentCart\App\Helpers\Helper; |
| 7 | 7 | use FluentCart\App\Models\Product; |
| 8 | 8 | use FluentCart\App\Modules\Templating\AssetLoader; |
| 9 | -use FluentCart\App\Vite; | |
| 10 | 9 | use FluentCart\Framework\Support\Arr; |
| 11 | 10 | |
| 12 | 11 | class ProductCardRender |
| 13 | 12 | { |
| @@ -86,8 +85,9 @@ | ||
| 86 | 85 | __('%s product card', 'fluent-cart'), $this->product->post_title)); |
| 87 | 86 | ?>"> |
| 88 | 87 | <?php $this->renderProductImage(); ?> |
| 89 | 88 | <?php $this->renderTitle(); ?> |
| 89 | + <?php $this->renderStarRating(); ?> | |
| 90 | 90 | <?php if (!Arr::get($this->config, 'hide_excerpt', false)) { $this->renderExcerpt(); } ?> |
| 91 | 91 | <?php $this->renderPrices(); ?> |
| 92 | 92 | <?php $this->showBuyButton(); ?> |
| 93 | 93 | </article> |
| @@ -134,8 +134,96 @@ | ||
| 134 | 134 | $defaultVariant |
| 135 | 135 | ); |
| 136 | 136 | } |
| 137 | 137 | |
| 138 | + public function renderStarRating() | |
| 139 | + { | |
| 140 | + if (!ModuleSettings::isActive('reviews')) { | |
| 141 | + return; | |
| 142 | + } | |
| 143 | + | |
| 144 | + // Store-level toggles: Settings → Store Settings → Product Page → | |
| 145 | + // Product Rating. Shop grid/carousels gate on show_rating_in_shop; | |
| 146 | + // relevant/related product sections gate on show_rating_in_relevant | |
| 147 | + // (callers tag those cards with config rating_context = 'relevant'). | |
| 148 | + // The explicit product-rating block (renderStarRatingBlock) stays | |
| 149 | + // unaffected — it is user-placed. | |
| 150 | + $ratingContext = Arr::get($this->config, 'rating_context', 'shop'); | |
| 151 | + | |
| 152 | + $ratingVisibilitySettingKey = $ratingContext === 'relevant' | |
| 153 | + ? 'show_rating_in_relevant' | |
| 154 | + : 'show_rating_in_shop'; | |
| 155 | + | |
| 156 | + if ((new \FluentCart\Api\StoreSettings())->get($ratingVisibilitySettingKey, 'yes') !== 'yes') { | |
| 157 | + return; | |
| 158 | + } | |
| 159 | + | |
| 160 | + $otherInfo = $this->product->detail->other_info ?: []; | |
| 161 | + $avgRating = Arr::get($otherInfo, 'average_rating', 0); | |
| 162 | + $reviewCount = (int) Arr::get($otherInfo, 'review_count', 0); | |
| 163 | + | |
| 164 | + if ($reviewCount < 1) { | |
| 165 | + return; | |
| 166 | + } | |
| 167 | + | |
| 168 | + $fullStars = (int) floor($avgRating); | |
| 169 | + $halfStar = ($avgRating - $fullStars) >= 0.5; | |
| 170 | + $emptyStars = 5 - $fullStars - ($halfStar ? 1 : 0); | |
| 171 | + | |
| 172 | + ?> | |
| 173 | + <div class="fct-product-card-rating" role="img" aria-label="<?php echo esc_attr(sprintf( | |
| 174 | + /* translators: %1$s: average rating, %2$d: review count */ | |
| 175 | + __('Rated %1$s out of 5 based on %2$d reviews', 'fluent-cart'), $avgRating, $reviewCount)); ?>"> | |
| 176 | + <span class="fct-product-card-stars" aria-hidden="true"> | |
| 177 | + <?php | |
| 178 | + for ($i = 0; $i < $fullStars; $i++) { | |
| 179 | + echo '<span class="fct-star fct-star-filled">★</span>'; | |
| 180 | + } | |
| 181 | + if ($halfStar) { | |
| 182 | + echo '<span class="fct-star fct-star-half"><span class="fct-star-half-empty">★</span><span class="fct-star-half-fill">★</span></span>'; | |
| 183 | + } | |
| 184 | + for ($i = 0; $i < $emptyStars; $i++) { | |
| 185 | + echo '<span class="fct-star fct-star-empty">★</span>'; | |
| 186 | + } | |
| 187 | + ?> | |
| 188 | + </span> | |
| 189 | + <span class="fct-product-card-review-count">(<?php echo esc_html($reviewCount); ?>)</span> | |
| 190 | + </div> | |
| 191 | + <?php | |
| 192 | + } | |
| 193 | + | |
| 194 | + public function renderStarRatingBlock($wrapperAttributes = '') | |
| 195 | + { | |
| 196 | + $otherInfo = $this->product->detail->other_info ?: []; | |
| 197 | + $avgRating = Arr::get($otherInfo, 'average_rating', 0); | |
| 198 | + $reviewCount = (int) Arr::get($otherInfo, 'review_count', 0); | |
| 199 | + | |
| 200 | + $fullStars = (int) floor($avgRating); | |
| 201 | + $halfStar = ($avgRating - $fullStars) >= 0.5; | |
| 202 | + $emptyStars = 5 - $fullStars - ($halfStar ? 1 : 0); | |
| 203 | + | |
| 204 | + ?> | |
| 205 | + <div <?php echo $wrapperAttributes; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> role="img" aria-label="<?php echo esc_attr(sprintf( | |
| 206 | + /* translators: %1$s: average rating, %2$d: review count */ | |
| 207 | + __('Rated %1$s out of 5 based on %2$d reviews', 'fluent-cart'), $avgRating, $reviewCount)); ?>"> | |
| 208 | + <span class="fct-product-card-stars" aria-hidden="true"> | |
| 209 | + <?php | |
| 210 | + for ($i = 0; $i < $fullStars; $i++) { | |
| 211 | + echo '<span class="fct-star fct-star-filled">★</span>'; | |
| 212 | + } | |
| 213 | + if ($halfStar) { | |
| 214 | + echo '<span class="fct-star fct-star-half"><span class="fct-star-half-empty">★</span><span class="fct-star-half-fill">★</span></span>'; | |
| 215 | + } | |
| 216 | + for ($i = 0; $i < $emptyStars; $i++) { | |
| 217 | + echo '<span class="fct-star fct-star-empty">★</span>'; | |
| 218 | + } | |
| 219 | + ?> | |
| 220 | + </span> | |
| 221 | + <span class="fct-product-card-review-count">(<?php echo esc_html($reviewCount); ?>)</span> | |
| 222 | + </div> | |
| 223 | + <?php | |
| 224 | + } | |
| 225 | + | |
| 138 | 226 | public function renderExcerpt($atts = '') |
| 139 | 227 | { |
| 140 | 228 | if (empty($this->product->post_excerpt)) { |
| 141 | 229 | return; |
| @@ -210,13 +298,16 @@ | ||
| 210 | 298 | if (!RenderGate::shouldRender('image', RenderGate::context($this->product, RenderGate::SCOPE_CARD))) { |
| 211 | 299 | return; |
| 212 | 300 | } |
| 213 | 301 | |
| 214 | - $image = $this->product->thumbnail; | |
| 302 | + // Product::$thumbnail already falls back to the placeholder, which | |
| 303 | + // hides whether this is one, so ask for the featured image directly. | |
| 304 | + $detail = $this->product->detail; | |
| 305 | + $image = $detail ? Arr::get((array) $detail->featured_media, 'url') : ''; | |
| 215 | 306 | $isPlaceholder = false; |
| 216 | 307 | |
| 217 | 308 | if (!$image) { |
| 218 | - $image = Vite::getAssetUrl('images/placeholder.svg'); | |
| 309 | + $image = Helper::getProductPlaceholderUrl(); | |
| 219 | 310 | $isPlaceholder = true; |
| 220 | 311 | } |
| 221 | 312 | |
| 222 | 313 | $altText = $isPlaceholder |
| @@ -229,9 +320,9 @@ | ||
| 229 | 320 | 'product' => $this->product, |
| 230 | 321 | 'scope' => 'product_card' |
| 231 | 322 | ])); |
| 232 | 323 | ?> |
| 233 | - <a class="fct-product-card-image-wrap" | |
| 324 | + <a class="fct-product-card-image-wrap<?php echo $isPlaceholder ? ' is-placeholder' : ''; ?>" | |
| 234 | 325 | href="<?php echo esc_url($this->viewUrl); ?>" |
| 235 | 326 | style="display: block;" |
| 236 | 327 | aria-label="<?php echo esc_attr(sprintf( |
| 237 | 328 | /* translators: %s: product title */ |