Files
cannaiq/wordpress-plugin/widgets/brand-grid.php
Kelly 74957a9ec5 feat(wordpress): Add new Elementor widgets and dynamic selectors v1.6.0
New Widgets:
- Brand Grid: Display brands in a grid with product counts
- Category List: Show categories in grid/list/pills layouts
- Specials Grid: Display products on sale with discount badges

Enhanced Product Grid Widget:
- Dynamic category dropdown (fetches from API)
- Dynamic brand dropdown (fetches from API)
- "On Special Only" toggle filter

New Plugin Methods:
- fetch_categories() - Get categories from API
- fetch_brands() - Get brands from API
- fetch_specials() - Get products on sale
- get_category_options() - Cached options for Elementor
- get_brand_options() - Cached options for Elementor

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-10 13:41:17 -07:00

185 lines
6.0 KiB
PHP

<?php
/**
* Elementor Brand Grid Widget
*/
if (!defined('ABSPATH')) {
exit;
}
class CannaIQ_Menus_Brand_Grid_Widget extends \Elementor\Widget_Base {
public function get_name() {
return 'cannaiq_brand_grid';
}
public function get_title() {
return __('CannaIQ Brand Grid', 'cannaiq-menus');
}
public function get_icon() {
return 'eicon-gallery-grid';
}
public function get_categories() {
return ['general'];
}
protected function register_controls() {
// Content Section
$this->start_controls_section(
'content_section',
[
'label' => __('Content', 'cannaiq-menus'),
'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
]
);
$this->add_control(
'limit',
[
'label' => __('Number of Brands', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::NUMBER,
'default' => 12,
'min' => 1,
'max' => 100,
]
);
$this->add_control(
'columns',
[
'label' => __('Columns', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::SELECT,
'default' => '4',
'options' => [
'2' => __('2 Columns', 'cannaiq-menus'),
'3' => __('3 Columns', 'cannaiq-menus'),
'4' => __('4 Columns', 'cannaiq-menus'),
'6' => __('6 Columns', 'cannaiq-menus'),
],
]
);
$this->add_control(
'show_product_count',
[
'label' => __('Show Product Count', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::SWITCHER,
'label_on' => __('Yes', 'cannaiq-menus'),
'label_off' => __('No', 'cannaiq-menus'),
'return_value' => 'yes',
'default' => 'yes',
]
);
$this->add_control(
'link_to_products',
[
'label' => __('Link to Products Page', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::URL,
'placeholder' => __('/products', 'cannaiq-menus'),
'description' => __('Brand name will be appended as ?brand=Name', 'cannaiq-menus'),
]
);
$this->end_controls_section();
// Style Section
$this->start_controls_section(
'style_section',
[
'label' => __('Style', 'cannaiq-menus'),
'tab' => \Elementor\Controls_Manager::TAB_STYLE,
]
);
$this->add_control(
'card_background',
[
'label' => __('Card Background', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::COLOR,
'default' => '#ffffff',
'selectors' => [
'{{WRAPPER}} .cannaiq-brand-card' => 'background-color: {{VALUE}};',
],
]
);
$this->add_control(
'card_border_radius',
[
'label' => __('Border Radius', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::SLIDER,
'size_units' => ['px'],
'range' => [
'px' => [
'min' => 0,
'max' => 50,
],
],
'default' => [
'size' => 8,
],
'selectors' => [
'{{WRAPPER}} .cannaiq-brand-card' => 'border-radius: {{SIZE}}{{UNIT}};',
],
]
);
$this->add_control(
'text_color',
[
'label' => __('Text Color', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::COLOR,
'default' => '#333333',
'selectors' => [
'{{WRAPPER}} .cannaiq-brand-card' => 'color: {{VALUE}};',
],
]
);
$this->end_controls_section();
}
protected function render() {
$settings = $this->get_settings_for_display();
$plugin = CannaIQ_Menus_Plugin::instance();
$brands = $plugin->fetch_brands(['limit' => $settings['limit']]);
if (!$brands) {
echo '<p>' . __('No brands found.', 'cannaiq-menus') . '</p>';
return;
}
$columns = $settings['columns'];
$link_base = $settings['link_to_products']['url'] ?? '';
?>
<div class="cannaiq-brand-grid cannaiq-grid-cols-<?php echo esc_attr($columns); ?>">
<?php foreach ($brands as $brand):
$brand_name = $brand['brand'] ?? $brand['brand_name'] ?? '';
$product_count = $brand['product_count'] ?? 0;
$brand_url = $link_base ? $link_base . '?brand=' . urlencode($brand_name) : '#';
?>
<div class="cannaiq-brand-card"
<?php if ($brand_url !== '#'): ?>onclick="window.location.href='<?php echo esc_url($brand_url); ?>'"<?php endif; ?>
style="cursor: <?php echo ($brand_url !== '#') ? 'pointer' : 'default'; ?>;">
<div class="cannaiq-brand-content">
<h3 class="cannaiq-brand-name">
<?php echo esc_html($brand_name); ?>
</h3>
<?php if ($settings['show_product_count'] === 'yes' && $product_count > 0): ?>
<span class="cannaiq-brand-count">
<?php echo esc_html($product_count); ?> <?php _e('products', 'cannaiq-menus'); ?>
</span>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
</div>
<?php
}
}