| 1 |
<?php |
| 2 |
/** |
| 3 |
* Woo Checkout Sticky Sidebar 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 |
* Placeholder sticky sidebar (Pro-only rendering). |
| 20 |
*/ |
| 21 |
class Woo_Checkout_Sticky_Sidebar extends Widget_Base |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Widget slug. |
| 25 |
* |
| 26 |
* @return string |
| 27 |
*/ |
| 28 |
public function get_name(): string |
| 29 |
{ |
| 30 |
return 'woo_checkout_sticky_sidebar'; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Widget title. |
| 35 |
* |
| 36 |
* @return string |
| 37 |
*/ |
| 38 |
public function get_title(): string |
| 39 |
{ |
| 40 |
return esc_html__('Checkout Sticky Sidebar', '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-checkout-sticky-sidebar'; |
| 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 |
'title_text', |
| 85 |
[ |
| 86 |
'label' => esc_html__('Sidebar Title', 'king-addons'), |
| 87 |
'type' => Controls_Manager::TEXT, |
| 88 |
'dynamic' => ['active' => true], |
| 89 |
'default' => esc_html__('Order Summary', 'king-addons'), |
| 90 |
] |
| 91 |
); |
| 92 |
|
| 93 |
$this->add_control( |
| 94 |
'sticky_offset', |
| 95 |
[ |
| 96 |
'label' => sprintf(__('Top Offset (px) (Pro) %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 97 |
'type' => Controls_Manager::NUMBER, |
| 98 |
'default' => 20, |
| 99 |
] |
| 100 |
); |
| 101 |
|
| 102 |
$this->add_control( |
| 103 |
'disable_below', |
| 104 |
[ |
| 105 |
'label' => sprintf(__('Disable sticky below (px) (Pro) %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 106 |
'type' => Controls_Manager::NUMBER, |
| 107 |
'default' => 768, |
| 108 |
] |
| 109 |
); |
| 110 |
|
| 111 |
$this->end_controls_section(); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Render widget output (Free notice). |
| 116 |
* |
| 117 |
* @return void |
| 118 |
*/ |
| 119 |
protected function render(): void |
| 120 |
{ |
| 121 |
if (!Woo_Context::maybe_render_context_notice('checkout')) { |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
$in_builder = class_exists('King_Addons\\Woo_Builder\\Context') && Woo_Context::is_editing_template_type('checkout'); |
| 126 |
if (!function_exists('is_checkout') || (!is_checkout() && !$in_builder) || (function_exists('is_order_received_page') && is_order_received_page())) { |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
if (!function_exists('woocommerce_order_review')) { |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
// woocommerce_order_review() reaches straight into WC()->cart. There is |
| 135 |
// no cart in the Elementor editor, so the call died with |
| 136 |
// "Call to a member function get_cart() on null" and took the whole |
| 137 |
// editor preview of the checkout template down with it. |
| 138 |
if (!function_exists('WC') || !WC()->cart) { |
| 139 |
if (\Elementor\Plugin::$instance->editor->is_edit_mode()) { |
| 140 |
echo '<div class="king-addons-woo-builder-notice">' |
| 141 |
. esc_html__('The sticky sidebar shows the order review from the live cart.', 'king-addons') |
| 142 |
. '</div>'; |
| 143 |
} |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
if (!king_addons_can_use_pro()) { |
| 148 |
if (Woo_Context::is_editor()) { |
| 149 |
echo '<div class="king-addons-woo-builder-notice">'; |
| 150 |
echo esc_html__('Sticky order sidebar is available in Pro.', 'king-addons'); |
| 151 |
echo '</div>'; |
| 152 |
} |
| 153 |
return; |
| 154 |
} |
| 155 |
|
| 156 |
$settings = $this->get_settings_for_display(); |
| 157 |
// A cleared field is '' rather than unset, and (int) '' is 0 - the |
| 158 |
// sidebar then pinned to the very top and stayed sticky on phones. |
| 159 |
$offset = max(0, (int) (($settings['sticky_offset'] ?? null) ?: 20)); |
| 160 |
$disable_below = max(0, (int) (($settings['disable_below'] ?? null) ?: 768)); |
| 161 |
|
| 162 |
echo '<aside class="ka-woo-checkout-sticky" data-ka-sticky="true" data-ka-sticky-offset="' . esc_attr($offset) . '" data-ka-sticky-breakpoint="' . esc_attr($disable_below) . '">'; |
| 163 |
if (!empty($settings['title_text'])) { |
| 164 |
echo '<h4 class="ka-woo-checkout-sticky__title">' . esc_html($settings['title_text']) . '</h4>'; |
| 165 |
} |
| 166 |
woocommerce_order_review(); |
| 167 |
echo '</aside>'; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|