| 1 |
<?php |
| 2 |
/** |
| 3 |
* Woo Checkout Order Summary widget. |
| 4 |
* |
| 5 |
* @package King_Addons |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace King_Addons; |
| 9 |
|
| 10 |
use Elementor\Controls_Manager; |
| 11 |
use Elementor\Group_Control_Border; |
| 12 |
use Elementor\Group_Control_Box_Shadow; |
| 13 |
use Elementor\Group_Control_Typography; |
| 14 |
|
| 15 |
if (!defined('ABSPATH')) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Displays checkout order review section. |
| 21 |
*/ |
| 22 |
class Woo_Checkout_Order_Summary extends Abstract_Checkout_Widget |
| 23 |
{ |
| 24 |
/** |
| 25 |
* Widget slug. |
| 26 |
* |
| 27 |
* @return string |
| 28 |
*/ |
| 29 |
public function get_name(): string |
| 30 |
{ |
| 31 |
return 'woo_checkout_order_summary'; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Widget title. |
| 36 |
* |
| 37 |
* @return string |
| 38 |
*/ |
| 39 |
public function get_title(): string |
| 40 |
{ |
| 41 |
return esc_html__('Checkout Order Summary', 'king-addons'); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Widget icon. |
| 46 |
* |
| 47 |
* @return string |
| 48 |
*/ |
| 49 |
public function get_icon(): string |
| 50 |
{ |
| 51 |
return 'eicon-order-review'; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Categories. |
| 56 |
* |
| 57 |
* @return array<int, string> |
| 58 |
*/ |
| 59 |
public function get_categories(): array |
| 60 |
{ |
| 61 |
return ['king-addons-woo-builder']; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Style dependencies. |
| 66 |
* |
| 67 |
* @return array<int, string> |
| 68 |
*/ |
| 69 |
public function get_style_depends(): array |
| 70 |
{ |
| 71 |
return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-woo-checkout-order-summary-style']; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Register controls. |
| 76 |
* |
| 77 |
* @return void |
| 78 |
*/ |
| 79 |
protected function register_controls(): void |
| 80 |
{ |
| 81 |
$this->start_controls_section( |
| 82 |
'section_content', |
| 83 |
[ |
| 84 |
'label' => esc_html__('Content', 'king-addons'), |
| 85 |
'tab' => Controls_Manager::TAB_CONTENT, |
| 86 |
] |
| 87 |
); |
| 88 |
|
| 89 |
$this->add_control( |
| 90 |
'show_thumbnails', |
| 91 |
[ |
| 92 |
'label' => sprintf(__('Show product thumbnails %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 93 |
'type' => Controls_Manager::SWITCHER, |
| 94 |
'return_value' => 'yes', |
| 95 |
] |
| 96 |
); |
| 97 |
|
| 98 |
$this->add_control( |
| 99 |
'show_sku', |
| 100 |
[ |
| 101 |
'label' => sprintf(__('Show SKU %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 102 |
'type' => Controls_Manager::SWITCHER, |
| 103 |
'return_value' => 'yes', |
| 104 |
] |
| 105 |
); |
| 106 |
|
| 107 |
$this->add_control( |
| 108 |
'show_meta', |
| 109 |
[ |
| 110 |
'label' => sprintf(__('Show item meta %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 111 |
'type' => Controls_Manager::SWITCHER, |
| 112 |
'return_value' => 'yes', |
| 113 |
] |
| 114 |
); |
| 115 |
|
| 116 |
$this->add_control( |
| 117 |
'sticky', |
| 118 |
[ |
| 119 |
'label' => sprintf(__('Make Sticky (Pro) %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 120 |
'type' => Controls_Manager::SWITCHER, |
| 121 |
'return_value' => 'yes', |
| 122 |
] |
| 123 |
); |
| 124 |
|
| 125 |
$this->add_control( |
| 126 |
'sticky_offset', |
| 127 |
[ |
| 128 |
'label' => sprintf(__('Sticky Offset (px) (Pro) %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 129 |
'type' => Controls_Manager::NUMBER, |
| 130 |
'default' => 20, |
| 131 |
'condition' => [ |
| 132 |
'sticky' => 'yes', |
| 133 |
], |
| 134 |
] |
| 135 |
); |
| 136 |
|
| 137 |
$this->add_control( |
| 138 |
'sticky_breakpoint', |
| 139 |
[ |
| 140 |
'label' => sprintf(__('Sticky Breakpoint (px) (Pro) %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 141 |
'type' => Controls_Manager::NUMBER, |
| 142 |
'default' => 768, |
| 143 |
'condition' => [ |
| 144 |
'sticky' => 'yes', |
| 145 |
], |
| 146 |
] |
| 147 |
); |
| 148 |
|
| 149 |
$this->end_controls_section(); |
| 150 |
|
| 151 |
$this->start_controls_section( |
| 152 |
'section_style', |
| 153 |
[ |
| 154 |
'label' => esc_html__('Box', 'king-addons'), |
| 155 |
'tab' => Controls_Manager::TAB_STYLE, |
| 156 |
] |
| 157 |
); |
| 158 |
|
| 159 |
$this->add_group_control( |
| 160 |
Group_Control_Typography::get_type(), |
| 161 |
[ |
| 162 |
'name' => 'text_typography', |
| 163 |
'selector' => '{{WRAPPER}} .ka-woo-checkout-summary', |
| 164 |
] |
| 165 |
); |
| 166 |
|
| 167 |
$this->add_control( |
| 168 |
'text_color', |
| 169 |
[ |
| 170 |
'label' => esc_html__('Text Color', 'king-addons'), |
| 171 |
'type' => Controls_Manager::COLOR, |
| 172 |
'selectors' => [ |
| 173 |
'{{WRAPPER}} .ka-woo-checkout-summary' => 'color: {{VALUE}};', |
| 174 |
], |
| 175 |
] |
| 176 |
); |
| 177 |
|
| 178 |
$this->add_group_control( |
| 179 |
Group_Control_Border::get_type(), |
| 180 |
[ |
| 181 |
'name' => 'box_border', |
| 182 |
'selector' => '{{WRAPPER}} .ka-woo-checkout-summary', |
| 183 |
] |
| 184 |
); |
| 185 |
|
| 186 |
$this->add_group_control( |
| 187 |
Group_Control_Box_Shadow::get_type(), |
| 188 |
[ |
| 189 |
'name' => 'box_shadow', |
| 190 |
'selector' => '{{WRAPPER}} .ka-woo-checkout-summary', |
| 191 |
] |
| 192 |
); |
| 193 |
|
| 194 |
$this->add_control( |
| 195 |
'box_padding', |
| 196 |
[ |
| 197 |
'label' => esc_html__('Padding', 'king-addons'), |
| 198 |
'type' => Controls_Manager::DIMENSIONS, |
| 199 |
'size_units' => ['px', 'em', '%'], |
| 200 |
'selectors' => [ |
| 201 |
'{{WRAPPER}} .ka-woo-checkout-summary' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 202 |
], |
| 203 |
] |
| 204 |
); |
| 205 |
|
| 206 |
$this->end_controls_section(); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Render widget output. |
| 211 |
* |
| 212 |
* @return void |
| 213 |
*/ |
| 214 |
protected function render(): void |
| 215 |
{ |
| 216 |
if (!$this->should_render()) { |
| 217 |
$this->render_missing_checkout_notice(); |
| 218 |
return; |
| 219 |
} |
| 220 |
|
| 221 |
if (!function_exists('WC')) { |
| 222 |
return; |
| 223 |
} |
| 224 |
|
| 225 |
$settings = $this->get_settings_for_display(); |
| 226 |
$can_pro = king_addons_can_use_pro(); |
| 227 |
|
| 228 |
$this->add_render_attribute('summary', 'class', 'ka-woo-checkout-summary'); |
| 229 |
|
| 230 |
$show_thumbs = !empty($settings['show_thumbnails']) && $can_pro; |
| 231 |
$show_sku = !empty($settings['show_sku']) && $can_pro; |
| 232 |
$show_meta = !empty($settings['show_meta']) && $can_pro; |
| 233 |
|
| 234 |
if (!empty($settings['sticky']) && $can_pro) { |
| 235 |
$offset = isset($settings['sticky_offset']) ? (int) $settings['sticky_offset'] : 20; |
| 236 |
$this->add_render_attribute('summary', 'class', 'ka-woo-checkout-summary--sticky'); |
| 237 |
$this->add_render_attribute('summary', 'data-ka-sticky', 'true'); |
| 238 |
$this->add_render_attribute('summary', 'data-ka-sticky-offset', (int) $offset); |
| 239 |
$breakpoint = isset($settings['sticky_breakpoint']) ? (int) $settings['sticky_breakpoint'] : 768; |
| 240 |
$this->add_render_attribute('summary', 'data-ka-sticky-breakpoint', (int) $breakpoint); |
| 241 |
$this->add_render_attribute('summary', 'style', 'position: sticky; top: ' . $offset . 'px;'); |
| 242 |
} |
| 243 |
|
| 244 |
echo '<div ' . $this->get_render_attribute_string('summary') . '>'; |
| 245 |
$cart = WC()->cart; |
| 246 |
if ($cart) { |
| 247 |
echo '<div class="ka-woo-checkout-summary__items">'; |
| 248 |
foreach ($cart->get_cart() as $cart_item_key => $cart_item) { |
| 249 |
$product = apply_filters('woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key); |
| 250 |
if (!$product || !$product->exists()) { |
| 251 |
continue; |
| 252 |
} |
| 253 |
$product_id = $product->get_id(); |
| 254 |
$name = $product->get_name(); |
| 255 |
$qty = $cart_item['quantity']; |
| 256 |
$line_total = $cart->get_product_subtotal($product, $qty); |
| 257 |
$thumbnail = $show_thumbs ? $product->get_image('thumbnail') : ''; |
| 258 |
$sku = $show_sku ? $product->get_sku() : ''; |
| 259 |
$meta = $show_meta ? wc_get_formatted_cart_item_data($cart_item, false) : ''; |
| 260 |
|
| 261 |
echo '<div class="ka-woo-checkout-summary__item">'; |
| 262 |
if ($thumbnail) { |
| 263 |
echo '<div class="ka-woo-checkout-summary__thumb">' . $thumbnail . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 264 |
} |
| 265 |
echo '<div class="ka-woo-checkout-summary__body">'; |
| 266 |
echo '<div class="ka-woo-checkout-summary__title">' . esc_html($name) . '</div>'; |
| 267 |
if ($sku) { |
| 268 |
echo '<div class="ka-woo-checkout-summary__meta ka-woo-checkout-summary__meta--sku">' . esc_html__('SKU:', 'king-addons') . ' ' . esc_html($sku) . '</div>'; |
| 269 |
} |
| 270 |
if ($meta) { |
| 271 |
echo '<div class="ka-woo-checkout-summary__meta">' . wp_kses_post($meta) . '</div>'; |
| 272 |
} |
| 273 |
echo '<div class="ka-woo-checkout-summary__qty">' . esc_html__('Qty:', 'king-addons') . ' ' . esc_html($qty) . '</div>'; |
| 274 |
echo '</div>'; |
| 275 |
echo '<div class="ka-woo-checkout-summary__price">' . wp_kses_post($line_total) . '</div>'; |
| 276 |
echo '</div>'; |
| 277 |
} |
| 278 |
echo '</div>'; |
| 279 |
|
| 280 |
echo '<div class="ka-woo-checkout-summary__totals">'; |
| 281 |
echo '<div class="ka-woo-checkout-summary__totals-row"><span>' . esc_html__('Subtotal', 'king-addons') . '</span><span>' . wp_kses_post($cart->get_cart_subtotal()) . '</span></div>'; |
| 282 |
|
| 283 |
foreach ($cart->get_coupons() as $code => $coupon) { |
| 284 |
$amount_html = '-' . wc_price($coupon->get_amount()); |
| 285 |
echo '<div class="ka-woo-checkout-summary__totals-row"><span>' . esc_html__('Coupon', 'king-addons') . ' (' . esc_html($code) . ')</span><span>' . wp_kses_post($amount_html) . '</span></div>'; |
| 286 |
} |
| 287 |
|
| 288 |
if ($cart->needs_shipping() && $cart->show_shipping()) { |
| 289 |
$packages = WC()->shipping()->get_packages(); |
| 290 |
WC()->shipping()->calculate_shipping($packages); |
| 291 |
$rates = $packages[0]['rates'] ?? []; |
| 292 |
$chosen = WC()->session ? WC()->session->get('chosen_shipping_methods') : []; |
| 293 |
$chosen_id = $chosen[0] ?? ''; |
| 294 |
foreach ($rates as $rate) { |
| 295 |
if ($chosen_id && $rate->id !== $chosen_id) { |
| 296 |
continue; |
| 297 |
} |
| 298 |
echo '<div class="ka-woo-checkout-summary__totals-row"><span>' . esc_html__('Shipping', 'king-addons') . '</span><span>' . wp_kses_post(wc_price($rate->get_cost())) . '</span></div>'; |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
foreach ($cart->get_fees() as $fee) { |
| 303 |
echo '<div class="ka-woo-checkout-summary__totals-row"><span>' . esc_html($fee->name) . '</span><span>' . wp_kses_post(wc_price($fee->amount)) . '</span></div>'; |
| 304 |
} |
| 305 |
|
| 306 |
if ('excl' === $cart->get_tax_price_display_mode()) { |
| 307 |
$tax_totals = $cart->get_tax_totals(); |
| 308 |
if (!empty($tax_totals)) { |
| 309 |
foreach ($tax_totals as $code => $tax) { |
| 310 |
echo '<div class="ka-woo-checkout-summary__totals-row"><span>' . esc_html($tax->label) . '</span><span>' . wp_kses_post($tax->formatted_amount) . '</span></div>'; |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
echo '<div class="ka-woo-checkout-summary__totals-row ka-woo-checkout-summary__totals-row--total"><span>' . esc_html__('Total', 'king-addons') . '</span><span>' . wp_kses_post($cart->get_total()) . '</span></div>'; |
| 316 |
echo '</div>'; |
| 317 |
} |
| 318 |
echo '</div>'; |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|