| 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 |
public function get_title(): string |
| 27 |
{ |
| 28 |
return esc_html__('Related Products', 'king-addons'); |
| 29 |
} |
| 30 |
|
| 31 |
public function get_icon(): string |
| 32 |
{ |
| 33 |
return 'eicon-products'; |
| 34 |
} |
| 35 |
|
| 36 |
public function get_categories(): array |
| 37 |
{ |
| 38 |
return ['king-addons-woo-builder']; |
| 39 |
} |
| 40 |
|
| 41 |
protected function register_controls(): void |
| 42 |
{ |
| 43 |
$this->start_controls_section( |
| 44 |
'section_content', |
| 45 |
[ |
| 46 |
'label' => esc_html__('Content', 'king-addons'), |
| 47 |
'tab' => Controls_Manager::TAB_CONTENT, |
| 48 |
] |
| 49 |
); |
| 50 |
|
| 51 |
$this->add_control( |
| 52 |
'products_limit', |
| 53 |
[ |
| 54 |
'label' => esc_html__('Products limit', 'king-addons'), |
| 55 |
'type' => Controls_Manager::NUMBER, |
| 56 |
'default' => 4, |
| 57 |
'min' => 1, |
| 58 |
] |
| 59 |
); |
| 60 |
|
| 61 |
$this->add_control( |
| 62 |
'columns', |
| 63 |
[ |
| 64 |
'label' => esc_html__('Columns', 'king-addons'), |
| 65 |
'type' => Controls_Manager::NUMBER, |
| 66 |
'default' => 4, |
| 67 |
'min' => 1, |
| 68 |
'max' => 6, |
| 69 |
] |
| 70 |
); |
| 71 |
|
| 72 |
$this->end_controls_section(); |
| 73 |
} |
| 74 |
|
| 75 |
protected function render(): void |
| 76 |
{ |
| 77 |
$product = $this->get_product(); |
| 78 |
if (!$product) { |
| 79 |
$this->render_missing_product_notice(); |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
$settings = $this->get_settings_for_display(); |
| 84 |
$limit = max(1, (int) ($settings['products_limit'] ?? 4)); |
| 85 |
$columns = max(1, min(6, (int) ($settings['columns'] ?? 4))); |
| 86 |
|
| 87 |
$related_ids = wc_get_related_products($product->get_id(), $limit); |
| 88 |
if (empty($related_ids)) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
$shortcode = sprintf('[products ids="%s" columns="%d"]', implode(',', $related_ids), $columns); |
| 93 |
echo '<div class="ka-woo-related">'; |
| 94 |
echo do_shortcode($shortcode); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 95 |
echo '</div>'; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|