| 1 |
<?php |
| 2 |
/** |
| 3 |
* Woo Cart Cross-sells widget. |
| 4 |
* |
| 5 |
* @package King_Addons |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace King_Addons; |
| 9 |
|
| 10 |
use Elementor\Controls_Manager; |
| 11 |
use Elementor\Group_Control_Border; |
| 12 |
use Elementor\Group_Control_Box_Shadow; |
| 13 |
use Elementor\Group_Control_Typography; |
| 14 |
|
| 15 |
if (!defined('ABSPATH')) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Renders cart cross-sell products. |
| 21 |
*/ |
| 22 |
class Woo_Cart_Cross_Sells extends Abstract_Cart_Widget |
| 23 |
{ |
| 24 |
/** |
| 25 |
* Widget slug. |
| 26 |
* |
| 27 |
* @return string |
| 28 |
*/ |
| 29 |
public function get_name(): string |
| 30 |
{ |
| 31 |
return 'woo_cart_cross_sells'; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Widget title. |
| 36 |
* |
| 37 |
* @return string |
| 38 |
*/ |
| 39 |
public function get_title(): string |
| 40 |
{ |
| 41 |
return esc_html__('Cart Cross-sells', 'king-addons'); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Widget icon. |
| 46 |
* |
| 47 |
* @return string |
| 48 |
*/ |
| 49 |
public function get_icon(): string |
| 50 |
{ |
| 51 |
return 'eicon-product-categories'; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Categories. |
| 56 |
* |
| 57 |
* @return array<int, string> |
| 58 |
*/ |
| 59 |
public function get_categories(): array |
| 60 |
{ |
| 61 |
return ['king-addons-woo-builder']; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Style dependencies. |
| 66 |
* |
| 67 |
* @return array<int, string> |
| 68 |
*/ |
| 69 |
public function get_style_depends(): array |
| 70 |
{ |
| 71 |
return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-woo-cart-cross-sells-style']; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Script dependencies. |
| 76 |
* |
| 77 |
* @return array<int, string> |
| 78 |
*/ |
| 79 |
public function get_script_depends(): array |
| 80 |
{ |
| 81 |
return [ |
| 82 |
KING_ADDONS_ASSETS_UNIQUE_KEY . '-slick-slick', |
| 83 |
KING_ADDONS_ASSETS_UNIQUE_KEY . '-woo-cart-cross-sells-script', |
| 84 |
]; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Register controls. |
| 89 |
* |
| 90 |
* @return void |
| 91 |
*/ |
| 92 |
protected function register_controls(): void |
| 93 |
{ |
| 94 |
$this->start_controls_section( |
| 95 |
'section_content', |
| 96 |
[ |
| 97 |
'label' => esc_html__('Content', 'king-addons'), |
| 98 |
'tab' => Controls_Manager::TAB_CONTENT, |
| 99 |
] |
| 100 |
); |
| 101 |
|
| 102 |
$this->add_control( |
| 103 |
'limit', |
| 104 |
[ |
| 105 |
'label' => esc_html__('Products Limit', 'king-addons'), |
| 106 |
'type' => Controls_Manager::NUMBER, |
| 107 |
'min' => 1, |
| 108 |
'default' => 4, |
| 109 |
] |
| 110 |
); |
| 111 |
|
| 112 |
$this->add_control( |
| 113 |
'columns', |
| 114 |
[ |
| 115 |
'label' => esc_html__('Columns', 'king-addons'), |
| 116 |
'type' => Controls_Manager::NUMBER, |
| 117 |
'min' => 1, |
| 118 |
'max' => 6, |
| 119 |
'default' => 4, |
| 120 |
] |
| 121 |
); |
| 122 |
|
| 123 |
$this->add_control( |
| 124 |
'columns_tablet', |
| 125 |
[ |
| 126 |
'label' => esc_html__('Columns Tablet', 'king-addons'), |
| 127 |
'type' => Controls_Manager::NUMBER, |
| 128 |
'min' => 1, |
| 129 |
'max' => 4, |
| 130 |
'default' => 2, |
| 131 |
] |
| 132 |
); |
| 133 |
|
| 134 |
$this->add_control( |
| 135 |
'columns_mobile', |
| 136 |
[ |
| 137 |
'label' => esc_html__('Columns Mobile', 'king-addons'), |
| 138 |
'type' => Controls_Manager::NUMBER, |
| 139 |
'min' => 1, |
| 140 |
'max' => 2, |
| 141 |
'default' => 1, |
| 142 |
] |
| 143 |
); |
| 144 |
|
| 145 |
$this->add_control( |
| 146 |
'show_heading', |
| 147 |
[ |
| 148 |
'label' => esc_html__('Show heading', 'king-addons'), |
| 149 |
'type' => Controls_Manager::SWITCHER, |
| 150 |
'return_value' => 'yes', |
| 151 |
'default' => 'yes', |
| 152 |
] |
| 153 |
); |
| 154 |
|
| 155 |
$this->add_control( |
| 156 |
'heading_text', |
| 157 |
[ |
| 158 |
'label' => esc_html__('Heading text', 'king-addons'), |
| 159 |
'type' => Controls_Manager::TEXT, |
| 160 |
'default' => esc_html__('You may also like…', 'king-addons'), |
| 161 |
'condition' => [ |
| 162 |
'show_heading' => 'yes', |
| 163 |
], |
| 164 |
] |
| 165 |
); |
| 166 |
|
| 167 |
$this->add_control( |
| 168 |
'show_arrows', |
| 169 |
[ |
| 170 |
'label' => esc_html__('Show Arrows', 'king-addons'), |
| 171 |
'type' => Controls_Manager::SWITCHER, |
| 172 |
'return_value' => 'yes', |
| 173 |
'default' => 'yes', |
| 174 |
] |
| 175 |
); |
| 176 |
|
| 177 |
$this->add_control( |
| 178 |
'show_dots', |
| 179 |
[ |
| 180 |
'label' => esc_html__('Show Dots', 'king-addons'), |
| 181 |
'type' => Controls_Manager::SWITCHER, |
| 182 |
'return_value' => 'yes', |
| 183 |
'default' => '', |
| 184 |
] |
| 185 |
); |
| 186 |
|
| 187 |
$this->add_control( |
| 188 |
'loop', |
| 189 |
[ |
| 190 |
'label' => esc_html__('Loop Slides', 'king-addons'), |
| 191 |
'type' => Controls_Manager::SWITCHER, |
| 192 |
'return_value' => 'yes', |
| 193 |
'default' => '', |
| 194 |
] |
| 195 |
); |
| 196 |
|
| 197 |
$this->add_control( |
| 198 |
'autoplay', |
| 199 |
[ |
| 200 |
'label' => esc_html__('Autoplay', 'king-addons'), |
| 201 |
'type' => Controls_Manager::SWITCHER, |
| 202 |
'return_value' => 'yes', |
| 203 |
'default' => '', |
| 204 |
] |
| 205 |
); |
| 206 |
|
| 207 |
$this->add_control( |
| 208 |
'autoplay_speed', |
| 209 |
[ |
| 210 |
'label' => esc_html__('Autoplay Speed (ms)', 'king-addons'), |
| 211 |
'type' => Controls_Manager::NUMBER, |
| 212 |
'default' => 4000, |
| 213 |
'condition' => [ |
| 214 |
'autoplay' => 'yes', |
| 215 |
], |
| 216 |
] |
| 217 |
); |
| 218 |
|
| 219 |
$this->end_controls_section(); |
| 220 |
|
| 221 |
$this->start_controls_section( |
| 222 |
'section_style', |
| 223 |
[ |
| 224 |
'label' => esc_html__('Card', 'king-addons'), |
| 225 |
'tab' => Controls_Manager::TAB_STYLE, |
| 226 |
] |
| 227 |
); |
| 228 |
|
| 229 |
$this->add_group_control( |
| 230 |
Group_Control_Typography::get_type(), |
| 231 |
[ |
| 232 |
'name' => 'title_typography', |
| 233 |
'selector' => '{{WRAPPER}} .cross-sells', |
| 234 |
] |
| 235 |
); |
| 236 |
|
| 237 |
$this->add_group_control( |
| 238 |
Group_Control_Border::get_type(), |
| 239 |
[ |
| 240 |
'name' => 'card_border', |
| 241 |
'selector' => '{{WRAPPER}} .cross-sells li.product', |
| 242 |
] |
| 243 |
); |
| 244 |
|
| 245 |
$this->add_group_control( |
| 246 |
Group_Control_Box_Shadow::get_type(), |
| 247 |
[ |
| 248 |
'name' => 'card_shadow', |
| 249 |
'selector' => '{{WRAPPER}} .cross-sells li.product', |
| 250 |
] |
| 251 |
); |
| 252 |
|
| 253 |
$this->end_controls_section(); |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Render widget output. |
| 258 |
* |
| 259 |
* @return void |
| 260 |
*/ |
| 261 |
protected function render(): void |
| 262 |
{ |
| 263 |
if (!$this->should_render()) { |
| 264 |
$this->render_missing_cart_notice(); |
| 265 |
return; |
| 266 |
} |
| 267 |
|
| 268 |
if (!function_exists('WC') || !WC()->cart || WC()->cart->is_empty()) { |
| 269 |
woocommerce_output_all_notices(); |
| 270 |
wc_get_template('cart/cart-empty.php'); |
| 271 |
return; |
| 272 |
} |
| 273 |
|
| 274 |
$settings = $this->get_settings_for_display(); |
| 275 |
$limit = max(1, (int) ($settings['limit'] ?? 4)); |
| 276 |
$columns = max(1, (int) ($settings['columns'] ?? 4)); |
| 277 |
$columns_tablet = max(1, (int) ($settings['columns_tablet'] ?? 2)); |
| 278 |
$columns_mobile = max(1, (int) ($settings['columns_mobile'] ?? 1)); |
| 279 |
$show_arrows = !empty($settings['show_arrows']); |
| 280 |
$show_dots = !empty($settings['show_dots']); |
| 281 |
$loop = !empty($settings['loop']); |
| 282 |
$autoplay = !empty($settings['autoplay']); |
| 283 |
$autoplay_speed = isset($settings['autoplay_speed']) ? (int) $settings['autoplay_speed'] : 4000; |
| 284 |
$heading_text = (!empty($settings['show_heading']) && !empty($settings['heading_text'])) ? $settings['heading_text'] : ''; |
| 285 |
|
| 286 |
echo '<div class="ka-woo-cart-cross-sells" data-ka-cross-sell="1" data-cols="' . esc_attr((string) $columns) . '" data-cols-tablet="' . esc_attr((string) $columns_tablet) . '" data-cols-mobile="' . esc_attr((string) $columns_mobile) . '" data-arrows="' . ($show_arrows ? '1' : '0') . '" data-dots="' . ($show_dots ? '1' : '0') . '" data-loop="' . ($loop ? '1' : '0') . '" data-autoplay="' . ($autoplay ? '1' : '0') . '" data-autoplay-speed="' . esc_attr((string) $autoplay_speed) . '">'; |
| 287 |
if ($heading_text) { |
| 288 |
echo '<h3 class="ka-woo-cart-cross-sells__heading">' . esc_html($heading_text) . '</h3>'; |
| 289 |
} |
| 290 |
echo '<div class="ka-woo-cart-cross-sells__list">'; |
| 291 |
woocommerce_cross_sell_display($limit, $columns); |
| 292 |
echo '</div>'; |
| 293 |
echo '</div>'; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|