| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\Renderer; |
| 4 |
|
| 5 |
use FluentCart\App\Helpers\Helper; |
| 6 |
use FluentCart\App\Models\Cart; |
| 7 |
use FluentCart\Framework\Support\Arr; |
| 8 |
use FluentCart\App\Vite; |
| 9 |
|
| 10 |
class CartItemRenderer |
| 11 |
{ |
| 12 |
protected $item = []; |
| 13 |
protected $cart = null; |
| 14 |
|
| 15 |
protected $product = null; |
| 16 |
|
| 17 |
protected $variant = null; |
| 18 |
|
| 19 |
public function __construct($item = [], ?Cart $cart = null) |
| 20 |
{ |
| 21 |
$this->item = $item; |
| 22 |
$this->cart = $cart; |
| 23 |
} |
| 24 |
|
| 25 |
protected function getEventInfo() |
| 26 |
{ |
| 27 |
return [ |
| 28 |
'item' => $this->item, |
| 29 |
'cart' => $this->cart, |
| 30 |
'product' => $this->product, |
| 31 |
'variant' => $this->variant, |
| 32 |
]; |
| 33 |
} |
| 34 |
|
| 35 |
public function render() |
| 36 |
{ |
| 37 |
$wrapperClassAttributes = [ |
| 38 |
'fct_line_item', |
| 39 |
'fct_product_id_' . Arr::get($this->item, 'post_id', ''), |
| 40 |
'fct_item_id_' . Arr::get($this->item, 'id', ''), |
| 41 |
'fct_item_type_' . Arr::get($this->item, 'other_info.payment_type', ''), |
| 42 |
]; |
| 43 |
|
| 44 |
if (Arr::get($this->item, 'featured_media')) { |
| 45 |
$wrapperClassAttributes[] = 'fct_has_image'; |
| 46 |
} |
| 47 |
|
| 48 |
$promoPriceOriginal = Arr::get($this->item, 'other_info.promo_price_original', 0); |
| 49 |
if ($promoPriceOriginal && $promoPriceOriginal > $this->item['unit_price']) { |
| 50 |
$promoPriceOriginal = $promoPriceOriginal * Arr::get($this->item, 'quantity', 1); |
| 51 |
} else { |
| 52 |
$promoPriceOriginal = ''; |
| 53 |
} |
| 54 |
|
| 55 |
$couponDiscount = (int) Arr::get($this->item, 'coupon_discount', 0); |
| 56 |
$lineTotal = (int) Arr::get($this->item, 'line_total', 0); |
| 57 |
$subtotal = (int) Arr::get($this->item, 'subtotal', 0); |
| 58 |
$hasCouponDiscount = $couponDiscount > 0 && $lineTotal < $subtotal; |
| 59 |
?> |
| 60 |
<div class="<?php $this->renderCssAtts($wrapperClassAttributes); ?>" role="listitem"> |
| 61 |
<div class="fct_line_item_body"> |
| 62 |
<div class="fct_line_item_info"> |
| 63 |
<?php $this->renderImage(); ?> |
| 64 |
<div class="fct_item_content"> |
| 65 |
<?php $this->renderTitle(); ?> |
| 66 |
<?php $this->renderChildVariants(); ?> |
| 67 |
<?php do_action('fluent_cart/cart/line_item/line_meta', $this->getEventInfo()); ?> |
| 68 |
</div> |
| 69 |
</div><!-- .fct_line_item_info --> |
| 70 |
|
| 71 |
<div class="fct_line_item_price" aria-label="<?php esc_attr_e('Price information', 'fluent-cart'); ?>"> |
| 72 |
<?php if($promoPriceOriginal) : ?> |
| 73 |
<div style="text-decoration: line-through;" class="fct_line_item_total fct_promo_price" aria-label="<?php esc_attr_e('Original price', 'fluent-cart'); ?>"> |
| 74 |
<?php echo esc_html(Helper::toDecimal($promoPriceOriginal)); ?> |
| 75 |
</div> |
| 76 |
<?php endif; ?> |
| 77 |
<div class="fct_item_price_wrapper"> |
| 78 |
<?php do_action('fluent_cart/cart/line_item/before_total', $this->getEventInfo()); ?> |
| 79 |
<span class="fct_line_item_total<?php echo $hasCouponDiscount ? ' fct_coupon_original_price' : ''; ?>" aria-label="<?php esc_attr_e('Total price', 'fluent-cart'); ?>"> |
| 80 |
<?php echo esc_html(Helper::toDecimal($subtotal)); ?> |
| 81 |
</span> |
| 82 |
<?php if ($hasCouponDiscount) : ?> |
| 83 |
<span class="fct_line_item_total fct_coupon_discounted_price" aria-label="<?php esc_attr_e('Discounted price', 'fluent-cart'); ?>"> |
| 84 |
<?php echo esc_html(Helper::toDecimal($lineTotal)); ?> |
| 85 |
</span> |
| 86 |
<?php endif; ?> |
| 87 |
<?php do_action('fluent_cart/cart/line_item/after_total', $this->getEventInfo()); ?> |
| 88 |
</div> |
| 89 |
</div><!-- .fct_line_item_price --> |
| 90 |
</div> |
| 91 |
<div class="fct_line_item_footer"> |
| 92 |
<?php do_action('fluent_cart/cart/line_item/footer_start', $this->getEventInfo()); ?> |
| 93 |
|
| 94 |
<?php $hasSetupFee = $this->maybeRenderSetupFeeInfo(); ?> |
| 95 |
<?php if ($hasSetupFee) : ?> |
| 96 |
<?php $this->renderSetupFeeInfo() ?> |
| 97 |
<?php do_action('fluent_cart/cart/line_item/after_setup_fee_info', $this->getEventInfo()); ?> |
| 98 |
<?php endif; ?> |
| 99 |
<?php do_action('fluent_cart/cart/line_item/footer_end', $this->getEventInfo()); ?> |
| 100 |
</div> |
| 101 |
</div> |
| 102 |
<?php |
| 103 |
} |
| 104 |
|
| 105 |
public function renderTitle() |
| 106 |
{ |
| 107 |
$href = Arr::get($this->item, 'view_url', ''); |
| 108 |
|
| 109 |
$mainTitle = (string) Arr::get($this->item, 'post_title', ''); |
| 110 |
|
| 111 |
// The Cart model already resolves & appends variation_display_title on |
| 112 |
// every cart item (Cart::getCartDataAttribute); fall back to the raw |
| 113 |
// variation title for items it didn't decorate. |
| 114 |
$subtitle = (string) Arr::get($this->item, 'variation_display_title', (string) Arr::get($this->item, 'title', '')); |
| 115 |
|
| 116 |
$quantity = Arr::get($this->item, 'quantity', 1); |
| 117 |
|
| 118 |
?> |
| 119 |
<div class="fct_item_title"> |
| 120 |
<?php do_action('fluent_cart/cart/line_item/before_main_title', $this->getEventInfo()); ?> |
| 121 |
<?php if ($quantity > 1): ?> |
| 122 |
<span class="fct_item_quantity" aria-label="<?php echo esc_attr(sprintf( |
| 123 |
/* translators: %d: quantity */ |
| 124 |
__('Quantity %d', 'fluent-cart'), $quantity)); ?>"> |
| 125 |
<?php echo esc_attr($quantity); ?> <span aria-hidden="true">x</span> |
| 126 |
</span> |
| 127 |
<?php endif; ?> |
| 128 |
<?php if ($href): ?> |
| 129 |
<a |
| 130 |
href="<?php echo esc_url($href); ?>" |
| 131 |
aria-label="<?php echo esc_attr(sprintf( |
| 132 |
/* translators: %s: product title */ |
| 133 |
__('View details for %s', 'fluent-cart'), $mainTitle)); ?>" |
| 134 |
> |
| 135 |
<?php echo wp_kses_post($mainTitle); ?> |
| 136 |
</a> |
| 137 |
<?php else: ?> |
| 138 |
<?php echo wp_kses_post($mainTitle); ?> |
| 139 |
<?php endif; ?> |
| 140 |
|
| 141 |
<?php if ($mainTitle != $subtitle && $subtitle): ?> |
| 142 |
<div class="fct_item_variant_title" aria-label="<?php esc_attr_e('Variant', 'fluent-cart'); ?>"> |
| 143 |
- <?php echo wp_kses_post($subtitle); ?> |
| 144 |
</div> |
| 145 |
<?php endif; ?> |
| 146 |
<?php $this->maybeRenderPaymentTypeInfo(); ?> |
| 147 |
<?php $this->maybeRenderPackageInfo(); ?> |
| 148 |
|
| 149 |
<?php do_action('fluent_cart/cart/line_item/after_main_title', $this->getEventInfo()); ?> |
| 150 |
</div> |
| 151 |
<?php |
| 152 |
} |
| 153 |
|
| 154 |
public function renderImage() |
| 155 |
{ |
| 156 |
$image = Arr::get($this->item, 'featured_media'); |
| 157 |
if (!$image) { |
| 158 |
$image = Vite::getAssetUrl('images/placeholder.svg'); |
| 159 |
} |
| 160 |
$href = Arr::get($this->item, 'view_url', ''); |
| 161 |
$altText = sprintf( |
| 162 |
/* translators: %s: product title */ |
| 163 |
__('Image of %s', 'fluent-cart'), Arr::get($this->item, 'title', __('product', 'fluent-cart'))); |
| 164 |
?> |
| 165 |
<div class="fct_item_image"> |
| 166 |
<?php if ($href): ?> |
| 167 |
<a href="<?php echo esc_url($href); ?>"> |
| 168 |
<img src="<?php echo esc_url($image); ?>" |
| 169 |
alt="<?php echo esc_attr($altText); ?>"/> |
| 170 |
</a> |
| 171 |
<?php else: ?> |
| 172 |
<img src="<?php echo esc_url($image); ?>" |
| 173 |
alt="<?php echo esc_attr($altText); ?>"/> |
| 174 |
<?php endif; ?> |
| 175 |
</div> |
| 176 |
<?php |
| 177 |
} |
| 178 |
|
| 179 |
protected function renderCssAtts($atts) |
| 180 |
{ |
| 181 |
echo esc_attr(implode(' ', $atts)); |
| 182 |
} |
| 183 |
|
| 184 |
protected function maybeRenderPaymentTypeInfo() |
| 185 |
{ |
| 186 |
$otherInfo = Arr::get($this->item, 'other_info', []); |
| 187 |
$paymentType = Arr::get($otherInfo, 'payment_type', ''); |
| 188 |
$itemPrice = Arr::get($this->item, 'unit_price', 0); |
| 189 |
|
| 190 |
if (isset($this->item['recurring_discounts'])) { |
| 191 |
$otherInfo['recurring_discounts'] = $this->item['recurring_discounts']; |
| 192 |
} |
| 193 |
|
| 194 |
if ($paymentType === 'subscription') { |
| 195 |
$subscriptionInfo = Helper::generateSubscriptionInfo($otherInfo, $itemPrice); |
| 196 |
$trialInfo = Helper::generateTrialInfo($otherInfo); |
| 197 |
?> |
| 198 |
<div class="fct_item_payment_info"> |
| 199 |
<span class="sr-only"><?php esc_html_e('Payment information', 'fluent-cart'); ?></span> |
| 200 |
|
| 201 |
<span> <?php echo wp_kses($subscriptionInfo, ['del' => true]); ?> </span> |
| 202 |
<?php if ($trialInfo): ?> |
| 203 |
<span class="trial-days"> <?php echo esc_html($trialInfo); ?> </span> |
| 204 |
<?php endif; ?> |
| 205 |
</div> |
| 206 |
<?php |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
$quantity = Arr::get($this->item, 'quantity', 1); |
| 211 |
|
| 212 |
if ($quantity < 2) { |
| 213 |
return; |
| 214 |
} |
| 215 |
?> |
| 216 |
<div class="fct_item_payment_info"> |
| 217 |
<?php |
| 218 |
/* translators: %1$s: formatted unit price */ |
| 219 |
printf(esc_html__('%1$s each', 'fluent-cart'), esc_html(Helper::toDecimal($itemPrice))); |
| 220 |
?> |
| 221 |
<?php do_action('fluent_cart/cart/line_item/unit_price_hint', $this->getEventInfo()); ?> |
| 222 |
</div> |
| 223 |
<?php |
| 224 |
} |
| 225 |
|
| 226 |
protected function maybeRenderSetupFeeInfo() |
| 227 |
{ |
| 228 |
$paymentType = Arr::get($this->item, 'other_info.payment_type', ''); |
| 229 |
if ($paymentType !== 'subscription') { |
| 230 |
return false; |
| 231 |
} |
| 232 |
|
| 233 |
$otherInfo = Arr::get($this->item, 'other_info', []); |
| 234 |
$setupFeeInfo = Helper::generateSetupFeeInfo($otherInfo); |
| 235 |
if (empty($setupFeeInfo)) { |
| 236 |
return false; |
| 237 |
} |
| 238 |
return true; |
| 239 |
} |
| 240 |
|
| 241 |
protected function renderSetupFeeInfo() |
| 242 |
{ |
| 243 |
|
| 244 |
$otherInfo = Arr::get($this->item, 'other_info', []); |
| 245 |
$setupFeeInfo = Helper::generateSetupFeeInfo($otherInfo, true); |
| 246 |
$setupFeeName = Arr::get($setupFeeInfo, 'signup_fee_name', ''); |
| 247 |
$setupFeePrice = Arr::get($setupFeeInfo, 'signup_fee_formatted', 0); |
| 248 |
|
| 249 |
?> |
| 250 |
<div class="fct_item_payment_info"> |
| 251 |
<div class="fct_setup_fee_info"> |
| 252 |
<span class="setup-fee"><?php esc_html_e($setupFeeName) ?></span> |
| 253 |
<span class="setup-fee-amount"> |
| 254 |
<?php esc_html_e($setupFeePrice) ?> |
| 255 |
<?php do_action('fluent_cart/cart/line_item/setup_fee_price_info', $this->getEventInfo()); ?> |
| 256 |
</span> |
| 257 |
</div> |
| 258 |
</div> |
| 259 |
<?php |
| 260 |
|
| 261 |
return true; |
| 262 |
} |
| 263 |
|
| 264 |
protected function maybeRenderPackageInfo() |
| 265 |
{ |
| 266 |
if (Arr::get($this->item, 'fulfillment_type') !== 'physical') { |
| 267 |
return; |
| 268 |
} |
| 269 |
|
| 270 |
$otherInfo = Arr::get($this->item, 'other_info', []); |
| 271 |
if (!is_array($otherInfo)) { |
| 272 |
return; |
| 273 |
} |
| 274 |
|
| 275 |
$packageSlug = Arr::get($otherInfo, 'package_slug', ''); |
| 276 |
$package = Helper::getPackageBySlug($packageSlug); |
| 277 |
if (!$package) { |
| 278 |
return; |
| 279 |
} |
| 280 |
|
| 281 |
$name = Arr::get($package, 'name', ''); |
| 282 |
$type = Arr::get($package, 'type', ''); |
| 283 |
|
| 284 |
$parts = []; |
| 285 |
if ($name) { |
| 286 |
$parts[] = $name; |
| 287 |
} |
| 288 |
|
| 289 |
$storeWeightUnit = Helper::shopConfig('weight_unit') ?: 'kg'; |
| 290 |
$productWeight = floatval(Arr::get($otherInfo, 'weight', 0)); |
| 291 |
$productWeightUnit = Arr::get($otherInfo, 'weight_unit', $storeWeightUnit); |
| 292 |
$convertedProductWeight = Helper::convertWeight($productWeight, $productWeightUnit, $storeWeightUnit); |
| 293 |
$packageWeight = floatval(Arr::get($package, 'weight', 0)); |
| 294 |
$packageWeightUnit = Arr::get($package, 'weight_unit', $storeWeightUnit); |
| 295 |
$convertedPackageWeight = Helper::convertWeight($packageWeight, $packageWeightUnit, $storeWeightUnit); |
| 296 |
$totalWeight = $convertedProductWeight + $convertedPackageWeight; |
| 297 |
|
| 298 |
if ($totalWeight) { |
| 299 |
$formatted = rtrim(rtrim(number_format($totalWeight, 2), '0'), '.'); |
| 300 |
$parts[] = $formatted . ' ' . $storeWeightUnit; |
| 301 |
} |
| 302 |
|
| 303 |
if (!$parts) { |
| 304 |
return; |
| 305 |
} |
| 306 |
|
| 307 |
$icon = ShippingMethodsRender::getPackageTypeIcon($type ?: 'box'); |
| 308 |
?> |
| 309 |
<div class="fct_item_package_info"> |
| 310 |
<?php echo $icon; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 311 |
<span><?php echo esc_html(implode(' · ', $parts)); ?></span> |
| 312 |
</div> |
| 313 |
<?php |
| 314 |
} |
| 315 |
|
| 316 |
public function renderChildVariants(){ |
| 317 |
$childVariants = Arr::get($this->item, 'child_variants', []); |
| 318 |
|
| 319 |
if (empty($childVariants)) { |
| 320 |
return; |
| 321 |
} |
| 322 |
|
| 323 |
$total = count($childVariants); |
| 324 |
|
| 325 |
?> |
| 326 |
<div class="fct-bundle-products" data-fluent-cart-collapsibles> |
| 327 |
<h4 class="fct-bundle-products-title"> |
| 328 |
<?php echo esc_html__('Bundle of', 'fluent-cart') . ':'; ?> |
| 329 |
</h4> |
| 330 |
<div class="fct-bundle-products-list"> |
| 331 |
<?php foreach (array_slice($childVariants, 0, 2) as $childVariant): ?> |
| 332 |
<p> |
| 333 |
<?php echo esc_html(Arr::get($childVariant, 'post_title')) . ' - ' . esc_html(Arr::get($childVariant, 'variation_title')); ?> |
| 334 |
</p> |
| 335 |
<?php endforeach; ?> |
| 336 |
|
| 337 |
<?php if ($total > 2): ?> |
| 338 |
<div class="fct-bundle-products-more"> |
| 339 |
<div class="fct-bundle-products-more-list"> |
| 340 |
<?php foreach (array_slice($childVariants, 2) as $childVariant): ?> |
| 341 |
<p> |
| 342 |
<?php echo esc_html(Arr::get($childVariant, 'post_title')) . ' - ' . esc_html(Arr::get($childVariant, 'variation_title')); ?> |
| 343 |
</p> |
| 344 |
<?php endforeach; ?> |
| 345 |
</div> |
| 346 |
</div> |
| 347 |
<?php endif;?> |
| 348 |
</div> |
| 349 |
|
| 350 |
<?php if ($total > 2) : ?> |
| 351 |
<a href="#" class="fct-see-more-btn" data-fluent-cart-collapsible-toggle> |
| 352 |
<span class="see-more-text"> |
| 353 |
<?php echo esc_html__('See More', 'fluent-cart'); ?> |
| 354 |
<svg xmlns="http://www.w3.org/2000/svg" width="12" height="7" viewBox="0 0 12 7" fill="none"> |
| 355 |
<path d="M0.75 0.75L5.04289 5.04289C5.37623 5.37623 5.54289 5.54289 5.75 5.54289C5.95711 5.54289 6.12377 5.37623 6.45711 5.04289L10.75 0.75" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/> |
| 356 |
</svg> |
| 357 |
</span> |
| 358 |
<span class="see-less-text"> |
| 359 |
<?php echo esc_html__('See Less', 'fluent-cart'); ?> |
| 360 |
<svg xmlns="http://www.w3.org/2000/svg" width="12" height="7" viewBox="0 0 14 8" fill="none"> |
| 361 |
<path d="M0.75 6.54297L6.04289 1.25008C6.37623 0.916742 6.54289 0.750076 6.75 0.750076C6.95711 0.750076 7.12377 0.916742 7.45711 1.25008L12.75 6.54297" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/> |
| 362 |
</svg> |
| 363 |
</span> |
| 364 |
</a> |
| 365 |
<?php endif; ?> |
| 366 |
</div> |
| 367 |
<?php } |
| 368 |
|
| 369 |
} |
| 370 |
|