Files
cannaiq/wordpress-plugin/widgets/single-product.php
Kelly 948a732dd5 feat: Rename WordPress plugin to CannaIQ Menus v1.5.3
- Rename plugin from Crawlsy Menus to CannaIQ Menus
- Update version to 1.5.3
- Update text domain to cannaiq-menus
- Rename all CSS classes from crawlsy-* to cannaiq-*
- Update shortcodes to [cannaiq_products] and [cannaiq_product]
- Add backward compatibility for legacy shortcodes
- Update download links on Home and LandingPage
- Fix health panel Redis timeout issue
- Add clear error message when backend not running

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-08 00:24:43 -07:00

369 lines
13 KiB
PHP

<?php
/**
* Elementor Single Product Widget
*/
if (!defined('ABSPATH')) {
exit;
}
class CannaIQ_Menus_Single_Product_Widget extends \Elementor\Widget_Base {
public function get_name() {
return 'cannaiq_single_product';
}
public function get_title() {
return __('CannaIQ Single Product', 'cannaiq-menus');
}
public function get_icon() {
return 'eicon-single-product';
}
public function get_categories() {
return ['general'];
}
/**
* Get products for the SELECT2 dropdown
*
* @return array Associative array of product_id => product_name
*/
protected function get_products_for_select() {
$options = ['' => __('-- Select a Product --', 'cannaiq-menus')];
$plugin = CannaIQ_Menus_Plugin::instance();
$products = $plugin->fetch_products(['limit' => 500]);
if ($products && is_array($products)) {
foreach ($products as $product) {
$label = $product['name'];
if (!empty($product['brand'])) {
$label = $product['brand'] . ' - ' . $label;
}
if (!empty($product['category'])) {
$label .= ' (' . $product['category'] . ')';
}
$options[$product['id']] = $label;
}
}
return $options;
}
protected function register_controls() {
// Content Section
$this->start_controls_section(
'content_section',
[
'label' => __('Content', 'cannaiq-menus'),
'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
]
);
// Get products for the select dropdown
$products_options = $this->get_products_for_select();
$this->add_control(
'product_id',
[
'label' => __('Select Product', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::SELECT2,
'options' => $products_options,
'default' => '',
'label_block' => true,
'description' => __('Search and select a product to display', 'cannaiq-menus'),
]
);
$this->add_control(
'product_id_manual',
[
'label' => __('Or Enter Product ID', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::NUMBER,
'default' => '',
'description' => __('Manually enter a product ID if not found in dropdown', 'cannaiq-menus'),
'condition' => [
'product_id' => '',
],
]
);
$this->add_control(
'layout',
[
'label' => __('Layout', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::SELECT,
'default' => 'horizontal',
'options' => [
'horizontal' => __('Horizontal', 'cannaiq-menus'),
'vertical' => __('Vertical', 'cannaiq-menus'),
],
]
);
$this->end_controls_section();
// Display Options Section
$this->start_controls_section(
'display_section',
[
'label' => __('Display Options', 'cannaiq-menus'),
'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
]
);
$this->add_control(
'show_image',
[
'label' => __('Show Image', '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(
'show_price',
[
'label' => __('Show Price', '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(
'show_description',
[
'label' => __('Show Description', '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(
'show_thc',
[
'label' => __('Show THC', '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(
'show_cbd',
[
'label' => __('Show CBD', '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(
'show_terpenes',
[
'label' => __('Show Terpenes', '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(
'show_effects',
[
'label' => __('Show Effects', '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(
'show_brand',
[
'label' => __('Show Brand', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::SWITCHER,
'label_on' => __('Yes', 'cannaiq-menus'),
'label_off' => __('No', 'cannaiq-menus'),
'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(
'background_color',
[
'label' => __('Background Color', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::COLOR,
'default' => '#ffffff',
'selectors' => [
'{{WRAPPER}} .cannaiq-single-product' => 'background-color: {{VALUE}};',
],
]
);
$this->add_control(
'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-single-product' => 'border-radius: {{SIZE}}{{UNIT}};',
],
]
);
$this->add_control(
'text_color',
[
'label' => __('Text Color', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::COLOR,
'default' => '#333333',
'selectors' => [
'{{WRAPPER}} .cannaiq-single-product' => 'color: {{VALUE}};',
],
]
);
$this->end_controls_section();
}
protected function render() {
$settings = $this->get_settings_for_display();
// Use dropdown selection, or fall back to manual ID
$product_id = !empty($settings['product_id']) ? $settings['product_id'] : $settings['product_id_manual'];
if (empty($product_id)) {
echo '<p>' . __('Please select or enter a product ID.', 'cannaiq-menus') . '</p>';
return;
}
$plugin = CannaIQ_Menus_Plugin::instance();
$product = $plugin->fetch_product($product_id);
if (!$product) {
echo '<p>' . __('Product not found.', 'cannaiq-menus') . '</p>';
return;
}
$layout = $settings['layout'];
$image_url = $product['image_url'] ?? '';
?>
<div class="cannaiq-single-product cannaiq-layout-<?php echo esc_attr($layout); ?>">
<?php if ($settings['show_image'] === 'yes' && !empty($image_url)): ?>
<div class="cannaiq-single-product-image">
<img src="<?php echo esc_url($image_url); ?>"
alt="<?php echo esc_attr($product['name']); ?>" />
</div>
<?php endif; ?>
<div class="cannaiq-single-product-details">
<h2 class="cannaiq-single-product-title">
<?php echo esc_html($product['name']); ?>
</h2>
<?php if ($settings['show_brand'] === 'yes' && !empty($product['brand'])): ?>
<div class="cannaiq-single-product-brand">
<strong><?php _e('Brand:', 'cannaiq-menus'); ?></strong>
<?php echo esc_html($product['brand']); ?>
</div>
<?php endif; ?>
<?php if ($settings['show_description'] === 'yes' && !empty($product['description'])): ?>
<div class="cannaiq-single-product-description">
<?php echo wp_kses_post(nl2br($product['description'])); ?>
</div>
<?php endif; ?>
<div class="cannaiq-single-product-info">
<?php if ($settings['show_thc'] === 'yes' && !empty($product['thc_percentage'])): ?>
<div class="cannaiq-info-item">
<strong><?php _e('THC:', 'cannaiq-menus'); ?></strong>
<span><?php echo esc_html($product['thc_percentage']); ?>%</span>
</div>
<?php endif; ?>
<?php if ($settings['show_cbd'] === 'yes' && !empty($product['cbd_percentage'])): ?>
<div class="cannaiq-info-item">
<strong><?php _e('CBD:', 'cannaiq-menus'); ?></strong>
<span><?php echo esc_html($product['cbd_percentage']); ?>%</span>
</div>
<?php endif; ?>
<?php if ($settings['show_effects'] === 'yes' && !empty($product['effects'])): ?>
<div class="cannaiq-info-item cannaiq-effects">
<strong><?php _e('Effects:', 'cannaiq-menus'); ?></strong>
<?php if (is_array($product['effects'])): ?>
<span><?php echo esc_html(implode(', ', $product['effects'])); ?></span>
<?php else: ?>
<span><?php echo esc_html($product['effects']); ?></span>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
<?php if ($settings['show_price'] === 'yes' && isset($product['regular_price'])): ?>
<div class="cannaiq-single-product-price">
<?php if (!empty($product['sale_price']) && $product['sale_price'] != $product['regular_price']): ?>
<span class="cannaiq-price-sale">$<?php echo esc_html($product['sale_price']); ?></span>
<span class="cannaiq-price-regular cannaiq-strikethrough">$<?php echo esc_html($product['regular_price']); ?></span>
<?php else: ?>
$<?php echo esc_html($product['regular_price']); ?>
<?php endif; ?>
</div>
<?php endif; ?>
<?php if (!$product['in_stock']): ?>
<div class="cannaiq-out-of-stock">
<?php _e('Out of Stock', 'cannaiq-menus'); ?>
</div>
<?php endif; ?>
</div>
</div>
<?php
}
}