| @@ -1,16 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentCart\App\Services\Renderer; |
| 4 | 4 | |
| 5 | -use FluentCart\Api\CurrencySettings; | |
| 5 | + | |
| 6 | 6 | use FluentCart\Api\StoreSettings; |
| 7 | 7 | use FluentCart\App\Helpers\Helper; |
| 8 | -use FluentCart\App\Models\Product; | |
| 9 | -use FluentCart\App\Models\ProductVariation; | |
| 10 | -use FluentCart\App\Vite; | |
| 11 | 8 | use FluentCart\Framework\Support\Arr; |
| 12 | -use FluentCart\App\App; | |
| 13 | 9 | use FluentCart\Framework\Support\Str; |
| 14 | 10 | use FluentCart\App\Helpers\CurrenciesHelper; |
| 15 | 11 | use FluentCart\App\Hooks\Handlers\ShortCodes\Buttons\AddToCartShortcode; |
| 16 | 12 | use FluentCart\App\Hooks\Handlers\ShortCodes\Buttons\DirectCheckoutShortcode; |
| @@ -43,8 +39,11 @@ | ||
| 43 | 39 | |
| 44 | 40 | protected $licenseEnable = 'no'; |
| 45 | 41 | |
| 46 | 42 | |
| 43 | + protected $productPerRow = 4; | |
| 44 | + | |
| 45 | + | |
| 47 | 46 | public function __construct($viewData) |
| 48 | 47 | { |
| 49 | 48 | $this->viewData = $viewData; |
| 50 | 49 | $this->variants = $this->viewData['variants']; |
| @@ -54,10 +53,10 @@ | ||
| 54 | 53 | |
| 55 | 54 | $this->sign = CurrenciesHelper::getCurrencySign($signName); |
| 56 | 55 | |
| 57 | 56 | $this->showCartButton = Arr::get($this->viewData, 'show_cart_button', 1); |
| 58 | - | |
| 59 | - | |
| 57 | + $productPerRow = absint(Arr::get($this->viewData, 'product_per_row', 4)); | |
| 58 | + $this->productPerRow = $productPerRow > 0 ? $productPerRow : 4; | |
| 60 | 59 | } |
| 61 | 60 | |
| 62 | 61 | public function render() |
| 63 | 62 | { |
| @@ -70,9 +69,13 @@ | ||
| 70 | 69 | aria-label="<?php esc_attr_e('Pricing table', 'fluent-cart'); ?>"> |
| 71 | 70 | <?php if ($useTabs) : ?> |
| 72 | 71 | <?php $this->renderTabs($groups); ?> |
| 73 | 72 | <?php else : ?> |
| 74 | - <div class="fluent-cart-pricing-table-variants-iterator" role="list"> | |
| 73 | + <div | |
| 74 | + class="fluent-cart-pricing-table-variants-iterator" | |
| 75 | + role="list" | |
| 76 | + style="--fluent-cart-pricing-table-product-per-row: <?php echo esc_attr($this->productPerRow); ?>;" | |
| 77 | + > | |
| 75 | 78 | <?php $this->renderVariant($this->variants); ?> |
| 76 | 79 | </div> |
| 77 | 80 | <?php endif; ?> |
| 78 | 81 | </div> |
| @@ -138,9 +141,13 @@ | ||
| 138 | 141 | class="fluent-cart-pricing-table-tab-pane<?php echo $isActive ? ' active' : ''; ?>" |
| 139 | 142 | data-tab-content |
| 140 | 143 | data-active_variant="<?php echo esc_attr($currentActiveVariant); ?>" |
| 141 | 144 | role="tabpanel"> |
| 142 | - <div class="fluent-cart-pricing-table-variants-iterator" role="list"> | |
| 145 | + <div | |
| 146 | + class="fluent-cart-pricing-table-variants-iterator" | |
| 147 | + role="list" | |
| 148 | + style="--fluent-cart-pricing-table-product-per-row: <?php echo esc_attr($this->productPerRow); ?>;" | |
| 149 | + > | |
| 143 | 150 | <?php $this->renderVariant($groupVariants); ?> |
| 144 | 151 | </div> |
| 145 | 152 | </div> |
| 146 | 153 | <?php endforeach; ?> |
| @@ -163,10 +170,36 @@ | ||
| 163 | 170 | |
| 164 | 171 | return $labels[$key] ?? ucfirst(str_replace('_', ' ', $key)); |
| 165 | 172 | } |
| 166 | 173 | |
| 167 | - public function renderVariant(array $variants = null) | |
| 174 | + protected function isVariantActive($variant): bool | |
| 168 | 175 | { |
| 176 | + $groupBy = Arr::get($this->viewData, 'group_by', 'repeat_interval'); | |
| 177 | + | |
| 178 | + if ($groupBy === 'none') { | |
| 179 | + $variantGroupKey = 'onetime'; | |
| 180 | + } else { | |
| 181 | + $groupKey = $groupBy === 'payment_type' ? 'payment_type' : 'repeat_interval'; | |
| 182 | + | |
| 183 | + $variantGroupKey = Arr::get($variant, "other_info.{$groupKey}") ?: 'onetime'; | |
| 184 | + | |
| 185 | + if (Arr::get($variant, 'other_info.installment') === 'yes') { | |
| 186 | + $variantGroupKey = 'installment'; | |
| 187 | + } | |
| 188 | + } | |
| 189 | + | |
| 190 | + $activeVariantMap = Arr::get($this->viewData, 'active_variant', []); | |
| 191 | + if (!\is_array($activeVariantMap)) { | |
| 192 | + $activeVariantMap = []; | |
| 193 | + } | |
| 194 | + | |
| 195 | + $variantId = Arr::get($variant, 'id'); | |
| 196 | + | |
| 197 | + return isset($activeVariantMap[$variantGroupKey]) && $activeVariantMap[$variantGroupKey] == $variantId; | |
| 198 | + } | |
| 199 | + | |
| 200 | + public function renderVariant(?array $variants = null) | |
| 201 | + { | |
| 169 | 202 | $variants = $variants ?? $this->variants; |
| 170 | 203 | |
| 171 | 204 | foreach ($variants as $index => $variant) { |
| 172 | 205 | $this->repeatInterval = Arr::get($variant, 'other_info.repeat_interval', null); |
| @@ -190,11 +223,24 @@ | ||
| 190 | 223 | $licenseMeta = Arr::get($variant, 'product.licenses_meta.meta_value', null); |
| 191 | 224 | |
| 192 | 225 | $this->licenseEnable = Arr::get($licenseMeta, 'enabled', 'no'); |
| 193 | 226 | |
| 227 | + $isActiveVariant = $this->isVariantActive($variant); | |
| 228 | + | |
| 229 | + // Parse badge settings from viewData | |
| 230 | + $badgeData = Arr::get($this->viewData, 'badge', ''); | |
| 231 | + $badgeConfig = is_string($badgeData) ? self::stringToAssocArray($badgeData) : (is_array($badgeData) ? $badgeData : []); | |
| 232 | + $badgeText = Arr::get($badgeConfig, 'text', __('Recommended', 'fluent-cart')); | |
| 233 | + $badgePosition = Arr::get($badgeConfig, 'position', 'right'); | |
| 234 | + | |
| 194 | 235 | ?> |
| 195 | 236 | |
| 196 | - <div class="fluent-cart-pricing-table-variant" data-fluent-cart-pricing-table-variant role="listitem"> | |
| 237 | + <div class="fluent-cart-pricing-table-variant<?php echo $isActiveVariant ? ' active' : ''; ?>" data-fluent-cart-pricing-table-variant role="listitem"> | |
| 238 | + <?php if ($isActiveVariant) : ?> | |
| 239 | + <div class="fluent-cart-pricing-table-variant-badge <?php echo esc_attr($badgePosition); ?>"> | |
| 240 | + <?php echo esc_html($badgeText); ?> | |
| 241 | + </div> | |
| 242 | + <?php endif; ?> | |
| 197 | 243 | |
| 198 | 244 | <div class="fluent-cart-pricing-table-variant-contents"> |
| 199 | 245 | <div class="fluent-cart-pricing-table-variant-title"> |
| 200 | 246 | <?php echo esc_html(Arr::get($variant, 'product.post_title', '')); ?> |
| @@ -327,8 +373,9 @@ | ||
| 327 | 373 | 'product_id' => Arr::get($variant, 'id', ''), |
| 328 | 374 | 'quantity' => 1, |
| 329 | 375 | 'variation_type' => Arr::get($variant, 'variation_type', ''), |
| 330 | 376 | 'payment_type' => Arr::get($variant, 'payment_type', ''), |
| 377 | + 'variation_id' => Arr::get($variant, 'id', ''), | |
| 331 | 378 | ]; |
| 332 | 379 | |
| 333 | 380 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in view template |
| 334 | 381 | echo AddToCartShortcode::make()->enqueueAssets()->render($data); |
| @@ -404,9 +451,13 @@ | ||
| 404 | 451 | 'is_pricing_table' => true, |
| 405 | 452 | 'quantity' => 1, |
| 406 | 453 | 'variation_type' => Arr::get($variant, 'variation_type', ''), |
| 407 | 454 | 'stock_availability' => Arr::get($variant, 'stock_status', ''), |
| 455 | + 'variation_id' => Arr::get($variant, 'id', ''), | |
| 408 | 456 | ]; |
| 457 | + | |
| 458 | + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in view template | |
| 459 | + echo DirectCheckoutShortcode::make()->enqueueAssets()->render($data); | |
| 409 | 460 | } |
| 410 | 461 | } |
| 411 | 462 | |
| 412 | 463 | private static function stringToAssocArray($string): array |