| 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 |
|
| 9 |
class CartItemRenderer |
| 10 |
{ |
| 11 |
protected $item = []; |
| 12 |
protected $cart = null; |
| 13 |
|
| 14 |
protected $product = null; |
| 15 |
|
| 16 |
protected $variant = null; |
| 17 |
|
| 18 |
public function __construct($item = [], ?Cart $cart = null) |
| 19 |
{ |
| 20 |
$this->item = $item; |
| 21 |
$this->cart = $cart; |
| 22 |
} |
| 23 |
|
| 24 |
protected function getEventInfo() |
| 25 |
{ |
| 26 |
return [ |
| 27 |
'item' => $this->item, |
| 28 |
'cart' => $this->cart, |
| 29 |
'product' => $this->product, |
| 30 |
'variant' => $this->variant, |
| 31 |
]; |
| 32 |
} |
| 33 |
|
| 34 |
public function render() |
| 35 |
{ |
| 36 |
$wrapperClassAttributes = [ |
| 37 |
'fct_line_item', |
| 38 |
'fct_product_id_' . Arr::get($this->item, 'post_id', ''), |
| 39 |
'fct_item_id_' . Arr::get($this->item, 'id', ''), |
| 40 |
'fct_item_type_' . Arr::get($this->item, 'other_info.payment_type', ''), |
| 41 |
]; |
| 42 |
|
| 43 |
if (Arr::get($this->item, 'featured_media')) { |
| 44 |
$wrapperClassAttributes[] = 'fct_has_image'; |
| 45 |
} |
| 46 |
|
| 47 |
$promoPriceOriginal = Arr::get($this->item, 'other_info.promo_price_original', 0); |
| 48 |
if ($promoPriceOriginal && $promoPriceOriginal > $this->item['unit_price']) { |
| 49 |
$promoPriceOriginal = $promoPriceOriginal * Arr::get($this->item, 'quantity', 1); |
| 50 |
} else { |
| 51 |
$promoPriceOriginal = ''; |
| 52 |
} |
| 53 |
?> |
| 54 |
<div class="<?php $this->renderCssAtts($wrapperClassAttributes); ?>" role="listitem"> |
| 55 |
<div class="fct_line_item_info"> |
| 56 |
<?php $this->renderImage(); ?> |
| 57 |
<div class="fct_item_content"> |
| 58 |
<?php $this->renderTitle(); ?> |
| 59 |
<?php $this->renderChildVariants(); ?> |
| 60 |
<?php do_action('fluent_cart/cart/line_item/line_meta', $this->getEventInfo()); ?> |
| 61 |
</div> |
| 62 |
</div><!-- .fct_line_item_info --> |
| 63 |
|
| 64 |
<div class="fct_line_item_price" aria-label="<?php esc_attr_e('Price information', 'fluent-cart'); ?>"> |
| 65 |
<?php do_action('fluent_cart/cart/line_item/before_total', $this->getEventInfo()); ?> |
| 66 |
<?php if($promoPriceOriginal) : ?> |
| 67 |
<div style="text-decoration: line-through;" class="fct_line_item_total fct_promo_price" aria-label="<?php esc_attr_e('Original price', 'fluent-cart'); ?>"> |
| 68 |
<?php echo esc_html(Helper::toDecimal($promoPriceOriginal)); ?> |
| 69 |
</div> |
| 70 |
<?php endif; ?> |
| 71 |
<span class="fct_line_item_total" aria-label="<?php esc_attr_e('Total price', 'fluent-cart'); ?>"> |
| 72 |
<?php echo esc_html(Helper::toDecimal(Arr::get($this->item, 'subtotal', 0))); ?> |
| 73 |
</span> |
| 74 |
<?php do_action('fluent_cart/cart/line_item/after_total', $this->getEventInfo()); ?> |
| 75 |
</div><!-- .fct_line_item_price --> |
| 76 |
</div> |
| 77 |
<?php |
| 78 |
} |
| 79 |
|
| 80 |
public function renderTitle() |
| 81 |
{ |
| 82 |
$href = Arr::get($this->item, 'view_url', ''); |
| 83 |
|
| 84 |
$mainTitle = (string) Arr::get($this->item, 'post_title', ''); |
| 85 |
$subtitle = (string) Arr::get($this->item, 'title', ''); |
| 86 |
|
| 87 |
$quantity = Arr::get($this->item, 'quantity', 1); |
| 88 |
|
| 89 |
?> |
| 90 |
<div class="fct_item_title"> |
| 91 |
<?php do_action('fluent_cart/cart/line_item/before_main_title', $this->getEventInfo()); ?> |
| 92 |
<?php if ($quantity > 1): ?> |
| 93 |
<span class="fct_item_quantity" aria-label="<?php echo esc_attr(sprintf( |
| 94 |
/* translators: %d: quantity */ |
| 95 |
__('Quantity %d', 'fluent-cart'), $quantity)); ?>"> |
| 96 |
<?php echo esc_attr($quantity); ?> <span aria-hidden="true">x</span> |
| 97 |
</span> |
| 98 |
<?php endif; ?> |
| 99 |
<?php if ($href): ?> |
| 100 |
<a |
| 101 |
href="<?php echo esc_url($href); ?>" |
| 102 |
aria-label="<?php echo esc_attr(sprintf( |
| 103 |
/* translators: %s: product title */ |
| 104 |
__('View details for %s', 'fluent-cart'), $mainTitle)); ?>" |
| 105 |
> |
| 106 |
<?php echo wp_kses_post($mainTitle); ?> |
| 107 |
</a> |
| 108 |
<?php else: ?> |
| 109 |
<?php echo wp_kses_post($mainTitle); ?> |
| 110 |
<?php endif; ?> |
| 111 |
|
| 112 |
<?php if ($mainTitle != $subtitle && $subtitle): ?> |
| 113 |
<div class="fct_item_variant_title" aria-label="<?php esc_attr_e('Variant', 'fluent-cart'); ?>"> |
| 114 |
- <?php echo wp_kses_post($subtitle); ?> |
| 115 |
</div> |
| 116 |
<?php endif; ?> |
| 117 |
<?php $this->maybeRenderPaymentTypeInfo(); ?> |
| 118 |
|
| 119 |
<?php do_action('fluent_cart/cart/line_item/after_main_title', $this->getEventInfo()); ?> |
| 120 |
</div> |
| 121 |
<?php |
| 122 |
} |
| 123 |
|
| 124 |
public function renderImage() |
| 125 |
{ |
| 126 |
$image = Arr::get($this->item, 'featured_media'); |
| 127 |
if (!$image) { |
| 128 |
return; |
| 129 |
} |
| 130 |
$href = Arr::get($this->item, 'view_url', ''); |
| 131 |
$altText = sprintf( |
| 132 |
/* translators: %s: product title */ |
| 133 |
__('Image of %s', 'fluent-cart'), Arr::get($this->item, 'title', __('product', 'fluent-cart'))); |
| 134 |
?> |
| 135 |
<div class="fct_item_image"> |
| 136 |
<?php if ($href): ?> |
| 137 |
<a href="<?php echo esc_url($href); ?>"> |
| 138 |
<img src="<?php echo esc_url($image); ?>" |
| 139 |
alt="<?php echo esc_attr($altText); ?>"/> |
| 140 |
</a> |
| 141 |
<?php else: ?> |
| 142 |
<img src="<?php echo esc_url($image); ?>" |
| 143 |
alt="<?php echo esc_attr($altText); ?>"/> |
| 144 |
<?php endif; ?> |
| 145 |
</div> |
| 146 |
<?php |
| 147 |
} |
| 148 |
|
| 149 |
protected function renderCssAtts($atts) |
| 150 |
{ |
| 151 |
echo esc_attr(implode(' ', $atts)); |
| 152 |
} |
| 153 |
|
| 154 |
protected function maybeRenderPaymentTypeInfo() |
| 155 |
{ |
| 156 |
$otherInfo = Arr::get($this->item, 'other_info', []); |
| 157 |
$paymentType = Arr::get($otherInfo, 'payment_type', ''); |
| 158 |
$itemPrice = Arr::get($this->item, 'unit_price', 0); |
| 159 |
|
| 160 |
if (isset($this->item['recurring_discounts'])) { |
| 161 |
$otherInfo['recurring_discounts'] = $this->item['recurring_discounts']; |
| 162 |
} |
| 163 |
|
| 164 |
if ($paymentType === 'subscription') { |
| 165 |
$subscriptionInfo = Helper::generateSubscriptionInfo($otherInfo, $itemPrice); |
| 166 |
$setupFeeInfo = Helper::generateSetupFeeInfo($otherInfo); |
| 167 |
$trialInfo = Helper::generateTrialInfo($otherInfo); |
| 168 |
?> |
| 169 |
<div class="fct_item_payment_info"> |
| 170 |
<span class="sr-only"><?php esc_html_e('Payment information', 'fluent-cart'); ?></span> |
| 171 |
|
| 172 |
<span> <?php echo wp_kses($subscriptionInfo, ['del' => true]); ?> </span> |
| 173 |
<?php if ($trialInfo): ?> |
| 174 |
<span class="trial-days"> <?php echo esc_html($trialInfo); ?> </span> |
| 175 |
<?php endif; ?> |
| 176 |
<?php if (!empty($setupFeeInfo)): ?> |
| 177 |
<span class="setup-fee"> <?php echo esc_html($setupFeeInfo); ?> </span> |
| 178 |
<?php endif; ?> |
| 179 |
</div> |
| 180 |
<?php |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
$quantity = Arr::get($this->item, 'quantity', 1); |
| 185 |
|
| 186 |
if ($quantity < 2) { |
| 187 |
return; |
| 188 |
} |
| 189 |
?> |
| 190 |
<div class="fct_item_payment_info"> |
| 191 |
<?php |
| 192 |
/* translators: %s is the item price */ |
| 193 |
printf(esc_html__('%s each', 'fluent-cart'), esc_html(Helper::toDecimal($itemPrice))); |
| 194 |
?> |
| 195 |
</div> |
| 196 |
<?php |
| 197 |
} |
| 198 |
|
| 199 |
public function renderChildVariants(){ |
| 200 |
$childVariants = Arr::get($this->item, 'child_variants', []); |
| 201 |
|
| 202 |
if (empty($childVariants)) { |
| 203 |
return; |
| 204 |
} |
| 205 |
|
| 206 |
$total = count($childVariants); |
| 207 |
|
| 208 |
?> |
| 209 |
<div class="fct-bundle-products" data-fluent-cart-collapsibles> |
| 210 |
<h4 class="fct-bundle-products-title"> |
| 211 |
<?php echo esc_html__('Bundle of', 'fluent-cart') . ':'; ?> |
| 212 |
</h4> |
| 213 |
<div class="fct-bundle-products-list"> |
| 214 |
<?php foreach (array_slice($childVariants, 0, 2) as $childVariant): ?> |
| 215 |
<p> |
| 216 |
<?php echo esc_html(Arr::get($childVariant, 'post_title')) . ' - ' . esc_html(Arr::get($childVariant, 'variation_title')); ?> |
| 217 |
</p> |
| 218 |
<?php endforeach; ?> |
| 219 |
|
| 220 |
<?php if ($total > 2): ?> |
| 221 |
<div class="fct-bundle-products-more"> |
| 222 |
<div class="fct-bundle-products-more-list"> |
| 223 |
<?php foreach (array_slice($childVariants, 2) as $childVariant): ?> |
| 224 |
<p> |
| 225 |
<?php echo esc_html(Arr::get($childVariant, 'post_title')) . ' - ' . esc_html(Arr::get($childVariant, 'variation_title')); ?> |
| 226 |
</p> |
| 227 |
<?php endforeach; ?> |
| 228 |
</div> |
| 229 |
</div> |
| 230 |
<?php endif;?> |
| 231 |
</div> |
| 232 |
|
| 233 |
<?php if ($total > 2) : ?> |
| 234 |
<a href="#" class="fct-see-more-btn" data-fluent-cart-collapsible-toggle> |
| 235 |
<span class="see-more-text"> |
| 236 |
<?php echo esc_html__('See More', 'fluent-cart'); ?> |
| 237 |
<svg xmlns="http://www.w3.org/2000/svg" width="12" height="7" viewBox="0 0 12 7" fill="none"> |
| 238 |
<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"/> |
| 239 |
</svg> |
| 240 |
</span> |
| 241 |
<span class="see-less-text"> |
| 242 |
<?php echo esc_html__('See Less', 'fluent-cart'); ?> |
| 243 |
<svg xmlns="http://www.w3.org/2000/svg" width="12" height="7" viewBox="0 0 14 8" fill="none"> |
| 244 |
<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"/> |
| 245 |
</svg> |
| 246 |
</span> |
| 247 |
</a> |
| 248 |
<?php endif; ?> |
| 249 |
</div> |
| 250 |
<?php } |
| 251 |
|
| 252 |
} |
| 253 |
|