| 1 |
<?php |
| 2 |
/** |
| 3 |
* Woo Product Price 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 WooCommerce product price. |
| 19 |
*/ |
| 20 |
class Woo_Product_Price extends Abstract_Single_Widget |
| 21 |
{ |
| 22 |
/** |
| 23 |
* Get widget name. |
| 24 |
*/ |
| 25 |
public function get_name(): string |
| 26 |
{ |
| 27 |
return 'woo_product_price'; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Get widget title. |
| 32 |
*/ |
| 33 |
public function get_title(): string |
| 34 |
{ |
| 35 |
return esc_html__('Product Price', 'king-addons'); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Get widget icon. |
| 40 |
*/ |
| 41 |
public function get_icon(): string |
| 42 |
{ |
| 43 |
return 'king-addons-icon king-addons-woo-product-price'; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Get categories. |
| 48 |
* |
| 49 |
* @return array<int,string> |
| 50 |
*/ |
| 51 |
public function get_categories(): array |
| 52 |
{ |
| 53 |
return ['king-addons-woo-builder']; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Style dependencies. |
| 58 |
* |
| 59 |
* @return array |
| 60 |
*/ |
| 61 |
public function get_style_depends(): array |
| 62 |
{ |
| 63 |
return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-woo-product-price-style']; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Controls. |
| 68 |
*/ |
| 69 |
protected function register_controls(): void |
| 70 |
{ |
| 71 |
$this->start_controls_section( |
| 72 |
'section_content', |
| 73 |
[ |
| 74 |
'label' => esc_html__('Content', 'king-addons'), |
| 75 |
'tab' => Controls_Manager::TAB_CONTENT, |
| 76 |
] |
| 77 |
); |
| 78 |
|
| 79 |
$this->add_control( |
| 80 |
'display_mode', |
| 81 |
[ |
| 82 |
'label' => esc_html__('Display', 'king-addons'), |
| 83 |
'type' => Controls_Manager::SELECT, |
| 84 |
'options' => [ |
| 85 |
'standard' => esc_html__('Standard (WooCommerce)', 'king-addons'), |
| 86 |
'regular_only' => sprintf(__('Regular price only %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 87 |
'sale_only' => sprintf(__('Sale price only %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 88 |
], |
| 89 |
'default' => 'standard', |
| 90 |
] |
| 91 |
); |
| 92 |
|
| 93 |
$this->add_control( |
| 94 |
'show_discount', |
| 95 |
[ |
| 96 |
'label' => sprintf(__('Show discount percent %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 97 |
'type' => Controls_Manager::SWITCHER, |
| 98 |
'return_value' => 'yes', |
| 99 |
] |
| 100 |
); |
| 101 |
|
| 102 |
$this->add_control( |
| 103 |
'discount_format', |
| 104 |
[ |
| 105 |
'label' => sprintf(__('Discount format %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 106 |
'type' => Controls_Manager::TEXT, |
| 107 |
'default' => '-{percent}%', |
| 108 |
'description' => esc_html__('Use {percent} placeholder.', 'king-addons'), |
| 109 |
] |
| 110 |
); |
| 111 |
|
| 112 |
$this->end_controls_section(); |
| 113 |
|
| 114 |
$this->start_controls_section( |
| 115 |
'section_style', |
| 116 |
[ |
| 117 |
'label' => esc_html__('Style', 'king-addons'), |
| 118 |
'tab' => Controls_Manager::TAB_STYLE, |
| 119 |
] |
| 120 |
); |
| 121 |
|
| 122 |
$this->add_group_control( |
| 123 |
Group_Control_Typography::get_type(), |
| 124 |
[ |
| 125 |
'name' => 'typography', |
| 126 |
'selector' => '{{WRAPPER}} .ka-woo-product-price', |
| 127 |
] |
| 128 |
); |
| 129 |
|
| 130 |
$this->add_control( |
| 131 |
'color', |
| 132 |
[ |
| 133 |
'label' => esc_html__('Price Color', 'king-addons'), |
| 134 |
'type' => Controls_Manager::COLOR, |
| 135 |
'selectors' => [ |
| 136 |
'{{WRAPPER}} .ka-woo-product-price .price, {{WRAPPER}} .ka-woo-product-price .price .amount' => 'color: {{VALUE}};', |
| 137 |
], |
| 138 |
] |
| 139 |
); |
| 140 |
|
| 141 |
$this->add_control( |
| 142 |
'sale_color', |
| 143 |
[ |
| 144 |
'label' => esc_html__('Sale Price Color', 'king-addons'), |
| 145 |
'type' => Controls_Manager::COLOR, |
| 146 |
'selectors' => [ |
| 147 |
'{{WRAPPER}} .ka-woo-product-price .price ins .amount' => 'color: {{VALUE}};', |
| 148 |
], |
| 149 |
] |
| 150 |
); |
| 151 |
|
| 152 |
$this->add_control( |
| 153 |
'regular_color', |
| 154 |
[ |
| 155 |
'label' => esc_html__('Regular Price Color', 'king-addons'), |
| 156 |
'type' => Controls_Manager::COLOR, |
| 157 |
'selectors' => [ |
| 158 |
'{{WRAPPER}} .ka-woo-product-price .price del .amount' => 'color: {{VALUE}};', |
| 159 |
], |
| 160 |
] |
| 161 |
); |
| 162 |
|
| 163 |
$this->add_control( |
| 164 |
'discount_color', |
| 165 |
[ |
| 166 |
'label' => esc_html__('Discount Badge Color', 'king-addons'), |
| 167 |
'type' => Controls_Manager::COLOR, |
| 168 |
'selectors' => [ |
| 169 |
'{{WRAPPER}} .ka-woo-product-price__discount' => 'color: {{VALUE}}; background: rgba(0,0,0,0.04);', |
| 170 |
], |
| 171 |
'condition' => [ |
| 172 |
'show_discount' => 'yes', |
| 173 |
], |
| 174 |
] |
| 175 |
); |
| 176 |
|
| 177 |
$this->add_responsive_control( |
| 178 |
'margin', |
| 179 |
[ |
| 180 |
'label' => esc_html__('Margin', 'king-addons'), |
| 181 |
'type' => Controls_Manager::DIMENSIONS, |
| 182 |
'size_units' => ['px', 'em', '%'], |
| 183 |
'selectors' => [ |
| 184 |
'{{WRAPPER}} .ka-woo-product-price' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 185 |
], |
| 186 |
] |
| 187 |
); |
| 188 |
|
| 189 |
$this->add_responsive_control( |
| 190 |
'padding', |
| 191 |
[ |
| 192 |
'label' => esc_html__('Padding', 'king-addons'), |
| 193 |
'type' => Controls_Manager::DIMENSIONS, |
| 194 |
'size_units' => ['px', 'em', '%'], |
| 195 |
'selectors' => [ |
| 196 |
'{{WRAPPER}} .ka-woo-product-price' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 197 |
], |
| 198 |
] |
| 199 |
); |
| 200 |
|
| 201 |
$this->end_controls_section(); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Render widget. |
| 206 |
*/ |
| 207 |
protected function render(): void |
| 208 |
{ |
| 209 |
$product = $this->get_product(); |
| 210 |
if (!$product) { |
| 211 |
$this->render_missing_product_notice(); |
| 212 |
return; |
| 213 |
} |
| 214 |
|
| 215 |
$settings = $this->get_settings_for_display(); |
| 216 |
$display_mode = $settings['display_mode'] ?? 'standard'; |
| 217 |
$can_pro = king_addons_can_use_pro(); |
| 218 |
|
| 219 |
if ($display_mode !== 'standard' && !$can_pro) { |
| 220 |
$display_mode = 'standard'; |
| 221 |
} |
| 222 |
|
| 223 |
$regular_raw = $product->get_regular_price(); |
| 224 |
$sale_raw = $product->get_sale_price(); |
| 225 |
// A variable product has no price of its own - both getters return an |
| 226 |
// empty string, and the display modes below fall back to WooCommerce's |
| 227 |
// own range. The discount block reads the variation prices instead. |
| 228 |
$regular = $regular_raw !== '' ? (float) $regular_raw : null; |
| 229 |
$sale = $sale_raw !== '' ? (float) $sale_raw : null; |
| 230 |
|
| 231 |
$price_html = $product->get_price_html(); |
| 232 |
$regular_html = $regular ? wc_price($regular) : ''; |
| 233 |
$sale_html = $sale ? wc_price($sale) : ''; |
| 234 |
|
| 235 |
if ('regular_only' === $display_mode) { |
| 236 |
$price_html = $regular_html ?: $price_html; |
| 237 |
} elseif ('sale_only' === $display_mode) { |
| 238 |
$price_html = $sale_html ?: $price_html; |
| 239 |
} elseif ($product->is_on_sale() && $regular_html && $sale_html) { |
| 240 |
$price_html = '<span class="ka-woo-product-price__regular">' . $regular_html . '</span><span class="ka-woo-product-price__sale">' . $sale_html . '</span>'; |
| 241 |
} |
| 242 |
|
| 243 |
$discount_html = ''; |
| 244 |
if (!empty($settings['show_discount']) && $can_pro && $product->is_on_sale()) { |
| 245 |
$regular_for_calc = $regular; |
| 246 |
$sale_for_calc = $sale; |
| 247 |
if ($product->is_type('variable')) { |
| 248 |
$regular_for_calc = (float) $product->get_variation_regular_price('min'); |
| 249 |
$sale_for_calc = (float) $product->get_variation_sale_price('min'); |
| 250 |
} |
| 251 |
if ($regular_for_calc > 0 && $sale_for_calc > 0 && $sale_for_calc < $regular_for_calc) { |
| 252 |
$percent = max(0, round((($regular_for_calc - $sale_for_calc) / $regular_for_calc) * 100)); |
| 253 |
$format = $settings['discount_format'] ?: '-{percent}%'; |
| 254 |
$discount_html = str_replace('{percent}', (string) $percent, $format); |
| 255 |
$discount_html = '<span class="ka-woo-product-price__discount">' . esc_html($discount_html) . '</span>'; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
echo '<div class="ka-woo-product-price">'; |
| 260 |
echo wp_kses_post($price_html); |
| 261 |
echo $discount_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 262 |
echo '</div>'; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|