| @@ -13,8 +13,9 @@ | ||
| 13 | 13 | use FluentCart\App\Http\Routes\WebRoutes; |
| 14 | 14 | use FluentCart\App\Models\ProductVariation; |
| 15 | 15 | use FluentCart\Framework\Support\Collection; |
| 16 | 16 | use FluentCart\App\Modules\Templating\AssetLoader; |
| 17 | +use FluentCart\App\Modules\FluentPlayer\ProductVideoRenderer; | |
| 17 | 18 | |
| 18 | 19 | class ProductRenderer |
| 19 | 20 | { |
| 20 | 21 | protected $product; |
| @@ -38,8 +39,10 @@ | ||
| 38 | 39 | protected $defaultGalleryImageId = 0; |
| 39 | 40 | |
| 40 | 41 | protected $galleryActiveSet = false; |
| 41 | 42 | |
| 43 | + protected $productVideoRenderer = null; | |
| 44 | + | |
| 42 | 45 | protected $paymentTypes = []; |
| 43 | 46 | |
| 44 | 47 | protected $variantsByPaymentTypes = []; |
| 45 | 48 | |
| @@ -46,8 +49,10 @@ | ||
| 46 | 49 | protected $activeTab = 'onetime'; |
| 47 | 50 | |
| 48 | 51 | protected $images = []; |
| 49 | 52 | |
| 53 | + protected $variantTermMap = []; | |
| 54 | + | |
| 50 | 55 | protected $defaultImageUrl = null; |
| 51 | 56 | |
| 52 | 57 | protected $defaultImageAlt = null; |
| 53 | 58 | |
| @@ -83,10 +88,41 @@ | ||
| 83 | 88 | if (!$defaultVariationId) { |
| 84 | 89 | $variationIds = $product->variants->pluck('id')->toArray(); |
| 85 | 90 | $defaultVariationId = $product->detail->default_variation_id; |
| 86 | 91 | |
| 87 | - if (!$defaultVariationId || !in_array($defaultVariationId, $variationIds)) { | |
| 88 | - $defaultVariationId = Arr::get($variationIds, '0'); | |
| 92 | + // For advanced variations the storefront selector only exposes ACTIVE | |
| 93 | + // variants (AdvancedVariationRenderer skips item_status != active), so | |
| 94 | + // resolve the default against the same set. default_variation_id is | |
| 95 | + // maintained stock/status-agnostically, so it can legitimately point | |
| 96 | + // at an inactive variant — accepting it here would render the inactive | |
| 97 | + // default's price/stock/button while the selector omits that id and | |
| 98 | + // falls back to a different active variant. Scoped to advanced so | |
| 99 | + // simple / simple_variations keep their existing default handling. | |
| 100 | + $isAdvanced = $product->detail | |
| 101 | + && $product->detail->variation_type === Helper::PRODUCT_TYPE_ADVANCE_VARIATION; | |
| 102 | + $eligibleVariants = $isAdvanced | |
| 103 | + ? $product->variants->where('item_status', 'active') | |
| 104 | + : $product->variants; | |
| 105 | + $eligibleIds = $eligibleVariants->pluck('id')->toArray(); | |
| 106 | + | |
| 107 | + if (!$defaultVariationId || !in_array($defaultVariationId, $eligibleIds)) { | |
| 108 | + // No valid stored default — fall back to the FIRST eligible | |
| 109 | + // combination by serial_index (active-only for advanced), not the | |
| 110 | + // first by DB/id order, so the server-rendered price/stock/button | |
| 111 | + // match what the storefront selector highlights. Stock is ignored | |
| 112 | + // — first by order wins. A NULL serial_index (legacy/malformed row | |
| 113 | + // the merchant never ordered) is pushed AFTER numbered variants so | |
| 114 | + // it can't become the default ahead of an explicitly ordered one — | |
| 115 | + // PHP's default null-first sort would otherwise promote it, and the | |
| 116 | + // frontend mirrors this by treating null serial as last too. | |
| 117 | + $firstBySerial = $eligibleVariants | |
| 118 | + ->sortBy(function ($variant) { | |
| 119 | + return is_null($variant->serial_index) | |
| 120 | + ? PHP_INT_MAX | |
| 121 | + : (int) $variant->serial_index; | |
| 122 | + }) | |
| 123 | + ->first(); | |
| 124 | + $defaultVariationId = $firstBySerial ? (int) $firstBySerial->id : Arr::get($variationIds, '0'); | |
| 89 | 125 | $hasExplicitDefault = false; |
| 90 | 126 | } |
| 91 | 127 | } |
| 92 | 128 | |
| @@ -104,13 +140,17 @@ | ||
| 104 | 140 | foreach ($this->product->variants as $variant) { |
| 105 | 141 | if ($variant->id == $this->defaultVariationId) { |
| 106 | 142 | $this->defaultVariant = $variant; |
| 107 | 143 | } |
| 108 | - $paymentType = Arr::get($variant->other_info, 'payment_type'); | |
| 109 | - if ($paymentType === 'onetime') { | |
| 144 | + // Read the authoritative payment_type column, not other_info. The | |
| 145 | + // ProductVariation accessor only injects payment_type into other_info | |
| 146 | + // for subscriptions, so one-time variants (notably advanced-variation | |
| 147 | + // combinations generated by Pro) leave other_info['payment_type'] | |
| 148 | + // unset. Anything that is not a subscription is a one-time path. | |
| 149 | + if ($variant->payment_type === 'subscription') { | |
| 150 | + $this->hasSubscription = true; | |
| 151 | + } else { | |
| 110 | 152 | $this->hasOnetime = true; |
| 111 | - } else if ($paymentType === 'subscription') { | |
| 112 | - $this->hasSubscription = true; | |
| 113 | 153 | } |
| 114 | 154 | } |
| 115 | 155 | |
| 116 | 156 | $this->buildProductGroups(); |
| @@ -257,8 +297,33 @@ | ||
| 257 | 297 | } |
| 258 | 298 | |
| 259 | 299 | public function renderBuySection($atts = []) |
| 260 | 300 | { |
| 301 | + $gateContext = RenderGate::context($this->product, RenderGate::SCOPE_SINGLE, $this->defaultVariant); | |
| 302 | + | |
| 303 | + // The buy section is the root the variation-selector JS binds to | |
| 304 | + // (data-fluent-cart-product-pricing-section). Gating it removes the | |
| 305 | + // variation picker along with the buttons, which is what a full catalog | |
| 306 | + // mode wants; to keep shoppers able to browse options while hiding only | |
| 307 | + // the purchase affordances, gate 'actions' instead. | |
| 308 | + if (!RenderGate::shouldRender('buy_section', $gateContext)) { | |
| 309 | + return; | |
| 310 | + } | |
| 311 | + | |
| 312 | + // Render no buy section when there is nothing purchasable — avoids a | |
| 313 | + // broken quantity + "Not Available" block. Two cases: | |
| 314 | + // - no variants at all (any product type), or | |
| 315 | + // - an advanced-variation product with no attribute_config yet (e.g. | |
| 316 | + // switched on before options were configured). Its variants are kept | |
| 317 | + // until generation, so we key on the config, not the variant count, | |
| 318 | + // to hide it until real combinations exist. | |
| 319 | + $isUnconfiguredAdvanced = $this->product->detail->variation_type === Helper::PRODUCT_TYPE_ADVANCE_VARIATION | |
| 320 | + && empty(Arr::get((array) $this->product->detail->other_info, 'attribute_config')); | |
| 321 | + | |
| 322 | + if ($this->product->variants->isEmpty() || $isUnconfiguredAdvanced) { | |
| 323 | + return; | |
| 324 | + } | |
| 325 | + | |
| 261 | 326 | $otherInfo = (array)Arr::get($this->product->detail, 'other_info'); |
| 262 | 327 | $groupBy = Arr::get($otherInfo, 'group_pricing_by', 'repeat_interval'); //repeat_interval,payment_type,none |
| 263 | 328 | |
| 264 | 329 | $this->renderBuySectionWrapperStart(); |
| @@ -277,8 +342,25 @@ | ||
| 277 | 342 | } |
| 278 | 343 | |
| 279 | 344 | public function renderVariationDisplay($atts = []) |
| 280 | 345 | { |
| 346 | + // Hand off rendering to the advanced-variation selector when the | |
| 347 | + // product is configured for advanced variations. The handler returns | |
| 348 | + // the filtered array with rendered=true after emitting its markup; if | |
| 349 | + // no listener handles it (e.g. an unconfigured advanced product), the | |
| 350 | + // filter is a no-op and we fall through to the simple-variation | |
| 351 | + // rendering below. | |
| 352 | + if ($this->product->detail->variation_type === Helper::PRODUCT_TYPE_ADVANCE_VARIATION) { | |
| 353 | + $result = apply_filters('fluent_cart/product/render_advanced_variation', [ | |
| 354 | + 'product' => $this->product, | |
| 355 | + 'selector_style' => Arr::get($atts, 'selector_style', 'auto'), | |
| 356 | + 'rendered' => false, | |
| 357 | + ]); | |
| 358 | + if (!empty($result['rendered'])) { | |
| 359 | + return; | |
| 360 | + } | |
| 361 | + } | |
| 362 | + | |
| 281 | 363 | $otherInfo = (array)Arr::get($this->product->detail, 'other_info'); |
| 282 | 364 | $groupBy = Arr::get($otherInfo, 'group_pricing_by', 'repeat_interval'); //repeat_interval,payment_type,none |
| 283 | 365 | |
| 284 | 366 | if (count($this->paymentTypes) === 1 || $groupBy === 'none') { |
| @@ -293,10 +375,13 @@ | ||
| 293 | 375 | $thumbnails = []; |
| 294 | 376 | |
| 295 | 377 | $featuredMedia = $this->product->thumbnail ?? Vite::getAssetUrl('images/placeholder.svg'); |
| 296 | 378 | |
| 297 | - if (!$featuredMedia) { | |
| 298 | - $featuredMedia = []; | |
| 379 | + // thumbnail can be an empty string (not null), so ?? above doesn't catch | |
| 380 | + // it — fall back to the placeholder so $featuredMedia is always a usable | |
| 381 | + // image URL for both the main <img> and data-default-image-url. | |
| 382 | + if (!$featuredMedia || !\is_string($featuredMedia)) { | |
| 383 | + $featuredMedia = Vite::getAssetUrl('images/placeholder.svg'); | |
| 299 | 384 | } |
| 300 | 385 | |
| 301 | 386 | $galleryImage = get_post_meta($this->product->ID, 'fluent-products-gallery-image', true); |
| 302 | 387 | |
| @@ -329,14 +414,37 @@ | ||
| 329 | 414 | if (isset($images[$imageId])) { |
| 330 | 415 | $imageMetaValue = $images[$imageId]; |
| 331 | 416 | $this->defaultImageUrl = Arr::get($imageMetaValue, 'media.0.url', ''); |
| 332 | 417 | $this->defaultImageAlt = Arr::get($imageMetaValue, 'media.0.title', ''); |
| 418 | + } else { | |
| 419 | + // Fallback to the first available thumbnail. Advanced-variation | |
| 420 | + // products often have per-variant images but no explicit | |
| 421 | + // default_variation_id — the thumbnails are keyed by real | |
| 422 | + // variant IDs while defaultGalleryImageId is 0, so the lookup | |
| 423 | + // above misses and the main image area renders blank with a | |
| 424 | + // broken-image icon. | |
| 425 | + $firstImage = reset($images); | |
| 426 | + $fallbackUrl = Arr::get($firstImage, 'media.0.url', ''); | |
| 427 | + $this->defaultImageUrl = $fallbackUrl ?: ($featuredMedia ?: ''); | |
| 428 | + $this->defaultImageAlt = $this->defaultImageAlt ?: Arr::get($firstImage, 'media.0.title', ''); | |
| 333 | 429 | } |
| 334 | 430 | } |
| 335 | 431 | |
| 432 | + // Nothing set a main image — e.g. a product with no variants, or no | |
| 433 | + // variant/gallery media. Fall back to the featured/placeholder image so | |
| 434 | + // the main area shows the placeholder instead of a broken <img src="">. | |
| 435 | + if (empty($this->defaultImageUrl)) { | |
| 436 | + $this->defaultImageUrl = $featuredMedia; | |
| 437 | + } | |
| 438 | + | |
| 439 | + $videoRenderer = $this->getProductVideoRenderer(); | |
| 440 | + $videoRenderer->showFirstByDefault(!$this->hasGalleryImages()); | |
| 441 | + $defaultVideoId = $videoRenderer->getDefaultMediaId(); | |
| 442 | + | |
| 336 | 443 | ?> |
| 337 | - <div class="fct-product-gallery-thumb" role="region" | |
| 338 | - aria-label="<?php echo esc_attr($this->product->post_title . ' gallery'); ?>"> | |
| 444 | + <div class="fct-product-gallery-thumb<?php echo $defaultVideoId ? ' is-video-active' : ''; ?>" role="region" | |
| 445 | + aria-label="<?php echo esc_attr($this->product->post_title . ' gallery'); ?>" | |
| 446 | + <?php echo $defaultVideoId ? 'data-fct-video-default="' . esc_attr((string) $defaultVideoId) . '"' : ''; ?>> | |
| 339 | 447 | <img |
| 340 | 448 | src="<?php echo esc_url($this->defaultImageUrl ?? '') ?>" |
| 341 | 449 | alt="<?php echo esc_attr($this->defaultImageAlt) ?>" |
| 342 | 450 | data-fluent-cart-single-product-page-product-thumbnail |
| @@ -341,36 +449,117 @@ | ||
| 341 | 449 | alt="<?php echo esc_attr($this->defaultImageAlt) ?>" |
| 342 | 450 | data-fluent-cart-single-product-page-product-thumbnail |
| 343 | 451 | data-default-image-url="<?php echo esc_url($featuredMedia) ?>" |
| 344 | 452 | /> |
| 453 | + <?php $this->getProductVideoRenderer()->renderInlinePlayers(); ?> | |
| 345 | 454 | </div> |
| 346 | 455 | <?php |
| 347 | 456 | } |
| 348 | 457 | |
| 458 | + /** | |
| 459 | + * Whether any gallery / variant image exists to show in the main area. | |
| 460 | + * Only meaningful after renderGalleryThumb() has built $this->images. | |
| 461 | + */ | |
| 462 | + protected function hasGalleryImages(): bool | |
| 463 | + { | |
| 464 | + foreach ($this->images as $image) { | |
| 465 | + foreach ((array) Arr::get($image, 'media', []) as $item) { | |
| 466 | + if (!empty(Arr::get($item, 'url'))) { | |
| 467 | + return true; | |
| 468 | + } | |
| 469 | + } | |
| 470 | + } | |
| 471 | + | |
| 472 | + return false; | |
| 473 | + } | |
| 474 | + | |
| 475 | + public function getProductVideoRenderer(): ProductVideoRenderer | |
| 476 | + { | |
| 477 | + if ($this->productVideoRenderer === null) { | |
| 478 | + $this->productVideoRenderer = new ProductVideoRenderer($this->product); | |
| 479 | + } | |
| 480 | + | |
| 481 | + return $this->productVideoRenderer; | |
| 482 | + } | |
| 483 | + | |
| 349 | 484 | public function renderGalleryThumbControls($maxThumbnails = null) |
| 350 | 485 | { |
| 351 | 486 | $totalThumbImages = Arr::pluck($this->images, 'media.*.url'); |
| 352 | 487 | |
| 353 | - if(count($totalThumbImages) == 1 && is_countable($totalThumbImages[0]) && count($totalThumbImages[0]) == 1){ | |
| 488 | + // A lone image needs no strip, but a video thumb still has to be reachable. | |
| 489 | + if(count($totalThumbImages) == 1 && is_countable($totalThumbImages[0]) && count($totalThumbImages[0]) == 1 && !$this->getProductVideoRenderer()->isAvailable()){ | |
| 354 | 490 | |
| 355 | 491 | return ''; |
| 356 | 492 | } |
| 357 | 493 | |
| 358 | - // Collect ALL gallery images as JSON for lightbox (even when max thumbnails limits visible thumbs) | |
| 494 | + // A lone video without images is already showing in the main area. | |
| 495 | + if (!$this->hasGalleryImages() && count($this->getProductVideoRenderer()->getVideos()) === 1) { | |
| 496 | + return ''; | |
| 497 | + } | |
| 498 | + | |
| 499 | + // Build variantTermMap and variantFirstMediaMap. | |
| 500 | + // For advanced variations, Pro builds both maps via the gallery_variation_data | |
| 501 | + // filter — it has access to AttributeGroup types (color/image) that free cannot | |
| 502 | + // query directly. For all other product types, free builds variantFirstMediaMap | |
| 503 | + // from the already-loaded $this->images; variantTermMap stays empty. | |
| 504 | + $this->variantTermMap = []; | |
| 505 | + $variantFirstMediaMap = []; | |
| 506 | + | |
| 507 | + if ($this->product->detail->variation_type === Helper::PRODUCT_TYPE_ADVANCE_VARIATION) { | |
| 508 | + $galleryVariationData = apply_filters('fluent_cart/product/gallery_variation_data', [ | |
| 509 | + 'variant_term_map' => [], | |
| 510 | + 'variant_first_media_map' => [], | |
| 511 | + ], $this->product); | |
| 512 | + $this->variantTermMap = (array) Arr::get($galleryVariationData, 'variant_term_map', []); | |
| 513 | + $variantFirstMediaMap = (array) Arr::get($galleryVariationData, 'variant_first_media_map', []); | |
| 514 | + } else { | |
| 515 | + foreach ($this->images as $imageId => $image) { | |
| 516 | + if (empty($image['media']) || !is_array($image['media'])) { | |
| 517 | + continue; | |
| 518 | + } | |
| 519 | + $firstItem = $image['media'][0] ?? null; | |
| 520 | + if (!$firstItem) { | |
| 521 | + continue; | |
| 522 | + } | |
| 523 | + $firstUrl = Arr::get($firstItem, 'url', ''); | |
| 524 | + $firstMediaId = (int) Arr::get($firstItem, 'id', 0); | |
| 525 | + if ($firstUrl) { | |
| 526 | + $variantFirstMediaMap[(int) $imageId] = [ | |
| 527 | + 'id' => $firstMediaId, | |
| 528 | + 'url' => $firstUrl, | |
| 529 | + ]; | |
| 530 | + } | |
| 531 | + } | |
| 532 | + } | |
| 533 | + | |
| 534 | + // Collect ALL gallery images as JSON for lightbox (even when max thumbnails limits visible thumbs). | |
| 535 | + // For advanced variations, deduplicate by media ID (when > 0) or by URL (for imported images). | |
| 359 | 536 | $allGalleryImages = []; |
| 537 | + $addedMediaKeys = []; | |
| 538 | + $isAdvVariation = !empty($this->variantTermMap); | |
| 360 | 539 | foreach ($this->images as $imageId => $image) { |
| 361 | 540 | if (empty($image['media']) || !is_array($image['media'])) { |
| 362 | 541 | continue; |
| 363 | 542 | } |
| 364 | 543 | foreach ($image['media'] as $item) { |
| 365 | - $url = Arr::get($item, 'url', ''); | |
| 544 | + $url = Arr::get($item, 'url', ''); | |
| 545 | + $mediaId = (int) Arr::get($item, 'id', 0); | |
| 366 | 546 | if (empty($url)) { |
| 367 | 547 | continue; |
| 368 | 548 | } |
| 549 | + if ($isAdvVariation) { | |
| 550 | + $dedupeKey = $mediaId > 0 ? 'i:' . $mediaId : 'u:' . $url; | |
| 551 | + if (isset($addedMediaKeys[$dedupeKey])) { | |
| 552 | + continue; // skip — already added this image | |
| 553 | + } | |
| 554 | + $addedMediaKeys[$dedupeKey] = true; | |
| 555 | + } | |
| 369 | 556 | $allGalleryImages[] = [ |
| 370 | 557 | 'url' => $url, |
| 371 | 558 | 'title' => Arr::get($item, 'title', ''), |
| 372 | 559 | 'variation_id' => (string) $imageId, |
| 560 | + 'term_id' => (int) ($this->variantTermMap[(int) $imageId] ?? 0), | |
| 561 | + 'media_id' => $mediaId, | |
| 373 | 562 | ]; |
| 374 | 563 | } |
| 375 | 564 | } |
| 376 | 565 | |
| @@ -379,9 +568,10 @@ | ||
| 379 | 568 | <div class="fct-gallery-thumb-controls" |
| 380 | 569 | role="toolbar" |
| 381 | 570 | aria-label="<?php echo esc_attr__('Product image thumbnails', 'fluent-cart'); ?>" |
| 382 | 571 | data-fluent-cart-single-product-page-product-thumbnail-controls |
| 383 | - data-all-gallery-images="<?php echo esc_attr(wp_json_encode($allGalleryImages) ?: '[]'); ?>"> | |
| 572 | + data-all-gallery-images="<?php echo esc_attr(wp_json_encode($allGalleryImages) ?: '[]'); ?>" | |
| 573 | + data-variant-first-media-map="<?php echo esc_attr(wp_json_encode($variantFirstMediaMap) ?: '{}'); ?>"> | |
| 384 | 574 | |
| 385 | 575 | <?php $this->renderGalleryThumbControl($maxThumbnails); ?> |
| 386 | 576 | |
| 387 | 577 | </div> |
| @@ -395,45 +585,76 @@ | ||
| 395 | 585 | if ($maxThumbnails !== null && $maxThumbnails <= 0) { |
| 396 | 586 | $maxThumbnails = null; // treat invalid value as "no limit" |
| 397 | 587 | } |
| 398 | 588 | |
| 399 | - $count = 0; | |
| 400 | - $totalImages = 0; | |
| 589 | + $isAdvVariation = !empty($this->variantTermMap); | |
| 590 | + $countedMediaKeys = []; | |
| 591 | + $count = 0; | |
| 592 | + $totalImages = 0; | |
| 593 | + $videoRenderer = $this->getProductVideoRenderer(); | |
| 401 | 594 | |
| 402 | - // First, count total renderable images | |
| 595 | + // The gallery opens on a video the admin put in front of every image, | |
| 596 | + // so no image thumb takes the selected slot in that case. | |
| 597 | + if ($videoRenderer->getDefaultMediaId()) { | |
| 598 | + $this->galleryActiveSet = true; | |
| 599 | + } | |
| 600 | + | |
| 601 | + // Count unique images to render. For advanced variations, deduplicate by WP media ID | |
| 602 | + // when available, or by URL for externally imported images (media ID = 0). | |
| 403 | 603 | foreach ($this->images as $imageId => $image) { |
| 404 | 604 | if (empty($image['media']) || !is_array($image['media'])) { |
| 405 | 605 | continue; |
| 406 | 606 | } |
| 407 | 607 | foreach ($image['media'] as $item) { |
| 408 | - if (!empty(Arr::get($item, 'url', ''))) { | |
| 409 | - $totalImages++; | |
| 608 | + $url = Arr::get($item, 'url', ''); | |
| 609 | + $mediaId = (int) Arr::get($item, 'id', 0); | |
| 610 | + if (empty($url)) { | |
| 611 | + continue; | |
| 410 | 612 | } |
| 613 | + if ($isAdvVariation) { | |
| 614 | + $mediaDedupeKey = $mediaId > 0 ? 'i:' . $mediaId : 'u:' . $url; | |
| 615 | + if (isset($countedMediaKeys[$mediaDedupeKey])) { | |
| 616 | + continue; | |
| 617 | + } | |
| 618 | + $countedMediaKeys[$mediaDedupeKey] = true; | |
| 619 | + } | |
| 620 | + $totalImages++; | |
| 411 | 621 | } |
| 412 | 622 | } |
| 413 | 623 | |
| 414 | - // Then render up to max | |
| 624 | + $renderedMediaKeys = []; | |
| 625 | + // Render up to max; skip already-rendered media for advanced variation products. | |
| 415 | 626 | foreach ($this->images as $imageId => $image) { |
| 416 | 627 | if (empty($image['media']) || !is_array($image['media'])) { |
| 417 | 628 | continue; |
| 418 | 629 | } |
| 419 | - | |
| 420 | 630 | foreach ($image['media'] as $item) { |
| 421 | - if (empty(Arr::get($item, 'url', ''))) { | |
| 631 | + $url = Arr::get($item, 'url', ''); | |
| 632 | + $mediaId = (int) Arr::get($item, 'id', 0); | |
| 633 | + if (empty($url)) { | |
| 422 | 634 | continue; |
| 423 | 635 | } |
| 424 | - | |
| 636 | + if ($isAdvVariation) { | |
| 637 | + $mediaDedupeKey = $mediaId > 0 ? 'i:' . $mediaId : 'u:' . $url; | |
| 638 | + if (isset($renderedMediaKeys[$mediaDedupeKey])) { | |
| 639 | + continue; | |
| 640 | + } | |
| 641 | + $renderedMediaKeys[$mediaDedupeKey] = true; | |
| 642 | + } | |
| 425 | 643 | if ($maxThumbnails !== null && $count >= (int) $maxThumbnails) { |
| 644 | + $videoRenderer->renderThumbControls(!$this->galleryActiveSet); | |
| 426 | 645 | $this->renderGallerySeeMoreButton($totalImages - (int) $maxThumbnails); |
| 427 | 646 | return; |
| 428 | 647 | } |
| 429 | - | |
| 430 | - $this->renderGalleryThumbControlButton($item, $imageId); | |
| 648 | + // Videos the admin dragged in front of this image come first. | |
| 649 | + $videoRenderer->renderThumbControlsBefore($count, !$this->galleryActiveSet); | |
| 650 | + $termId = (int) ($this->variantTermMap[(int) $imageId] ?? 0); | |
| 651 | + $this->renderGalleryThumbControlButton($item, $imageId, $termId, $mediaId); | |
| 431 | 652 | $count++; |
| 432 | 653 | } |
| 433 | - | |
| 434 | 654 | } |
| 435 | 655 | |
| 656 | + $videoRenderer->renderThumbControls(!$this->galleryActiveSet); | |
| 436 | 657 | } |
| 437 | 658 | |
| 438 | 659 | public function renderGallerySeeMoreButton($remainingCount) |
| 439 | 660 | { |
| @@ -464,9 +685,9 @@ | ||
| 464 | 685 | </button> |
| 465 | 686 | <?php |
| 466 | 687 | } |
| 467 | 688 | |
| 468 | - public function renderGalleryThumbControlButton($item, $imageId) | |
| 689 | + public function renderGalleryThumbControlButton($item, $imageId, $termId = 0, $mediaId = 0) | |
| 469 | 690 | { |
| 470 | 691 | |
| 471 | 692 | $isHidden = ''; //$imageId != $this->defaultVariationId ? 'is-hidden' : ''; |
| 472 | 693 | $itemUrl = Arr::get($item, 'url', ''); |
| @@ -482,11 +703,13 @@ | ||
| 482 | 703 | class="fct-gallery-thumb-control-button <?php echo $isSelected ? 'active' : ''; ?> <?php echo esc_attr($isHidden); ?>" |
| 483 | 704 | data-fluent-cart-thumb-control-button |
| 484 | 705 | data-url="<?php echo esc_url($itemUrl); ?>" |
| 485 | 706 | data-variation-id="<?php echo esc_attr($imageId); ?>" |
| 707 | + data-term-id="<?php echo esc_attr((string) $termId); ?>" | |
| 708 | + data-media-id="<?php echo esc_attr((string) $mediaId); ?>" | |
| 486 | 709 | aria-label="<?php echo |
| 487 | - /* translators: %s image title */ | |
| 488 | - esc_attr(sprintf(__('View %s image', 'fluent-cart'), $itemTitle)); | |
| 710 | + /* translators: %1$s: image title */ | |
| 711 | + esc_attr(sprintf(__('View %1$s image', 'fluent-cart'), $itemTitle)); | |
| 489 | 712 | ?>" |
| 490 | 713 | aria-pressed="<?php echo $isSelected ? 'true' : 'false'; ?>" |
| 491 | 714 | tabindex="<?php echo $isSelected ? '0' : '-1'; ?>" |
| 492 | 715 | > |
| @@ -504,9 +727,14 @@ | ||
| 504 | 727 | } |
| 505 | 728 | |
| 506 | 729 | public function renderGallery($args = []) |
| 507 | 730 | { |
| 731 | + $gateContext = RenderGate::context($this->product, RenderGate::SCOPE_SINGLE, $this->defaultVariant); | |
| 508 | 732 | |
| 733 | + if (!RenderGate::shouldRender('image', $gateContext)) { | |
| 734 | + return; | |
| 735 | + } | |
| 736 | + | |
| 509 | 737 | $defaults = [ |
| 510 | 738 | 'thumbnail_mode' => 'all', // horizontal, vertical |
| 511 | 739 | 'thumb_position' => 'bottom', // bottom, left, right, top |
| 512 | 740 | 'scrollable_thumbs' => 'no', // yes / no |
| @@ -540,13 +768,21 @@ | ||
| 540 | 768 | } |
| 541 | 769 | |
| 542 | 770 | public function renderTitle() |
| 543 | 771 | { |
| 772 | + $gateContext = RenderGate::context($this->product, RenderGate::SCOPE_SINGLE, $this->defaultVariant); | |
| 773 | + | |
| 774 | + if (!RenderGate::shouldRender('title', $gateContext)) { | |
| 775 | + return; | |
| 776 | + } | |
| 777 | + | |
| 778 | + do_action('fluent_cart/product/single/before_title_block', $gateContext); | |
| 544 | 779 | ?> |
| 545 | 780 | <div class="fct-product-title"> |
| 546 | 781 | <h1 id="fct-product-summary-title"><?php echo esc_html($this->product->post_title); ?></h1> |
| 547 | 782 | </div> |
| 548 | 783 | <?php |
| 784 | + do_action('fluent_cart/product/single/after_title_block', $gateContext); | |
| 549 | 785 | } |
| 550 | 786 | |
| 551 | 787 | public function renderStockAvailability($wrapper_attributes = '') |
| 552 | 788 | { |
| @@ -566,15 +802,44 @@ | ||
| 566 | 802 | // Check default variant stock for both simple and variable products |
| 567 | 803 | if ($this->defaultVariant) { |
| 568 | 804 | $isStock = $isStock && $this->defaultVariant->isStock(); |
| 569 | 805 | } |
| 806 | + | |
| 570 | 807 | $stockLabel = Arr::get($stockAvailability, 'availability'); |
| 571 | - $statusClass = $isStock ? ($stockAvailability['class'] ?? '') : 'out-of-stock'; | |
| 808 | + $statusClass = $stockAvailability['class'] ?? ''; | |
| 809 | + | |
| 810 | + // Optional per-status custom labels (e.g. set on the Bricks Product Stock | |
| 811 | + // element via the fluent_cart/product_stock_availability filter). Emitted as | |
| 812 | + // data-attributes so the frontend JS, which re-derives the badge text on load | |
| 813 | + // and on variant switches, prefers them over the generic label map instead of | |
| 814 | + // overwriting them. Absent for the default template, so behavior is unchanged. | |
| 815 | + $inStockText = Arr::get($stockAvailability, 'in_stock_text'); | |
| 816 | + $outOfStockText = Arr::get($stockAvailability, 'out_of_stock_text'); | |
| 817 | + | |
| 818 | + // The variant-level check above can override the aggregate stock_availability | |
| 819 | + // used for $stockLabel/$statusClass (e.g. this specific default variant is out | |
| 820 | + // of stock even though the product overall has other in-stock variants) — keep | |
| 821 | + // the label and class in sync so the badge never shows mismatched text/color. | |
| 822 | + // Honor the custom out-of-stock label here too so it survives this override on | |
| 823 | + // first load, matching what the frontend JS shows after a variant switch. | |
| 824 | + if (!$isStock) { | |
| 825 | + $statusClass = 'out-of-stock'; | |
| 826 | + $stockLabel = !empty($outOfStockText) ? $outOfStockText : __('Out of Stock', 'fluent-cart'); | |
| 827 | + } | |
| 828 | + | |
| 829 | + $badgeAttributes = ''; | |
| 830 | + if (!empty($inStockText)) { | |
| 831 | + $badgeAttributes .= sprintf(' data-in-stock-text="%s"', esc_attr($inStockText)); | |
| 832 | + } | |
| 833 | + if (!empty($outOfStockText)) { | |
| 834 | + $badgeAttributes .= sprintf(' data-out-of-stock-text="%s"', esc_attr($outOfStockText)); | |
| 835 | + } | |
| 836 | + | |
| 572 | 837 | echo sprintf( |
| 573 | 838 | '<div class="fct-product-stock %1$s" role="status" aria-live="polite"> |
| 574 | 839 | <div %2$s> |
| 575 | 840 | <span class="fct-stock-label">%3$s</span> |
| 576 | - <span class="fct-stock-badge fct_status_badge_%1$s" data-fluent-cart-product-stock> | |
| 841 | + <span class="fct-stock-badge fct_status_badge_%1$s" data-fluent-cart-product-stock%5$s> | |
| 577 | 842 | %4$s |
| 578 | 843 | </span> |
| 579 | 844 | </div> |
| 580 | 845 | </div>', |
| @@ -580,22 +845,15 @@ | ||
| 580 | 845 | </div>', |
| 581 | 846 | esc_attr($statusClass), |
| 582 | 847 | $wrapper_attributes, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 583 | 848 | esc_html__('Availability:', 'fluent-cart'), |
| 584 | - esc_html($stockLabel) | |
| 849 | + esc_html($stockLabel), | |
| 850 | + $badgeAttributes // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- values passed through esc_attr() above | |
| 585 | 851 | ); |
| 586 | 852 | } |
| 587 | 853 | |
| 588 | 854 | public function renderSku($wrapper_attributes = '', $showLabel = true, $label = '', $variant = null) |
| 589 | 855 | { |
| 590 | - if (!$variant) { | |
| 591 | - $variant = $this->defaultVariant ?: $this->product->variants->first(); | |
| 592 | - } | |
| 593 | - | |
| 594 | - if (!$variant || empty($variant->sku)) { | |
| 595 | - return; | |
| 596 | - } | |
| 597 | - | |
| 598 | 856 | if (!$label) { |
| 599 | 857 | $label = __('SKU:', 'fluent-cart'); |
| 600 | 858 | } |
| 601 | 859 | |
| @@ -603,9 +861,13 @@ | ||
| 603 | 861 | if ($showLabel && $label) { |
| 604 | 862 | $labelHtml = sprintf('<span class="fct-product-sku__label">%s</span> ', esc_html($label)); |
| 605 | 863 | } |
| 606 | 864 | |
| 607 | - echo sprintf( | |
| 865 | + if ($variant) { | |
| 866 | + if (empty($variant->sku)) { | |
| 867 | + return; | |
| 868 | + } | |
| 869 | + echo sprintf( | |
| 608 | 870 | '<div class="fct-product-sku"> |
| 609 | 871 | <div %s> |
| 610 | 872 | %s<span class="fct-product-sku__value" data-fluent-cart-product-sku>%s</span> |
| 611 | 873 | </div> |
| @@ -612,15 +874,48 @@ | ||
| 612 | 874 | </div>', |
| 613 | 875 | $wrapper_attributes, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 614 | 876 | $labelHtml, |
| 615 | 877 | esc_html($variant->sku) |
| 616 | - ); | |
| 878 | + ); | |
| 879 | + return; | |
| 880 | + } | |
| 881 | + | |
| 882 | + foreach ($this->product->variants as $v) { | |
| 883 | + if (empty($v->sku)) { | |
| 884 | + continue; | |
| 885 | + } | |
| 886 | + $isHidden = ($this->defaultVariant && $this->defaultVariant->id != $v->id) ? ' is-hidden' : ''; | |
| 887 | + echo sprintf( | |
| 888 | + '<div class="fct-product-sku fluent-cart-product-variation-content%s" data-variation-id="%s"> | |
| 889 | + <div %s> | |
| 890 | + %s<span class="fct-product-sku__value" data-fluent-cart-product-sku>%s</span> | |
| 891 | + </div> | |
| 892 | + </div>', | |
| 893 | + $isHidden, | |
| 894 | + esc_attr($v->id), | |
| 895 | + $wrapper_attributes, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 896 | + $labelHtml, | |
| 897 | + esc_html($v->sku) | |
| 898 | + ); | |
| 899 | + } | |
| 617 | 900 | } |
| 618 | 901 | |
| 902 | + /** | |
| 903 | + * @deprecated Use PackageDescriptionRenderer::renderPackageDescription() directly. | |
| 904 | + * Kept as a compatibility shim for external callers (themes, extensions) — | |
| 905 | + * package-description rendering now lives in PackageDescriptionRenderer. | |
| 906 | + */ | |
| 619 | 907 | public function renderPackageDescription($wrapper_attributes = '', $showName = true, $showDimensions = true, $showProductWeight = true, $showTotalWeight = true, $variant = null) |
| 620 | 908 | { |
| 621 | - $variant = $variant ?: ($this->defaultVariant ?: $this->product->variants->first()); | |
| 622 | - (new ProductCardRender($this->product))->renderPackageDescription($wrapper_attributes, $showName, $showDimensions, $showProductWeight, $showTotalWeight, $variant); | |
| 909 | + (new PackageDescriptionRenderer($this->product))->renderPackageDescription( | |
| 910 | + $wrapper_attributes, | |
| 911 | + $showName, | |
| 912 | + $showDimensions, | |
| 913 | + $showProductWeight, | |
| 914 | + $showTotalWeight, | |
| 915 | + $variant, | |
| 916 | + $this->defaultVariant | |
| 917 | + ); | |
| 623 | 918 | } |
| 624 | 919 | |
| 625 | 920 | /** |
| 626 | 921 | * Build a JSON string of package info for a variant (used as data attribute for JS switching). |
| @@ -689,14 +984,22 @@ | ||
| 689 | 984 | $excerpt = $this->product->post_excerpt; |
| 690 | 985 | if (!$excerpt) { |
| 691 | 986 | return; |
| 692 | 987 | } |
| 988 | + | |
| 989 | + $gateContext = RenderGate::context($this->product, RenderGate::SCOPE_SINGLE, $this->defaultVariant); | |
| 990 | + | |
| 991 | + if (!RenderGate::shouldRender('excerpt', $gateContext)) { | |
| 992 | + return; | |
| 993 | + } | |
| 994 | + | |
| 995 | + do_action('fluent_cart/product/single/before_excerpt_block', $gateContext); | |
| 693 | 996 | ?> |
| 694 | 997 | <div class="fct-product-excerpt" aria-labelledby="fct-product-summary-title"> |
| 695 | 998 | <p><?php echo wp_kses_post($excerpt); ?></p> |
| 696 | 999 | </div> |
| 697 | 1000 | <?php |
| 698 | - | |
| 1001 | + do_action('fluent_cart/product/single/after_excerpt_block', $gateContext); | |
| 699 | 1002 | } |
| 700 | 1003 | |
| 701 | 1004 | public function renderDescription() |
| 702 | 1005 | { |
| @@ -726,8 +1029,14 @@ | ||
| 726 | 1029 | } |
| 727 | 1030 | |
| 728 | 1031 | public function renderPrices() |
| 729 | 1032 | { |
| 1033 | + $gateContext = RenderGate::context($this->product, RenderGate::SCOPE_SINGLE, $this->defaultVariant); | |
| 1034 | + | |
| 1035 | + if (!RenderGate::shouldRender('price', $gateContext)) { | |
| 1036 | + return; | |
| 1037 | + } | |
| 1038 | + | |
| 730 | 1039 | if ($this->product->detail->variation_type === 'simple') { |
| 731 | 1040 | // we have to render for the simple product |
| 732 | 1041 | |
| 733 | 1042 | $first_price = $this->product->variants()->first(); |
| @@ -741,97 +1050,76 @@ | ||
| 741 | 1050 | $comparePrice = $first_price ? (int)$first_price->compare_price : 0; |
| 742 | 1051 | if ($comparePrice <= $itemPrice) { |
| 743 | 1052 | $comparePrice = 0; |
| 744 | 1053 | } |
| 745 | - do_action('fluent_cart/product/single/before_price_block', [ | |
| 1054 | + do_action('fluent_cart/product/single/before_price_block', RenderContext::decorate([ | |
| 746 | 1055 | 'product' => $this->product, |
| 747 | 1056 | 'current_price' => $itemPrice, |
| 748 | 1057 | 'scope' => 'price_range' |
| 749 | - ]); | |
| 1058 | + ])); | |
| 750 | 1059 | ?> |
| 751 | - <?php | |
| 1060 | + <div class="fct-price-range fct-product-prices"> | |
| 752 | 1061 | |
| 753 | - if ($comparePrice) { | |
| 754 | - $aria_label = sprintf( | |
| 755 | - /* translators: 1: Original price, 2: Current item price */ | |
| 756 | - __('Original Price: %1$s, Price: %2$s', 'fluent-cart'), | |
| 757 | - Helper::toDecimal($comparePrice), | |
| 758 | - Helper::toDecimal($itemPrice) | |
| 759 | - ); | |
| 760 | - } else { | |
| 761 | - $aria_label = sprintf( | |
| 762 | - /* translators: 1: Current item price */ | |
| 763 | - __('Price: %1$s', 'fluent-cart'), | |
| 764 | - Helper::toDecimal($itemPrice) | |
| 765 | - ); | |
| 766 | - } | |
| 767 | - | |
| 768 | - ?> | |
| 769 | - <div class="fct-price-range fct-product-prices" role="term" | |
| 770 | - aria-label="<?php echo esc_attr($aria_label); ?>"> | |
| 771 | - | |
| 772 | 1062 | <?php if ($comparePrice): ?> |
| 773 | 1063 | <span class="fct-compare-price"> |
| 774 | - <del aria-label="<?php echo esc_attr(__('Original price', 'fluent-cart')); ?>"><?php echo esc_html(Helper::toDecimal($comparePrice)); ?></del> | |
| 1064 | + <span class="fct-sr-only"><?php echo esc_html__('Original price:', 'fluent-cart'); ?></span> | |
| 1065 | + <del><?php echo esc_html(Helper::toDecimal($comparePrice)); ?></del> | |
| 775 | 1066 | </span> |
| 776 | 1067 | <?php endif; ?> |
| 777 | - <span class="fct-item-price" aria-label="<?php echo esc_attr(__('Current price', 'fluent-cart')); ?>"> | |
| 1068 | + <span class="fct-item-price"> | |
| 1069 | + <span class="fct-sr-only"><?php echo $comparePrice ? esc_html__('Sale price:', 'fluent-cart') : esc_html__('Price:', 'fluent-cart'); ?></span> | |
| 778 | 1070 | <?php echo esc_html(Helper::toDecimal($itemPrice)); ?> |
| 779 | - <?php do_action('fluent_cart/product/after_price', [ | |
| 1071 | + <?php do_action('fluent_cart/product/after_price', RenderContext::decorate([ | |
| 780 | 1072 | 'product' => $this->product, |
| 1073 | + 'variant' => $first_price, | |
| 781 | 1074 | 'current_price' => $itemPrice, |
| 782 | 1075 | 'scope' => 'price_range' |
| 783 | - ]); ?> | |
| 1076 | + ])); ?> | |
| 1077 | + <?php RenderHelper::renderPriceSuffix($this->product, $first_price, 'price_range'); ?> | |
| 784 | 1078 | </span> |
| 785 | 1079 | </div> |
| 786 | 1080 | <?php |
| 787 | - do_action('fluent_cart/product/single/after_price_block', [ | |
| 1081 | + do_action('fluent_cart/product/single/after_price_block', RenderContext::decorate([ | |
| 788 | 1082 | 'product' => $this->product, |
| 789 | 1083 | 'current_price' => $itemPrice, |
| 790 | 1084 | 'scope' => 'price_range' |
| 791 | - ]); | |
| 1085 | + ])); | |
| 792 | 1086 | return; |
| 793 | 1087 | } |
| 794 | 1088 | $min_price = $this->product->detail->min_price; |
| 795 | 1089 | $max_price = $this->product->detail->max_price; |
| 796 | 1090 | |
| 797 | - do_action('fluent_cart/product/single/before_price_range_block', [ | |
| 1091 | + do_action('fluent_cart/product/single/before_price_range_block', RenderContext::decorate([ | |
| 798 | 1092 | 'product' => $this->product, |
| 799 | 1093 | 'current_price' => $min_price, |
| 800 | 1094 | 'scope' => 'price_range' |
| 801 | - ]); | |
| 1095 | + ])); | |
| 802 | 1096 | ?> |
| 803 | - <?php | |
| 804 | - $aria_label = sprintf( | |
| 805 | - /* translators: 1: Minimum price, 2: Maximum price */ | |
| 806 | - __('Price range: %1$s - %2$s', 'fluent-cart'), | |
| 807 | - Helper::toDecimal($min_price), | |
| 808 | - Helper::toDecimal($max_price) | |
| 809 | - ); | |
| 810 | - ?> | |
| 811 | - <div class="fct-product-prices fct-price-range" role="term" aria-label="<?php echo esc_attr($aria_label); ?>"> | |
| 1097 | + <div class="fct-product-prices fct-price-range"> | |
| 812 | 1098 | |
| 813 | 1099 | <?php if ($max_price && $max_price != $min_price && $max_price > $min_price): ?> |
| 1100 | + <span class="fct-sr-only"><?php echo esc_html__('Price range:', 'fluent-cart'); ?></span> | |
| 814 | 1101 | <span class="fct-min-price"><?php echo esc_html(Helper::toDecimal($min_price)); ?></span> |
| 815 | 1102 | <span class="fct-price-separator" aria-hidden="true">-</span> |
| 1103 | + <span class="fct-sr-only"><?php echo esc_html__('to', 'fluent-cart'); ?></span> | |
| 816 | 1104 | <?php endif; ?> |
| 817 | 1105 | <span class="fct-max-price"> |
| 818 | 1106 | <?php echo esc_html(Helper::toDecimal($max_price)); ?> |
| 819 | 1107 | </span> |
| 820 | 1108 | |
| 821 | - <?php do_action('fluent_cart/product/after_price', [ | |
| 1109 | + <?php do_action('fluent_cart/product/after_price', RenderContext::decorate([ | |
| 822 | 1110 | 'product' => $this->product, |
| 823 | 1111 | 'current_price' => $min_price, |
| 824 | 1112 | 'scope' => 'price_range' |
| 825 | - ]); ?> | |
| 1113 | + ])); ?> | |
| 826 | 1114 | |
| 827 | 1115 | </div> |
| 828 | 1116 | <?php |
| 829 | - do_action('fluent_cart/product/single/after_price_range_block', [ | |
| 1117 | + do_action('fluent_cart/product/single/after_price_range_block', RenderContext::decorate([ | |
| 830 | 1118 | 'product' => $this->product, |
| 831 | 1119 | 'current_price' => $min_price, |
| 832 | 1120 | 'scope' => 'price_range' |
| 833 | - ]); | |
| 1121 | + ])); | |
| 834 | 1122 | } |
| 835 | 1123 | |
| 836 | 1124 | public function renderVariants($atts = []) |
| 837 | 1125 | { |
| @@ -856,19 +1144,19 @@ | ||
| 856 | 1144 | ?> |
| 857 | 1145 | <div class="<?php echo esc_attr(implode(' ', $classes)); ?>" role="radiogroup" |
| 858 | 1146 | aria-label="<?php esc_attr_e('Product Variants', 'fluent-cart'); ?>"> |
| 859 | 1147 | <?php foreach ($variants as $variant) { |
| 860 | - do_action('fluent_cart/product/single/before_variant_item', [ | |
| 1148 | + do_action('fluent_cart/product/single/before_variant_item', RenderContext::decorate([ | |
| 861 | 1149 | 'product' => $this->product, |
| 862 | 1150 | 'variant' => $variant, |
| 863 | 1151 | 'scope' => 'product_variant_item' |
| 864 | - ]); | |
| 1152 | + ])); | |
| 865 | 1153 | $this->renderVariationItem($variant, $this->defaultVariationId); |
| 866 | - do_action('fluent_cart/product/single/after_variant_item', [ | |
| 1154 | + do_action('fluent_cart/product/single/after_variant_item', RenderContext::decorate([ | |
| 867 | 1155 | 'product' => $this->product, |
| 868 | 1156 | 'variant' => $variant, |
| 869 | 1157 | 'scope' => 'product_variant_item' |
| 870 | - ]); | |
| 1158 | + ])); | |
| 871 | 1159 | } ?> |
| 872 | 1160 | </div> |
| 873 | 1161 | <?php |
| 874 | 1162 | } |
| @@ -874,17 +1162,24 @@ | ||
| 874 | 1162 | } |
| 875 | 1163 | |
| 876 | 1164 | public function renderItemPrice() |
| 877 | 1165 | { |
| 1166 | + // Same 'price' gate as renderPrices(): one filter hides the price | |
| 1167 | + // wherever it appears, rather than making callers hunt for the second | |
| 1168 | + // place the single product page prints one. | |
| 1169 | + if (!RenderGate::shouldRender('price', RenderGate::context($this->product, RenderGate::SCOPE_SINGLE, $this->defaultVariant))) { | |
| 1170 | + return; | |
| 1171 | + } | |
| 1172 | + | |
| 878 | 1173 | if ($this->product->detail->variation_type === 'simple' && !$this->hasSubscription) { |
| 879 | 1174 | return; // for simple product we already rendered the price |
| 880 | 1175 | } |
| 881 | 1176 | |
| 882 | - do_action('fluent_cart/product/single/before_price_block', [ | |
| 1177 | + do_action('fluent_cart/product/single/before_price_block', RenderContext::decorate([ | |
| 883 | 1178 | 'product' => $this->product, |
| 884 | 1179 | 'current_price' => $this->defaultVariant ? $this->defaultVariant->item_price : 0, |
| 885 | 1180 | 'scope' => 'product_variant_price' |
| 886 | - ]); | |
| 1181 | + ])); | |
| 887 | 1182 | |
| 888 | 1183 | foreach ($this->product->variants as $variant) { |
| 889 | 1184 | if ($this->shouldRenderPriceInPriceSection()) { |
| 890 | 1185 | $this->renderVariantPricingWrapperStart($variant); |
| @@ -918,18 +1213,31 @@ | ||
| 918 | 1213 | |
| 919 | 1214 | |
| 920 | 1215 | |
| 921 | 1216 | |
| 922 | - do_action('fluent_cart/product/single/after_price_block', [ | |
| 1217 | + do_action('fluent_cart/product/single/after_price_block', RenderContext::decorate([ | |
| 923 | 1218 | 'product' => $this->product, |
| 924 | 1219 | 'current_price' => $this->defaultVariant ? $this->defaultVariant->item_price : 0, |
| 925 | 1220 | 'scope' => 'product_variant_price' |
| 926 | - ]); | |
| 1221 | + ])); | |
| 927 | 1222 | } |
| 928 | 1223 | |
| 929 | 1224 | public function shouldRenderPriceInPriceSection(): bool |
| 930 | 1225 | { |
| 931 | - return !($this->viewType === 'text' && $this->columnType === 'one'); | |
| 1226 | + // simple_variations keeps the legacy inline price flow for the | |
| 1227 | + // one-column layouts that render the variant title (text-only and | |
| 1228 | + // image+text), where the price sits on each variant row. | |
| 1229 | + // Every other variation type always renders the price in the dedicated | |
| 1230 | + // below-block so the data-fluent-cart-product-item-price element keeps | |
| 1231 | + // a consistent position across every view/column combination. | |
| 1232 | + if ($this->product->detail->variation_type === Helper::PRODUCT_TYPE_SIMPLE_VARIATION) { | |
| 1233 | + $isInlineRowLayout = in_array($this->viewType, ['text', 'both'], true) | |
| 1234 | + && $this->columnType === 'one'; | |
| 1235 | + | |
| 1236 | + return !$isInlineRowLayout; | |
| 1237 | + } | |
| 1238 | + | |
| 1239 | + return true; | |
| 932 | 1240 | } |
| 933 | 1241 | |
| 934 | 1242 | public function applyVariationPriceFilter($variant, $paymentType = 'onetime') |
| 935 | 1243 | { |
| @@ -938,14 +1246,15 @@ | ||
| 938 | 1246 | 'product' => $this->product, |
| 939 | 1247 | 'variant' => $variant, |
| 940 | 1248 | 'scope' => 'product_variant_price' |
| 941 | 1249 | ])); |
| 942 | - do_action('fluent_cart/product/after_price', [ | |
| 1250 | + do_action('fluent_cart/product/after_price', RenderContext::decorate([ | |
| 943 | 1251 | 'product' => $this->product, |
| 944 | 1252 | 'variant' => $variant, |
| 945 | 1253 | 'current_price' => $variant->item_price, |
| 946 | 1254 | 'scope' => 'product_variant_price' |
| 947 | - ]); | |
| 1255 | + ])); | |
| 1256 | + RenderHelper::renderPriceSuffix($this->product, $variant, 'product_variant_price'); | |
| 948 | 1257 | } |
| 949 | 1258 | |
| 950 | 1259 | public function renderComparePriceWrapperStart($atts = []) |
| 951 | 1260 | { |
| @@ -967,8 +1276,9 @@ | ||
| 967 | 1276 | return; |
| 968 | 1277 | } ?> |
| 969 | 1278 | |
| 970 | 1279 | <span class="fct-compare-price"> |
| 1280 | + <span class="fct-sr-only"><?php echo esc_html__('Original price:', 'fluent-cart'); ?></span> | |
| 971 | 1281 | <del><?php echo esc_html(Helper::toDecimal($variant->compare_price)); ?></del> |
| 972 | 1282 | </span> |
| 973 | 1283 | <?php |
| 974 | 1284 | } |
| @@ -1054,8 +1364,14 @@ | ||
| 1054 | 1364 | <?php } |
| 1055 | 1365 | |
| 1056 | 1366 | public function renderQuantity() |
| 1057 | 1367 | { |
| 1368 | + $gateContext = RenderGate::context($this->product, RenderGate::SCOPE_SINGLE, $this->defaultVariant); | |
| 1369 | + | |
| 1370 | + if (!RenderGate::shouldRender('quantity', $gateContext)) { | |
| 1371 | + return; | |
| 1372 | + } | |
| 1373 | + | |
| 1058 | 1374 | $soldIndividually = $this->product->soldIndividually(); |
| 1059 | 1375 | |
| 1060 | 1376 | if (!$this->hasOnetime || $soldIndividually) { |
| 1061 | 1377 | return; |
| @@ -1074,12 +1390,12 @@ | ||
| 1074 | 1390 | if ($this->hasSubscription && Arr::get($defaultVariantData, 'payment_type') !== 'onetime') { |
| 1075 | 1391 | $attributes['class'] .= ' is-hidden'; |
| 1076 | 1392 | } |
| 1077 | 1393 | |
| 1078 | - do_action('fluent_cart/product/single/before_quantity_block', [ | |
| 1394 | + do_action('fluent_cart/product/single/before_quantity_block', RenderContext::decorate([ | |
| 1079 | 1395 | 'product' => $this->product, |
| 1080 | 1396 | 'scope' => 'product_quantity_block' |
| 1081 | - ]); | |
| 1397 | + ])); | |
| 1082 | 1398 | ?> |
| 1083 | 1399 | <div <?php $this->renderAttributes($attributes); ?>> |
| 1084 | 1400 | <label for="fct-product-qty-input" class="quantity-title"> |
| 1085 | 1401 | <?php esc_html_e('Quantity', 'fluent-cart'); ?> |
| @@ -1123,23 +1439,39 @@ | ||
| 1123 | 1439 | </button> |
| 1124 | 1440 | </div> |
| 1125 | 1441 | </div> |
| 1126 | 1442 | <?php |
| 1127 | - do_action('fluent_cart/product/single/after_quantity_block', [ | |
| 1443 | + do_action('fluent_cart/product/single/after_quantity_block', RenderContext::decorate([ | |
| 1128 | 1444 | 'product' => $this->product, |
| 1129 | 1445 | 'scope' => 'product_quantity_block' |
| 1130 | - ]); | |
| 1446 | + ])); | |
| 1131 | 1447 | } |
| 1132 | 1448 | |
| 1133 | 1449 | public function renderPurchaseButtons($atts = []) |
| 1134 | 1450 | { |
| 1451 | + $gateContext = RenderGate::context($this->product, RenderGate::SCOPE_SINGLE, $this->defaultVariant); | |
| 1452 | + | |
| 1453 | + if (!RenderGate::shouldRender('actions', $gateContext)) { | |
| 1454 | + return; | |
| 1455 | + } | |
| 1456 | + | |
| 1457 | + do_action('fluent_cart/product/single/before_actions_block', $gateContext); | |
| 1458 | + | |
| 1135 | 1459 | $buyNowButtonAtts = $atts; |
| 1136 | 1460 | $this->renderBuyNowButton($buyNowButtonAtts); |
| 1137 | 1461 | $this->renderAddToCartButton($atts); |
| 1462 | + | |
| 1463 | + do_action('fluent_cart/product/single/after_actions_block', $gateContext); | |
| 1138 | 1464 | } |
| 1139 | 1465 | |
| 1140 | 1466 | public function renderBuyNowButton($atts = []) |
| 1141 | 1467 | { |
| 1468 | + $gateContext = RenderGate::context($this->product, RenderGate::SCOPE_SINGLE, $this->defaultVariant); | |
| 1469 | + | |
| 1470 | + if (!RenderGate::shouldRenderPurchaseButton('buy_now_button', $gateContext)) { | |
| 1471 | + return; | |
| 1472 | + } | |
| 1473 | + | |
| 1142 | 1474 | // Stock management check using isStock() method |
| 1143 | 1475 | // if (ModuleSettings::isActive('stock_management')) { |
| 1144 | 1476 | // if ($this->product->detail->variation_type === 'simple' && $this->defaultVariant) { |
| 1145 | 1477 | // if (!$this->defaultVariant->isStock()) { |
| @@ -1221,8 +1553,16 @@ | ||
| 1221 | 1553 | } |
| 1222 | 1554 | |
| 1223 | 1555 | public function renderBuyNowButtonBlock($atts = []) |
| 1224 | 1556 | { |
| 1557 | + $gateContext = RenderGate::context($this->product, RenderGate::SCOPE_SINGLE, $this->defaultVariant); | |
| 1558 | + | |
| 1559 | + // Same gate as the in-section button: a page assembled from standalone | |
| 1560 | + // button blocks must honour catalog mode too, or the gate leaks. | |
| 1561 | + if (!RenderGate::shouldRenderPurchaseButton('buy_now_button', $gateContext)) { | |
| 1562 | + return; | |
| 1563 | + } | |
| 1564 | + | |
| 1225 | 1565 | $text = Arr::get($atts, 'text', __('Buy Now', 'fluent-cart')); |
| 1226 | 1566 | $variantIds = Arr::get($atts, 'variant_ids', []); |
| 1227 | 1567 | $variantId = Arr::get($variantIds, 0); |
| 1228 | 1568 | $customClass = trim(Arr::get($atts, 'class', '')); |
| @@ -1307,8 +1647,14 @@ | ||
| 1307 | 1647 | } |
| 1308 | 1648 | |
| 1309 | 1649 | public function renderAddToCartButton($atts = []) |
| 1310 | 1650 | { |
| 1651 | + $gateContext = RenderGate::context($this->product, RenderGate::SCOPE_SINGLE, $this->defaultVariant); | |
| 1652 | + | |
| 1653 | + if (!RenderGate::shouldRenderPurchaseButton('add_to_cart_button', $gateContext)) { | |
| 1654 | + return; | |
| 1655 | + } | |
| 1656 | + | |
| 1311 | 1657 | $defaults = [ |
| 1312 | 1658 | 'buy_now_text' => __('Buy Now', 'fluent-cart'), |
| 1313 | 1659 | 'add_to_cart_text' => __('Add To Cart', 'fluent-cart'), |
| 1314 | 1660 | ]; |
| @@ -1356,10 +1702,17 @@ | ||
| 1356 | 1702 | $wrapperAttributes = RenderHelper::getBlockWrapperAttributes($cartAttributes); |
| 1357 | 1703 | } |
| 1358 | 1704 | |
| 1359 | 1705 | |
| 1360 | - // Render add to cart if product supports onetime, or if out of stock (to show "Not Available") | |
| 1361 | - if ($this->hasOnetime || $isOutOfStock) : | |
| 1706 | + // Render add to cart when the product supports a one-time path, when out | |
| 1707 | + // of stock (to show "Not Available"), or for advanced-variation products. | |
| 1708 | + // The advanced selector toggles this button's visibility / disabled / | |
| 1709 | + // payment-type state per selection, so it must exist in the DOM even for | |
| 1710 | + // a subscription-only product whose in-stock default starts hidden via | |
| 1711 | + // the is-hidden class applied above — otherwise an out-of-stock | |
| 1712 | + // subscription combination has no button to surface "Not Available". | |
| 1713 | + $isAdvancedVariation = $this->product->detail->variation_type === Helper::PRODUCT_TYPE_ADVANCE_VARIATION; | |
| 1714 | + if ($this->hasOnetime || $isOutOfStock || $isAdvancedVariation) : | |
| 1362 | 1715 | ?> |
| 1363 | 1716 | <?php |
| 1364 | 1717 | $variantTitle = $this->defaultVariant ? $this->defaultVariant->variation_title : ''; |
| 1365 | 1718 | $addToCartAriaLabel = $variantTitle |
| @@ -1396,8 +1749,15 @@ | ||
| 1396 | 1749 | } |
| 1397 | 1750 | |
| 1398 | 1751 | public function renderAddToCartButtonBlock($atts = []) |
| 1399 | 1752 | { |
| 1753 | + $gateContext = RenderGate::context($this->product, RenderGate::SCOPE_SINGLE, $this->defaultVariant); | |
| 1754 | + | |
| 1755 | + // Same gate as the in-section button — see renderBuyNowButtonBlock(). | |
| 1756 | + if (!RenderGate::shouldRenderPurchaseButton('add_to_cart_button', $gateContext)) { | |
| 1757 | + return; | |
| 1758 | + } | |
| 1759 | + | |
| 1400 | 1760 | $text = Arr::get($atts, 'text', __('Add To Cart', 'fluent-cart')); |
| 1401 | 1761 | $customClass = trim(Arr::get($atts, 'class', '')); |
| 1402 | 1762 | $extraClass = trim(Arr::get($atts, 'extra_class', '')); |
| 1403 | 1763 | |
| @@ -1492,9 +1852,9 @@ | ||
| 1492 | 1852 | <p class="has-text-align-center has-large-font-size m-0"> |
| 1493 | 1853 | <?php echo esc_html__('No Product Found!', 'fluent-cart'); ?> |
| 1494 | 1854 | </p> |
| 1495 | 1855 | |
| 1496 | - <p class="has-text-align-center"> | |
| 1856 | + <p class="has-text-align-center m-0"> | |
| 1497 | 1857 | <?php echo esc_html__('You can try clearing any filters.', 'fluent-cart'); ?> |
| 1498 | 1858 | </p> |
| 1499 | 1859 | </div> |
| 1500 | 1860 | <?php |
| @@ -1531,14 +1891,8 @@ | ||
| 1531 | 1891 | if ($variant->id == $defaultId) { |
| 1532 | 1892 | $itemClasses[] = 'selected'; |
| 1533 | 1893 | } |
| 1534 | 1894 | |
| 1535 | - $priceSuffix = apply_filters('fluent_cart/product/price_suffix_atts', '', [ | |
| 1536 | - 'product' => $this->product, | |
| 1537 | - 'variant' => $variant, | |
| 1538 | - 'scope' => 'variant_item' | |
| 1539 | - ]); | |
| 1540 | - | |
| 1541 | 1895 | $renderingAttributes = [ |
| 1542 | 1896 | 'data-fluent-cart-product-variant' => '', |
| 1543 | 1897 | 'data-cart-id' => $variant->id, |
| 1544 | 1898 | 'data-item-stock' => $variant->isStock() ? 'in-stock' : 'out-of-stock', |
| @@ -1546,9 +1900,8 @@ | ||
| 1546 | 1900 | 'data-payment-type' => $paymentType, |
| 1547 | 1901 | 'data-available-stock' => $availableStocks, |
| 1548 | 1902 | 'data-item-price' => Helper::toDecimal($variant->item_price), |
| 1549 | 1903 | 'data-compare-price' => $comparePrice, |
| 1550 | - 'data-price-suffix' => $priceSuffix, | |
| 1551 | 1904 | 'data-stock-management' => ModuleSettings::isActive('stock_management') ? 'yes' : 'no', |
| 1552 | 1905 | 'data-sku' => $variant->sku ?? '', |
| 1553 | 1906 | 'data-package-info' => $this->getVariantPackageInfoJson($variant), |
| 1554 | 1907 | ]; |
| @@ -1594,30 +1947,29 @@ | ||
| 1594 | 1947 | if ($this->viewType === 'both' || $this->viewType === 'image') { |
| 1595 | 1948 | $this->renderVariantImage($variant); |
| 1596 | 1949 | } |
| 1597 | 1950 | ?> |
| 1598 | - <?php | |
| 1599 | - if ($this->viewType === 'both' || $this->viewType === 'text') { | |
| 1600 | - echo '<div class="fct-product-variant-title" aria-label="' . esc_attr(__('Variant title', 'fluent-cart')) . '">' . esc_html($variant->variation_title) . '</div>'; | |
| 1601 | - } | |
| 1602 | - ?> | |
| 1951 | + <?php if ($this->viewType === 'both' || $this->viewType === 'text'): ?> | |
| 1952 | + <div class="fct-product-variant-text"> | |
| 1953 | + <div class="fct-product-variant-title"><?php echo esc_html($variant->variation_title); ?></div> | |
| 1954 | + <?php if (!$this->shouldRenderPriceInPriceSection() && $paymentType === 'subscription'): ?> | |
| 1955 | + <?php $this->renderSubscriptionInfo($variant); ?> | |
| 1956 | + <?php endif; ?> | |
| 1957 | + </div> | |
| 1958 | + <?php endif; ?> | |
| 1603 | 1959 | </div> |
| 1604 | 1960 | |
| 1605 | - <?php if ($this->viewType === 'text' && $paymentType === 'subscription' && $this->columnType === 'one'): ?> | |
| 1606 | - | |
| 1607 | - <?php $this->renderSubscriptionInfo($variant); ?> | |
| 1608 | - <?php endif; ?> | |
| 1609 | - | |
| 1610 | - <?php if ($this->viewType === 'text' && $this->columnType === 'one'): ?> | |
| 1961 | + <?php if (!$this->shouldRenderPriceInPriceSection()): ?> | |
| 1611 | 1962 | <div class="fct-product-variant-price"> |
| 1612 | 1963 | <?php if ($comparePrice): ?> |
| 1613 | 1964 | <div class="fct-product-variant-compare-price"> |
| 1614 | - <del aria-label="<?php echo esc_attr(__('Original price', 'fluent-cart')); ?>"> | |
| 1965 | + <span class="fct-sr-only"><?php echo esc_html__('Original price:', 'fluent-cart'); ?></span> | |
| 1966 | + <del> | |
| 1615 | 1967 | <span><?php echo esc_html(Helper::toDecimal($comparePrice)); ?></span></del> |
| 1616 | 1968 | </div> |
| 1617 | 1969 | <?php endif; ?> |
| 1618 | - <div class="fct-product-variant-item-price" | |
| 1619 | - aria-label="<?php echo esc_attr(__('Current price', 'fluent-cart')); ?>"> | |
| 1970 | + <div class="fct-product-variant-item-price"> | |
| 1971 | + <span class="fct-sr-only"><?php echo $comparePrice ? esc_html__('Sale price:', 'fluent-cart') : esc_html__('Price:', 'fluent-cart'); ?></span> | |
| 1620 | 1972 | <span><?php echo esc_html(Helper::toDecimal($itemPrice)); ?></span> |
| 1621 | 1973 | </div> |
| 1622 | 1974 | </div> |
| 1623 | 1975 | <?php endif; ?> |
| @@ -1741,21 +2093,21 @@ | ||
| 1741 | 2093 | //Convert to collection safely before sorting |
| 1742 | 2094 | $variants = (new Collection($variants))->sortBy('serial_index')->values(); |
| 1743 | 2095 | |
| 1744 | 2096 | foreach ($variants as $variant) { |
| 1745 | - do_action('fluent_cart/product/single/before_variant_item', [ | |
| 2097 | + do_action('fluent_cart/product/single/before_variant_item', RenderContext::decorate([ | |
| 1746 | 2098 | 'product' => $this->product, |
| 1747 | 2099 | 'variant' => $variant, |
| 1748 | 2100 | 'scope' => 'product_variant_item' |
| 1749 | - ]); | |
| 2101 | + ])); | |
| 1750 | 2102 | |
| 1751 | 2103 | $this->renderVariationItem($variant, $this->defaultVariationId); |
| 1752 | 2104 | |
| 1753 | - do_action('fluent_cart/product/single/after_variant_item', [ | |
| 2105 | + do_action('fluent_cart/product/single/after_variant_item', RenderContext::decorate([ | |
| 1754 | 2106 | 'product' => $this->product, |
| 1755 | 2107 | 'variant' => $variant, |
| 1756 | 2108 | 'scope' => 'product_variant_item' |
| 1757 | - ]); | |
| 2109 | + ])); | |
| 1758 | 2110 | } |
| 1759 | 2111 | ?> |
| 1760 | 2112 | </div> |
| 1761 | 2113 | |