| 1 |
<?php |
| 2 |
/** |
| 3 |
* Woo Product Related widget. |
| 4 |
* |
| 5 |
* @package King_Addons |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace King_Addons; |
| 9 |
|
| 10 |
use Elementor\Controls_Manager; |
| 11 |
|
| 12 |
if (!defined('ABSPATH')) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Displays related products grid. |
| 18 |
*/ |
| 19 |
class Woo_Product_Related extends Abstract_Single_Widget |
| 20 |
{ |
| 21 |
public function get_name(): string |
| 22 |
{ |
| 23 |
return 'woo_product_related'; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Style dependencies. |
| 28 |
* |
| 29 |
* @return array<int, string> |
| 30 |
*/ |
| 31 |
public function get_style_depends(): array |
| 32 |
{ |
| 33 |
return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-woo-loop-style']; |
| 34 |
} |
| 35 |
|
| 36 |
public function get_title(): string |
| 37 |
{ |
| 38 |
return esc_html__('Related Products', 'king-addons'); |
| 39 |
} |
| 40 |
|
| 41 |
public function get_icon(): string |
| 42 |
{ |
| 43 |
return 'king-addons-icon king-addons-woo-product-related'; |
| 44 |
} |
| 45 |
|
| 46 |
public function get_categories(): array |
| 47 |
{ |
| 48 |
return ['king-addons-woo-builder']; |
| 49 |
} |
| 50 |
|
| 51 |
protected function register_controls(): void |
| 52 |
{ |
| 53 |
$this->start_controls_section( |
| 54 |
'section_content', |
| 55 |
[ |
| 56 |
'label' => esc_html__('Content', 'king-addons'), |
| 57 |
'tab' => Controls_Manager::TAB_CONTENT, |
| 58 |
] |
| 59 |
); |
| 60 |
|
| 61 |
$this->add_control( |
| 62 |
'products_limit', |
| 63 |
[ |
| 64 |
'label' => esc_html__('Products limit', 'king-addons'), |
| 65 |
'type' => Controls_Manager::NUMBER, |
| 66 |
'default' => 4, |
| 67 |
'min' => 1, |
| 68 |
] |
| 69 |
); |
| 70 |
|
| 71 |
$this->add_control( |
| 72 |
'columns', |
| 73 |
[ |
| 74 |
'label' => esc_html__('Columns', 'king-addons'), |
| 75 |
'type' => Controls_Manager::NUMBER, |
| 76 |
'default' => 4, |
| 77 |
'min' => 1, |
| 78 |
'max' => 6, |
| 79 |
] |
| 80 |
); |
| 81 |
|
| 82 |
$this->end_controls_section(); |
| 83 |
} |
| 84 |
|
| 85 |
protected function render(): void |
| 86 |
{ |
| 87 |
$product = $this->get_product(); |
| 88 |
if (!$product) { |
| 89 |
$this->render_missing_product_notice(); |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
$settings = $this->get_settings_for_display(); |
| 94 |
// ?: not ??: a cleared number field is '' rather than null, and |
| 95 |
// (int) '' is 0 - both settings silently collapsed to one. |
| 96 |
$limit = max(1, (int) (($settings['products_limit'] ?? null) ?: 4)); |
| 97 |
$columns = max(1, min(6, (int) (($settings['columns'] ?? null) ?: 4))); |
| 98 |
|
| 99 |
$related_ids = wc_get_related_products($product->get_id(), $limit); |
| 100 |
if (empty($related_ids)) { |
| 101 |
// Related products depend on shared categories or tags; an empty |
| 102 |
// result in the editor should say so rather than draw nothing. |
| 103 |
if (\Elementor\Plugin::$instance->editor->is_edit_mode()) { |
| 104 |
echo '<div class="king-addons-woo-builder-notice">' |
| 105 |
. esc_html__('No related products found - they come from products sharing a category or tag.', 'king-addons') |
| 106 |
. '</div>'; |
| 107 |
} |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
$shortcode = sprintf('[products ids="%s" columns="%d"]', implode(',', $related_ids), $columns); |
| 112 |
echo '<div class="ka-woo-related">'; |
| 113 |
echo do_shortcode($shortcode); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 114 |
echo '</div>'; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|