| 1 |
<?php |
| 2 |
/** |
| 3 |
* Woo Checkout Progress widget. |
| 4 |
* |
| 5 |
* @package King_Addons |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace King_Addons; |
| 9 |
|
| 10 |
use Elementor\Controls_Manager; |
| 11 |
use Elementor\Group_Control_Typography; |
| 12 |
|
| 13 |
if (!defined('ABSPATH')) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Displays checkout progress steps. |
| 19 |
*/ |
| 20 |
class Woo_Checkout_Progress extends Abstract_Checkout_Widget |
| 21 |
{ |
| 22 |
/** |
| 23 |
* Widget slug. |
| 24 |
* |
| 25 |
* @return string |
| 26 |
*/ |
| 27 |
public function get_name(): string |
| 28 |
{ |
| 29 |
return 'woo_checkout_progress'; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Widget title. |
| 34 |
* |
| 35 |
* @return string |
| 36 |
*/ |
| 37 |
public function get_title(): string |
| 38 |
{ |
| 39 |
return esc_html__('Checkout Progress', 'king-addons'); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Widget icon. |
| 44 |
* |
| 45 |
* @return string |
| 46 |
*/ |
| 47 |
public function get_icon(): string |
| 48 |
{ |
| 49 |
return 'eicon-progress-tracker'; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Categories. |
| 54 |
* |
| 55 |
* @return array<int, string> |
| 56 |
*/ |
| 57 |
public function get_categories(): array |
| 58 |
{ |
| 59 |
return ['king-addons-woo-builder']; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Style dependencies. |
| 64 |
* |
| 65 |
* @return array<int, string> |
| 66 |
*/ |
| 67 |
public function get_style_depends(): array |
| 68 |
{ |
| 69 |
return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-woo-checkout-progress-style']; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Register controls. |
| 74 |
* |
| 75 |
* @return void |
| 76 |
*/ |
| 77 |
protected function register_controls(): void |
| 78 |
{ |
| 79 |
$this->start_controls_section( |
| 80 |
'section_content', |
| 81 |
[ |
| 82 |
'label' => esc_html__('Content', 'king-addons'), |
| 83 |
'tab' => Controls_Manager::TAB_CONTENT, |
| 84 |
] |
| 85 |
); |
| 86 |
|
| 87 |
$this->add_control( |
| 88 |
'layout', |
| 89 |
[ |
| 90 |
'label' => sprintf(__('Layout (Pro) %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 91 |
'type' => Controls_Manager::SELECT, |
| 92 |
'options' => [ |
| 93 |
'bar' => esc_html__('Bar', 'king-addons'), |
| 94 |
'steps' => esc_html__('Steps (Pro)', 'king-addons'), |
| 95 |
], |
| 96 |
'default' => 'bar', |
| 97 |
] |
| 98 |
); |
| 99 |
|
| 100 |
$this->end_controls_section(); |
| 101 |
|
| 102 |
$this->start_controls_section( |
| 103 |
'section_style', |
| 104 |
[ |
| 105 |
'label' => esc_html__('Style', 'king-addons'), |
| 106 |
'tab' => Controls_Manager::TAB_STYLE, |
| 107 |
] |
| 108 |
); |
| 109 |
|
| 110 |
$this->add_group_control( |
| 111 |
Group_Control_Typography::get_type(), |
| 112 |
[ |
| 113 |
'name' => 'text_typography', |
| 114 |
'selector' => '{{WRAPPER}} .ka-woo-checkout-progress', |
| 115 |
] |
| 116 |
); |
| 117 |
|
| 118 |
$this->add_control( |
| 119 |
'color_base', |
| 120 |
[ |
| 121 |
'label' => esc_html__('Base Color', 'king-addons'), |
| 122 |
'type' => Controls_Manager::COLOR, |
| 123 |
'selectors' => [ |
| 124 |
'{{WRAPPER}} .ka-woo-checkout-progress' => '--ka-checkout-progress-base: {{VALUE}};', |
| 125 |
], |
| 126 |
] |
| 127 |
); |
| 128 |
|
| 129 |
$this->add_control( |
| 130 |
'color_active', |
| 131 |
[ |
| 132 |
'label' => esc_html__('Active Color', 'king-addons'), |
| 133 |
'type' => Controls_Manager::COLOR, |
| 134 |
'selectors' => [ |
| 135 |
'{{WRAPPER}} .ka-woo-checkout-progress' => '--ka-checkout-progress-active: {{VALUE}};', |
| 136 |
], |
| 137 |
] |
| 138 |
); |
| 139 |
|
| 140 |
$this->add_control( |
| 141 |
'color_complete', |
| 142 |
[ |
| 143 |
'label' => sprintf(__('Completed Color (Pro) %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 144 |
'type' => Controls_Manager::COLOR, |
| 145 |
'selectors' => [ |
| 146 |
'{{WRAPPER}} .ka-woo-checkout-progress' => '--ka-checkout-progress-complete: {{VALUE}};', |
| 147 |
], |
| 148 |
] |
| 149 |
); |
| 150 |
|
| 151 |
$this->end_controls_section(); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Render widget output. |
| 156 |
* |
| 157 |
* @return void |
| 158 |
*/ |
| 159 |
protected function render(): void |
| 160 |
{ |
| 161 |
$order_received = function_exists('is_order_received_page') && is_order_received_page(); |
| 162 |
$in_checkout = $this->should_render() || $order_received; |
| 163 |
$in_cart = function_exists('is_cart') && is_cart(); |
| 164 |
if (!$in_checkout && !$in_cart) { |
| 165 |
$this->render_missing_checkout_notice(); |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
$settings = $this->get_settings_for_display(); |
| 170 |
$can_pro = king_addons_can_use_pro(); |
| 171 |
|
| 172 |
$layout = $settings['layout'] ?? 'bar'; |
| 173 |
if ('steps' === $layout && !$can_pro) { |
| 174 |
$layout = 'bar'; |
| 175 |
} |
| 176 |
|
| 177 |
$current = 'details'; |
| 178 |
if ($in_cart) { |
| 179 |
$current = 'cart'; |
| 180 |
} |
| 181 |
if ($order_received) { |
| 182 |
$current = 'complete'; |
| 183 |
} elseif (function_exists('is_checkout_pay_page') && is_checkout_pay_page()) { |
| 184 |
$current = 'payment'; |
| 185 |
} |
| 186 |
|
| 187 |
$steps = [ |
| 188 |
['key' => 'cart', 'label' => esc_html__('Cart', 'king-addons')], |
| 189 |
['key' => 'details', 'label' => esc_html__('Details', 'king-addons')], |
| 190 |
['key' => 'payment', 'label' => esc_html__('Payment', 'king-addons')], |
| 191 |
['key' => 'complete', 'label' => esc_html__('Complete', 'king-addons')], |
| 192 |
]; |
| 193 |
|
| 194 |
echo '<div class="ka-woo-checkout-progress ka-woo-checkout-progress--' . esc_attr($layout) . '">'; |
| 195 |
|
| 196 |
if ('bar' === $layout) { |
| 197 |
$percent = 0; |
| 198 |
switch ($current) { |
| 199 |
case 'cart': |
| 200 |
$percent = 25; |
| 201 |
break; |
| 202 |
case 'details': |
| 203 |
$percent = 50; |
| 204 |
break; |
| 205 |
case 'payment': |
| 206 |
$percent = 75; |
| 207 |
break; |
| 208 |
case 'complete': |
| 209 |
$percent = 100; |
| 210 |
break; |
| 211 |
} |
| 212 |
echo '<div class="ka-woo-checkout-progress__bar"><span style="width:' . (int) $percent . '%"></span></div>'; |
| 213 |
echo '<div class="ka-woo-checkout-progress__labels">'; |
| 214 |
foreach ($steps as $step) { |
| 215 |
echo '<span class="ka-woo-checkout-progress__label">' . esc_html($step['label']) . '</span>'; |
| 216 |
} |
| 217 |
echo '</div>'; |
| 218 |
} else { |
| 219 |
echo '<div class="ka-woo-checkout-progress__steps">'; |
| 220 |
foreach ($steps as $step) { |
| 221 |
$status = 'upcoming'; |
| 222 |
if ($step['key'] === $current) { |
| 223 |
$status = 'active'; |
| 224 |
} elseif (array_search($step['key'], array_column($steps, 'key'), true) <= array_search($current, array_column($steps, 'key'), true)) { |
| 225 |
$status = 'complete'; |
| 226 |
} |
| 227 |
echo '<div class="ka-woo-checkout-progress__step is-' . esc_attr($status) . '">'; |
| 228 |
echo '<span class="ka-woo-checkout-progress__dot"></span>'; |
| 229 |
echo '<span class="ka-woo-checkout-progress__text">' . esc_html($step['label']) . '</span>'; |
| 230 |
echo '</div>'; |
| 231 |
} |
| 232 |
echo '</div>'; |
| 233 |
} |
| 234 |
|
| 235 |
echo '</div>'; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|