318 lines
11 KiB
PHP
318 lines
11 KiB
PHP
<?php
|
|
/**
|
|
* Elementor Product Grid Widget
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
class Dutchie_Menus_Product_Grid_Widget extends \Elementor\Widget_Base {
|
|
|
|
public function get_name() {
|
|
return 'dutchie_product_grid';
|
|
}
|
|
|
|
public function get_title() {
|
|
return __('Dutchie Product Grid', 'dutchie-menus');
|
|
}
|
|
|
|
public function get_icon() {
|
|
return 'eicon-products';
|
|
}
|
|
|
|
public function get_categories() {
|
|
return ['general'];
|
|
}
|
|
|
|
protected function register_controls() {
|
|
|
|
// Content Section
|
|
$this->start_controls_section(
|
|
'content_section',
|
|
[
|
|
'label' => __('Content', 'dutchie-menus'),
|
|
'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
|
|
]
|
|
);
|
|
|
|
$this->add_control(
|
|
'store_id',
|
|
[
|
|
'label' => __('Store ID', 'dutchie-menus'),
|
|
'type' => \Elementor\Controls_Manager::NUMBER,
|
|
'default' => get_option('dutchie_default_store_id', 1),
|
|
'min' => 1,
|
|
]
|
|
);
|
|
|
|
$this->add_control(
|
|
'category_id',
|
|
[
|
|
'label' => __('Category ID', 'dutchie-menus'),
|
|
'type' => \Elementor\Controls_Manager::NUMBER,
|
|
'default' => '',
|
|
'description' => __('Leave empty to show all categories', 'dutchie-menus'),
|
|
]
|
|
);
|
|
|
|
$this->add_control(
|
|
'limit',
|
|
[
|
|
'label' => __('Number of Products', 'dutchie-menus'),
|
|
'type' => \Elementor\Controls_Manager::NUMBER,
|
|
'default' => 12,
|
|
'min' => 1,
|
|
'max' => 100,
|
|
]
|
|
);
|
|
|
|
$this->add_control(
|
|
'columns',
|
|
[
|
|
'label' => __('Columns', 'dutchie-menus'),
|
|
'type' => \Elementor\Controls_Manager::SELECT,
|
|
'default' => '3',
|
|
'options' => [
|
|
'2' => __('2 Columns', 'dutchie-menus'),
|
|
'3' => __('3 Columns', 'dutchie-menus'),
|
|
'4' => __('4 Columns', 'dutchie-menus'),
|
|
'6' => __('6 Columns', 'dutchie-menus'),
|
|
],
|
|
]
|
|
);
|
|
|
|
$this->add_control(
|
|
'in_stock_only',
|
|
[
|
|
'label' => __('In Stock Only', 'dutchie-menus'),
|
|
'type' => \Elementor\Controls_Manager::SWITCHER,
|
|
'label_on' => __('Yes', 'dutchie-menus'),
|
|
'label_off' => __('No', 'dutchie-menus'),
|
|
'return_value' => 'yes',
|
|
'default' => 'yes',
|
|
]
|
|
);
|
|
|
|
$this->add_control(
|
|
'search',
|
|
[
|
|
'label' => __('Search Filter', 'dutchie-menus'),
|
|
'type' => \Elementor\Controls_Manager::TEXT,
|
|
'default' => '',
|
|
'description' => __('Filter products by name', 'dutchie-menus'),
|
|
]
|
|
);
|
|
|
|
$this->end_controls_section();
|
|
|
|
// Display Options Section
|
|
$this->start_controls_section(
|
|
'display_section',
|
|
[
|
|
'label' => __('Display Options', 'dutchie-menus'),
|
|
'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
|
|
]
|
|
);
|
|
|
|
$this->add_control(
|
|
'show_image',
|
|
[
|
|
'label' => __('Show Image', 'dutchie-menus'),
|
|
'type' => \Elementor\Controls_Manager::SWITCHER,
|
|
'label_on' => __('Yes', 'dutchie-menus'),
|
|
'label_off' => __('No', 'dutchie-menus'),
|
|
'return_value' => 'yes',
|
|
'default' => 'yes',
|
|
]
|
|
);
|
|
|
|
$this->add_control(
|
|
'show_price',
|
|
[
|
|
'label' => __('Show Price', 'dutchie-menus'),
|
|
'type' => \Elementor\Controls_Manager::SWITCHER,
|
|
'label_on' => __('Yes', 'dutchie-menus'),
|
|
'label_off' => __('No', 'dutchie-menus'),
|
|
'return_value' => 'yes',
|
|
'default' => 'yes',
|
|
]
|
|
);
|
|
|
|
$this->add_control(
|
|
'show_thc',
|
|
[
|
|
'label' => __('Show THC', 'dutchie-menus'),
|
|
'type' => \Elementor\Controls_Manager::SWITCHER,
|
|
'label_on' => __('Yes', 'dutchie-menus'),
|
|
'label_off' => __('No', 'dutchie-menus'),
|
|
'return_value' => 'yes',
|
|
'default' => 'yes',
|
|
]
|
|
);
|
|
|
|
$this->add_control(
|
|
'show_cbd',
|
|
[
|
|
'label' => __('Show CBD', 'dutchie-menus'),
|
|
'type' => \Elementor\Controls_Manager::SWITCHER,
|
|
'label_on' => __('Yes', 'dutchie-menus'),
|
|
'label_off' => __('No', 'dutchie-menus'),
|
|
'return_value' => 'yes',
|
|
'default' => 'yes',
|
|
]
|
|
);
|
|
|
|
$this->add_control(
|
|
'show_description',
|
|
[
|
|
'label' => __('Show Description', 'dutchie-menus'),
|
|
'type' => \Elementor\Controls_Manager::SWITCHER,
|
|
'label_on' => __('Yes', 'dutchie-menus'),
|
|
'label_off' => __('No', 'dutchie-menus'),
|
|
'return_value' => 'yes',
|
|
'default' => 'no',
|
|
]
|
|
);
|
|
|
|
$this->end_controls_section();
|
|
|
|
// Style Section
|
|
$this->start_controls_section(
|
|
'style_section',
|
|
[
|
|
'label' => __('Style', 'dutchie-menus'),
|
|
'tab' => \Elementor\Controls_Manager::TAB_STYLE,
|
|
]
|
|
);
|
|
|
|
$this->add_control(
|
|
'card_background',
|
|
[
|
|
'label' => __('Card Background', 'dutchie-menus'),
|
|
'type' => \Elementor\Controls_Manager::COLOR,
|
|
'default' => '#ffffff',
|
|
'selectors' => [
|
|
'{{WRAPPER}} .dutchie-product-card' => 'background-color: {{VALUE}};',
|
|
],
|
|
]
|
|
);
|
|
|
|
$this->add_control(
|
|
'card_border_radius',
|
|
[
|
|
'label' => __('Border Radius', 'dutchie-menus'),
|
|
'type' => \Elementor\Controls_Manager::SLIDER,
|
|
'size_units' => ['px'],
|
|
'range' => [
|
|
'px' => [
|
|
'min' => 0,
|
|
'max' => 50,
|
|
],
|
|
],
|
|
'default' => [
|
|
'size' => 8,
|
|
],
|
|
'selectors' => [
|
|
'{{WRAPPER}} .dutchie-product-card' => 'border-radius: {{SIZE}}{{UNIT}};',
|
|
],
|
|
]
|
|
);
|
|
|
|
$this->add_control(
|
|
'text_color',
|
|
[
|
|
'label' => __('Text Color', 'dutchie-menus'),
|
|
'type' => \Elementor\Controls_Manager::COLOR,
|
|
'default' => '#333333',
|
|
'selectors' => [
|
|
'{{WRAPPER}} .dutchie-product-card' => 'color: {{VALUE}};',
|
|
],
|
|
]
|
|
);
|
|
|
|
$this->end_controls_section();
|
|
}
|
|
|
|
protected function render() {
|
|
$settings = $this->get_settings_for_display();
|
|
|
|
$args = [
|
|
'store_id' => $settings['store_id'],
|
|
'limit' => $settings['limit'],
|
|
'in_stock' => $settings['in_stock_only'] === 'yes' ? 'true' : 'false',
|
|
];
|
|
|
|
if (!empty($settings['category_id'])) {
|
|
$args['category_id'] = $settings['category_id'];
|
|
}
|
|
|
|
if (!empty($settings['search'])) {
|
|
$args['search'] = $settings['search'];
|
|
}
|
|
|
|
$plugin = Dutchie_Menus_Plugin::instance();
|
|
$products = $plugin->fetch_products($args);
|
|
|
|
if (!$products) {
|
|
echo '<p>' . __('No products found.', 'dutchie-menus') . '</p>';
|
|
return;
|
|
}
|
|
|
|
$columns = $settings['columns'];
|
|
?>
|
|
<div class="dutchie-product-grid dutchie-grid-cols-<?php echo esc_attr($columns); ?>">
|
|
<?php foreach ($products as $product): ?>
|
|
<div class="dutchie-product-card">
|
|
<?php if ($settings['show_image'] === 'yes' && !empty($product['image_url_full'])): ?>
|
|
<div class="dutchie-product-image">
|
|
<img src="<?php echo esc_url($product['image_url_full']); ?>"
|
|
alt="<?php echo esc_attr($product['name']); ?>"
|
|
loading="lazy" />
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="dutchie-product-content">
|
|
<h3 class="dutchie-product-title">
|
|
<?php echo esc_html($product['name']); ?>
|
|
</h3>
|
|
|
|
<?php if ($settings['show_description'] === 'yes' && !empty($product['description'])): ?>
|
|
<p class="dutchie-product-description">
|
|
<?php echo esc_html(wp_trim_words($product['description'], 15)); ?>
|
|
</p>
|
|
<?php endif; ?>
|
|
|
|
<div class="dutchie-product-meta">
|
|
<?php if ($settings['show_thc'] === 'yes' && !empty($product['metadata']['thc'])): ?>
|
|
<span class="dutchie-meta-item dutchie-thc">
|
|
<strong>THC:</strong> <?php echo esc_html($product['metadata']['thc']); ?>
|
|
</span>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($settings['show_cbd'] === 'yes' && !empty($product['metadata']['cbd'])): ?>
|
|
<span class="dutchie-meta-item dutchie-cbd">
|
|
<strong>CBD:</strong> <?php echo esc_html($product['metadata']['cbd']); ?>
|
|
</span>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<?php if ($settings['show_price'] === 'yes' && isset($product['price'])): ?>
|
|
<div class="dutchie-product-price">
|
|
$<?php echo number_format($product['price'], 2); ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if (!$product['in_stock']): ?>
|
|
<div class="dutchie-out-of-stock">
|
|
<?php _e('Out of Stock', 'dutchie-menus'); ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php
|
|
}
|
|
}
|