Files
cannaiq/wordpress-plugin/widgets/card-template-compact.php
Kelly 0b4ed48d2f
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
feat: Add premade card templates and click analytics
WordPress Plugin v2.0.0:
- Add Promo Banner widget (dark banner with deal text)
- Add Horizontal Product Row widget (wide list format)
- Add Category Card widget (image-based categories)
- Add Compact Card widget (dense grid layout)
- Add CannaiQAnalytics click tracking (tracks add_to_cart,
  product_view, promo_click, category_click events)
- Register cannaiq-templates Elementor category
- Fix branding: CannaiQAnalytics (not CannaIQAnalytics)

Backend:
- Add POST /api/analytics/click endpoint for WordPress plugin
- Accepts API token auth, records to product_click_events table
- Stores metadata: product_name, price, category, url, referrer

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-17 02:03:28 -07:00

406 lines
14 KiB
PHP

<?php
/**
* Elementor Compact Product Card Widget
* Smaller vertical card for dense grids
*/
if (!defined('ABSPATH')) {
exit;
}
class CannaIQ_Card_Compact_Widget extends \Elementor\Widget_Base {
public function get_name() {
return 'cannaiq_card_compact';
}
public function get_title() {
return __('CannaiQ Compact Card', 'cannaiq-menus');
}
public function get_icon() {
return 'eicon-posts-grid';
}
public function get_categories() {
return ['cannaiq-templates'];
}
public function get_keywords() {
return ['cannaiq', 'product', 'compact', 'card', 'small'];
}
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(
'store_id',
[
'label' => __('Store ID', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::NUMBER,
'default' => get_option('cannaiq_default_store_id', 1),
'min' => 1,
]
);
$this->add_control(
'limit',
[
'label' => __('Number of Products', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::NUMBER,
'default' => 12,
'min' => 1,
'max' => 50,
]
);
$this->add_control(
'columns',
[
'label' => __('Columns', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::SELECT,
'default' => '4',
'options' => [
'3' => __('3 Columns', 'cannaiq-menus'),
'4' => __('4 Columns', 'cannaiq-menus'),
'5' => __('5 Columns', 'cannaiq-menus'),
'6' => __('6 Columns', 'cannaiq-menus'),
],
]
);
$this->add_control(
'category',
[
'label' => __('Category', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::SELECT,
'default' => '',
'options' => CannaIQ_Menus_Plugin::instance()->get_category_options(),
]
);
$this->add_control(
'specials_only',
[
'label' => __('Specials Only', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::SWITCHER,
'return_value' => 'yes',
'default' => 'no',
]
);
$this->end_controls_section();
// Display Options
$this->start_controls_section(
'display_section',
[
'label' => __('Display Options', 'cannaiq-menus'),
'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
]
);
$this->add_control(
'show_brand',
[
'label' => __('Show Brand', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::SWITCHER,
'return_value' => 'yes',
'default' => 'yes',
]
);
$this->add_control(
'show_thc_cbd',
[
'label' => __('Show THC/CBD', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::SWITCHER,
'return_value' => 'yes',
'default' => 'yes',
]
);
$this->add_control(
'show_discount_badge',
[
'label' => __('Show Discount Badge', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::SWITCHER,
'return_value' => 'yes',
'default' => 'yes',
]
);
$this->add_control(
'show_original_price',
[
'label' => __('Show Original Price', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::SWITCHER,
'return_value' => 'yes',
'default' => 'yes',
]
);
$this->add_control(
'show_cart_button',
[
'label' => __('Show Add to Cart', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::SWITCHER,
'return_value' => 'yes',
'default' => 'yes',
]
);
$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',
]
);
$this->add_control(
'border_color',
[
'label' => __('Border Color', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::COLOR,
'default' => '#e5e7eb',
]
);
$this->add_control(
'discount_badge_color',
[
'label' => __('Discount Badge Color', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::COLOR,
'default' => '#fbbf24',
]
);
$this->add_control(
'button_color',
[
'label' => __('Button Color', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::COLOR,
'default' => '#f97316',
]
);
$this->add_control(
'border_radius',
[
'label' => __('Border Radius', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::SLIDER,
'size_units' => ['px'],
'range' => [
'px' => [
'min' => 0,
'max' => 20,
],
],
'default' => [
'size' => 8,
],
]
);
$this->end_controls_section();
}
protected function render() {
$settings = $this->get_settings_for_display();
$args = [
'store_id' => $settings['store_id'],
'limit' => $settings['limit'],
];
if (!empty($settings['category'])) {
$args['type'] = $settings['category'];
}
$plugin = CannaIQ_Menus_Plugin::instance();
if ($settings['specials_only'] === 'yes') {
$products = $plugin->fetch_specials($args);
} else {
$products = $plugin->fetch_products($args);
}
if (!$products) {
echo '<p>' . __('No products found.', 'cannaiq-menus') . '</p>';
return;
}
$columns = $settings['columns'];
$card_bg = $settings['card_background'];
$border_color = $settings['border_color'];
$discount_color = $settings['discount_badge_color'];
$btn_color = $settings['button_color'];
$radius = $settings['border_radius']['size'] . 'px';
$col_widths = [
'3' => '33.333%',
'4' => '25%',
'5' => '20%',
'6' => '16.666%',
];
$col_width = $col_widths[$columns] ?? '25%';
?>
<div class="cannaiq-compact-grid" style="
display: flex;
flex-wrap: wrap;
gap: 16px;
margin: -8px;
">
<?php foreach ($products as $product):
$image_url = $product['image_url'] ?? $product['primary_image_url'] ?? '';
$product_url = !empty($product['menu_url']) ? $product['menu_url'] : '#';
$regular_price = $product['regular_price'] ?? $product['price_rec'] ?? 0;
$sale_price = $product['sale_price'] ?? $product['price_rec_special'] ?? $regular_price;
$has_discount = $regular_price > 0 && $sale_price < $regular_price;
$discount_percent = $has_discount ? round((($regular_price - $sale_price) / $regular_price) * 100) : 0;
$brand = $product['brand'] ?? '';
$thc = $product['thc_percentage'] ?? '';
$cbd = $product['cbd_percentage'] ?? '';
?>
<div class="cannaiq-compact-card" style="
width: calc(<?php echo esc_attr($col_width); ?> - 16px);
min-width: 140px;
background: <?php echo esc_attr($card_bg); ?>;
border: 1px solid <?php echo esc_attr($border_color); ?>;
border-radius: <?php echo esc_attr($radius); ?>;
padding: 12px;
text-align: center;
">
<?php if (!empty($image_url)): ?>
<div class="cannaiq-cc-image" style="
width: 100%;
aspect-ratio: 1;
margin-bottom: 10px;
position: relative;
">
<img src="<?php echo esc_url($image_url); ?>"
alt="<?php echo esc_attr($product['name']); ?>"
style="
width: 100%;
height: 100%;
object-fit: contain;
" />
<div style="
position: absolute;
bottom: 4px;
left: 50%;
transform: translateX(-50%);
font-size: 10px;
color: #9ca3af;
background: rgba(255,255,255,0.9);
padding: 2px 6px;
border-radius: 4px;
">Stock photo. Actual product may vary.</div>
</div>
<?php endif; ?>
<div class="cannaiq-cc-name" style="
font-weight: 600;
font-size: 13px;
line-height: 1.3;
margin-bottom: 4px;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
">
<?php echo esc_html($product['name']); ?>
</div>
<?php if ($settings['show_brand'] === 'yes' && !empty($brand)): ?>
<div class="cannaiq-cc-brand" style="
font-size: 12px;
color: #6b7280;
margin-bottom: 6px;
">
<?php echo esc_html($brand); ?>
</div>
<?php endif; ?>
<?php if ($settings['show_thc_cbd'] === 'yes' && (!empty($thc) || !empty($cbd))): ?>
<div class="cannaiq-cc-potency" style="
font-size: 11px;
color: #6b7280;
margin-bottom: 8px;
">
<?php if (!empty($thc)): ?>THC: <?php echo esc_html($thc); ?>%<?php endif; ?>
<?php if (!empty($thc) && !empty($cbd)): ?> · <?php endif; ?>
<?php if (!empty($cbd)): ?>CBD: <?php echo esc_html($cbd); ?>%<?php endif; ?>
</div>
<?php endif; ?>
<div class="cannaiq-cc-price" style="margin-bottom: 10px;">
<?php if ($settings['show_original_price'] === 'yes' && $has_discount): ?>
<div style="
text-decoration: line-through;
color: #9ca3af;
font-size: 12px;
">$<?php echo esc_html(number_format($regular_price, 2)); ?></div>
<?php endif; ?>
<div style="display: flex; align-items: center; justify-content: center; gap: 6px;">
<span style="font-size: 18px; font-weight: 700; color: #16a34a;">
$<?php echo esc_html(number_format($sale_price, 2)); ?>
</span>
<?php if ($settings['show_discount_badge'] === 'yes' && $has_discount): ?>
<span style="
background: <?php echo esc_attr($discount_color); ?>;
color: #1f2937;
font-size: 10px;
padding: 2px 6px;
border-radius: 4px;
font-weight: 600;
"><?php echo esc_html($discount_percent); ?>% off</span>
<?php endif; ?>
</div>
</div>
<?php if ($settings['show_cart_button'] === 'yes'): ?>
<a href="<?php echo esc_url($product_url); ?>"
target="_blank"
class="cannaiq-cc-button"
style="
display: block;
background: <?php echo esc_attr($btn_color); ?>;
color: white;
padding: 10px 16px;
border-radius: 6px;
text-decoration: none;
font-weight: 600;
font-size: 13px;
transition: opacity 0.2s;
">ADD TO CART</a>
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
<?php
}
}