| 1 |
<?php |
| 2 |
/** |
| 3 |
* Woo Product Stock 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 product stock status. |
| 19 |
*/ |
| 20 |
class Woo_Product_Stock extends Abstract_Single_Widget |
| 21 |
{ |
| 22 |
public function get_name(): string |
| 23 |
{ |
| 24 |
return 'woo_product_stock'; |
| 25 |
} |
| 26 |
|
| 27 |
public function get_title(): string |
| 28 |
{ |
| 29 |
return esc_html__('Product Stock', 'king-addons'); |
| 30 |
} |
| 31 |
|
| 32 |
public function get_icon(): string |
| 33 |
{ |
| 34 |
return 'eicon-check-circle'; |
| 35 |
} |
| 36 |
|
| 37 |
public function get_categories(): array |
| 38 |
{ |
| 39 |
return ['king-addons-woo-builder']; |
| 40 |
} |
| 41 |
|
| 42 |
public function get_style_depends(): array |
| 43 |
{ |
| 44 |
return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-woo-product-stock-style']; |
| 45 |
} |
| 46 |
|
| 47 |
protected function register_controls(): void |
| 48 |
{ |
| 49 |
$this->start_controls_section( |
| 50 |
'section_content', |
| 51 |
[ |
| 52 |
'label' => esc_html__('Content', 'king-addons'), |
| 53 |
'tab' => Controls_Manager::TAB_CONTENT, |
| 54 |
] |
| 55 |
); |
| 56 |
|
| 57 |
$this->add_control( |
| 58 |
'text_in_stock', |
| 59 |
[ |
| 60 |
'label' => esc_html__('In stock text', 'king-addons'), |
| 61 |
'type' => Controls_Manager::TEXT, |
| 62 |
'default' => esc_html__('In stock', 'king-addons'), |
| 63 |
] |
| 64 |
); |
| 65 |
|
| 66 |
$this->add_control( |
| 67 |
'text_out_stock', |
| 68 |
[ |
| 69 |
'label' => esc_html__('Out of stock text', 'king-addons'), |
| 70 |
'type' => Controls_Manager::TEXT, |
| 71 |
'default' => esc_html__('Out of stock', 'king-addons'), |
| 72 |
] |
| 73 |
); |
| 74 |
|
| 75 |
$this->add_control( |
| 76 |
'text_on_backorder', |
| 77 |
[ |
| 78 |
'label' => sprintf(__('On backorder text %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 79 |
'type' => Controls_Manager::TEXT, |
| 80 |
'default' => esc_html__('Available on backorder', 'king-addons'), |
| 81 |
] |
| 82 |
); |
| 83 |
|
| 84 |
$this->add_control( |
| 85 |
'show_quantity', |
| 86 |
[ |
| 87 |
'label' => sprintf(__('Show quantity %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 88 |
'type' => Controls_Manager::SWITCHER, |
| 89 |
'return_value' => 'yes', |
| 90 |
] |
| 91 |
); |
| 92 |
|
| 93 |
$this->add_control( |
| 94 |
'show_status_icon', |
| 95 |
[ |
| 96 |
'label' => sprintf(__('Show status icon %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 97 |
'type' => Controls_Manager::SWITCHER, |
| 98 |
'return_value' => 'yes', |
| 99 |
'default' => 'yes', |
| 100 |
] |
| 101 |
); |
| 102 |
|
| 103 |
$this->add_control( |
| 104 |
'low_stock_threshold', |
| 105 |
[ |
| 106 |
'label' => sprintf(__('Low stock threshold %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 107 |
'type' => Controls_Manager::NUMBER, |
| 108 |
'min' => 1, |
| 109 |
'description' => esc_html__('Show low stock message when below threshold.', 'king-addons'), |
| 110 |
] |
| 111 |
); |
| 112 |
|
| 113 |
$this->add_control( |
| 114 |
'text_low_stock', |
| 115 |
[ |
| 116 |
'label' => esc_html__('Low stock text (Pro)', 'king-addons'), |
| 117 |
'type' => Controls_Manager::TEXT, |
| 118 |
'default' => esc_html__('Only {qty} left', 'king-addons'), |
| 119 |
] |
| 120 |
); |
| 121 |
|
| 122 |
$this->add_control( |
| 123 |
'hide_if_in_stock', |
| 124 |
[ |
| 125 |
'label' => sprintf(__('Hide if in stock %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 126 |
'type' => Controls_Manager::SWITCHER, |
| 127 |
'return_value' => 'yes', |
| 128 |
] |
| 129 |
); |
| 130 |
|
| 131 |
$this->end_controls_section(); |
| 132 |
|
| 133 |
$this->start_controls_section( |
| 134 |
'section_style', |
| 135 |
[ |
| 136 |
'label' => esc_html__('Style', 'king-addons'), |
| 137 |
'tab' => Controls_Manager::TAB_STYLE, |
| 138 |
] |
| 139 |
); |
| 140 |
|
| 141 |
$this->add_group_control( |
| 142 |
Group_Control_Typography::get_type(), |
| 143 |
[ |
| 144 |
'name' => 'typography', |
| 145 |
'selector' => '{{WRAPPER}} .ka-woo-product-stock', |
| 146 |
] |
| 147 |
); |
| 148 |
|
| 149 |
$this->add_control( |
| 150 |
'color_in', |
| 151 |
[ |
| 152 |
'label' => esc_html__('Color: In stock', 'king-addons'), |
| 153 |
'type' => Controls_Manager::COLOR, |
| 154 |
'selectors' => [ |
| 155 |
'{{WRAPPER}} .ka-woo-product-stock--in' => 'color: {{VALUE}};', |
| 156 |
], |
| 157 |
] |
| 158 |
); |
| 159 |
|
| 160 |
$this->add_control( |
| 161 |
'color_low', |
| 162 |
[ |
| 163 |
'label' => esc_html__('Color: Low stock (Pro)', 'king-addons'), |
| 164 |
'type' => Controls_Manager::COLOR, |
| 165 |
'selectors' => [ |
| 166 |
'{{WRAPPER}} .ka-woo-product-stock--low' => 'color: {{VALUE}};', |
| 167 |
], |
| 168 |
] |
| 169 |
); |
| 170 |
|
| 171 |
$this->add_control( |
| 172 |
'color_out', |
| 173 |
[ |
| 174 |
'label' => esc_html__('Color: Out of stock', 'king-addons'), |
| 175 |
'type' => Controls_Manager::COLOR, |
| 176 |
'selectors' => [ |
| 177 |
'{{WRAPPER}} .ka-woo-product-stock--out' => 'color: {{VALUE}};', |
| 178 |
], |
| 179 |
] |
| 180 |
); |
| 181 |
|
| 182 |
$this->add_control( |
| 183 |
'color_backorder', |
| 184 |
[ |
| 185 |
'label' => sprintf(__('Color: Backorder %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 186 |
'type' => Controls_Manager::COLOR, |
| 187 |
'selectors' => [ |
| 188 |
'{{WRAPPER}} .ka-woo-product-stock--backorder' => 'color: {{VALUE}};', |
| 189 |
], |
| 190 |
] |
| 191 |
); |
| 192 |
|
| 193 |
$this->add_responsive_control( |
| 194 |
'margin', |
| 195 |
[ |
| 196 |
'label' => esc_html__('Margin', 'king-addons'), |
| 197 |
'type' => Controls_Manager::DIMENSIONS, |
| 198 |
'size_units' => ['px', 'em', '%'], |
| 199 |
'selectors' => [ |
| 200 |
'{{WRAPPER}} .ka-woo-product-stock' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 201 |
], |
| 202 |
] |
| 203 |
); |
| 204 |
|
| 205 |
$this->end_controls_section(); |
| 206 |
} |
| 207 |
|
| 208 |
protected function render(): void |
| 209 |
{ |
| 210 |
$product = $this->get_product(); |
| 211 |
if (!$product) { |
| 212 |
$this->render_missing_product_notice(); |
| 213 |
return; |
| 214 |
} |
| 215 |
|
| 216 |
$settings = $this->get_settings_for_display(); |
| 217 |
$can_pro = king_addons_can_use_pro(); |
| 218 |
|
| 219 |
$availability = $product->get_availability(); |
| 220 |
$stock_status = $availability['class'] ?? ''; |
| 221 |
|
| 222 |
$is_in_stock = $product->is_in_stock(); |
| 223 |
$qty = $product->get_stock_quantity(); |
| 224 |
$backorders = $product->get_backorders(); |
| 225 |
|
| 226 |
if (!empty($settings['hide_if_in_stock']) && $can_pro && $is_in_stock && empty($settings['low_stock_threshold'])) { |
| 227 |
return; |
| 228 |
} |
| 229 |
|
| 230 |
$text = $settings['text_in_stock'] ?? esc_html__('In stock', 'king-addons'); |
| 231 |
$class = 'ka-woo-product-stock ka-woo-product-stock--in'; |
| 232 |
$icon = ''; |
| 233 |
if (!empty($settings['show_status_icon']) && $can_pro) { |
| 234 |
$icon = '<span class="ka-woo-product-stock__icon" aria-hidden="true"></span>'; |
| 235 |
} |
| 236 |
|
| 237 |
if (!$is_in_stock) { |
| 238 |
$text = $settings['text_out_stock'] ?? esc_html__('Out of stock', 'king-addons'); |
| 239 |
$class = 'ka-woo-product-stock ka-woo-product-stock--out'; |
| 240 |
} elseif ($product->backorders_allowed() && !empty($settings['text_on_backorder']) && $can_pro) { |
| 241 |
$text = $settings['text_on_backorder']; |
| 242 |
$class = 'ka-woo-product-stock ka-woo-product-stock--backorder'; |
| 243 |
} |
| 244 |
|
| 245 |
if ($is_in_stock && $can_pro && !empty($settings['low_stock_threshold']) && is_numeric($qty)) { |
| 246 |
$threshold = (int) $settings['low_stock_threshold']; |
| 247 |
if ($threshold > 0 && $qty <= $threshold) { |
| 248 |
$low_text = $settings['text_low_stock'] ?: 'Only {qty} left'; |
| 249 |
$low_text = str_replace('{qty}', (string) $qty, $low_text); |
| 250 |
$text = $low_text; |
| 251 |
$class = 'ka-woo-product-stock ka-woo-product-stock--low'; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
if (!empty($settings['show_quantity']) && $can_pro && $is_in_stock && is_numeric($qty) && empty($settings['low_stock_threshold'])) { |
| 256 |
$text .= ' (' . (int) $qty . ')'; |
| 257 |
} |
| 258 |
|
| 259 |
echo '<div class="' . esc_attr($class) . '">' . $icon . esc_html($text) . '</div>'; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|