| 1 |
<?php |
| 2 |
/** |
| 3 |
* Woo Cart Empty Message widget. |
| 4 |
* |
| 5 |
* @package King_Addons |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace King_Addons; |
| 9 |
|
| 10 |
use Elementor\Controls_Manager; |
| 11 |
use Elementor\Widget_Base; |
| 12 |
use King_Addons\Woo_Builder\Context as Woo_Context; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Displays empty cart notice with optional button. |
| 20 |
*/ |
| 21 |
class Woo_Cart_Empty extends Widget_Base |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Widget slug. |
| 25 |
* |
| 26 |
* @return string |
| 27 |
*/ |
| 28 |
public function get_name(): string |
| 29 |
{ |
| 30 |
return 'woo_cart_empty'; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Widget title. |
| 35 |
* |
| 36 |
* @return string |
| 37 |
*/ |
| 38 |
public function get_title(): string |
| 39 |
{ |
| 40 |
return esc_html__('Cart Empty Message', 'king-addons'); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Widget icon. |
| 45 |
* |
| 46 |
* @return string |
| 47 |
*/ |
| 48 |
public function get_icon(): string |
| 49 |
{ |
| 50 |
return 'king-addons-icon king-addons-woo-cart-empty'; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Widget categories. |
| 55 |
* |
| 56 |
* @return array<int,string> |
| 57 |
*/ |
| 58 |
public function get_categories(): array |
| 59 |
{ |
| 60 |
return ['king-addons-woo-builder']; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Register controls. |
| 65 |
* |
| 66 |
* @return void |
| 67 |
*/ |
| 68 |
public function get_custom_help_url() |
| 69 |
{ |
| 70 |
return 'mailto:[email protected]?subject=Bug Report - King Addons&body=Please describe the issue'; |
| 71 |
} |
| 72 |
|
| 73 |
protected function register_controls(): void |
| 74 |
{ |
| 75 |
$this->start_controls_section( |
| 76 |
'section_content', |
| 77 |
[ |
| 78 |
'label' => esc_html__('Content', 'king-addons'), |
| 79 |
'tab' => Controls_Manager::TAB_CONTENT, |
| 80 |
] |
| 81 |
); |
| 82 |
|
| 83 |
$this->add_control( |
| 84 |
'message', |
| 85 |
[ |
| 86 |
'label' => esc_html__('Message', 'king-addons'), |
| 87 |
'type' => Controls_Manager::TEXTAREA, |
| 88 |
'dynamic' => ['active' => true], |
| 89 |
'default' => __('Your cart is currently empty.', 'king-addons'), |
| 90 |
] |
| 91 |
); |
| 92 |
|
| 93 |
$this->add_control( |
| 94 |
'show_return_shop', |
| 95 |
[ |
| 96 |
'label' => esc_html__('Show “Return to shop” button', 'king-addons'), |
| 97 |
'type' => Controls_Manager::SWITCHER, |
| 98 |
'return_value' => 'yes', |
| 99 |
'default' => 'yes', |
| 100 |
] |
| 101 |
); |
| 102 |
|
| 103 |
$this->end_controls_section(); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Render widget output. |
| 108 |
* |
| 109 |
* @return void |
| 110 |
*/ |
| 111 |
protected function render(): void |
| 112 |
{ |
| 113 |
if (!Woo_Context::maybe_render_context_notice('cart')) { |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
$in_builder = class_exists('King_Addons\\Woo_Builder\\Context') && Woo_Context::is_editing_template_type('cart'); |
| 118 |
if (!function_exists('is_cart') || (!is_cart() && !$in_builder)) { |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
$cart_empty = function_exists('WC') && WC()->cart && WC()->cart->is_empty(); |
| 123 |
if (!$cart_empty && !$in_builder) { |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
$settings = $this->get_settings_for_display(); |
| 128 |
$message = (string) ($settings['message'] ?? ''); |
| 129 |
|
| 130 |
echo '<div class="ka-woo-cart-empty">'; |
| 131 |
if ('' !== trim($message)) { |
| 132 |
// The control is a textarea, so line breaks the author typed have |
| 133 |
// to survive - printing it raw collapsed the message into one line. |
| 134 |
echo '<p>' . nl2br(esc_html($message)) . '</p>'; |
| 135 |
} |
| 136 |
|
| 137 |
if (!empty($settings['show_return_shop'])) { |
| 138 |
$shop_url = wc_get_page_permalink('shop'); |
| 139 |
if ($shop_url) { |
| 140 |
echo '<a class="button" href="' . esc_url($shop_url) . '">' . esc_html__('Return to shop', 'king-addons') . '</a>'; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
echo '</div>'; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|