File: /mnt/data/dreamsrent-wp/wp-content/plugins/dreamsrent-widgets/widgets/class-plan-pricing.php
<?php
namespace Dreamsrentelementor\Widgets;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Repeater;
if ( ! defined( 'ABSPATH' ) ) exit;
class DSR_plan_pricing extends Widget_Base {
public function get_name() {
return 'dsr_plan_pricing';
}
public function get_title() {
return __( 'DreamsRent Plans', 'dreamsrent_elementor' );
}
public function get_icon() {
return 'eicon-price-table';
}
public function get_categories() {
return [ 'dreamsrentelemetortheme' ];
}
protected function register_controls() {
$this->start_controls_section( 'section_content', [
'label' => __( 'Content', 'dreamsrent_elementor' ),
] );
$this->add_control( 'columns', [
'label' => __( 'Columns', 'dreamsrent_elementor' ),
'type' => Controls_Manager::NUMBER,
'default' => 3,
'min' => 1,
'max' => 4,
] );
$this->add_control( 'header_title', [
'label' => __( 'Header Title', 'dreamsrent_elementor' ),
'type' => Controls_Manager::TEXT,
'default' => __( 'Pricing', 'dreamsrent_elementor' ),
] );
$this->add_control( 'header_description', [
'label' => __( 'Header Description', 'dreamsrent_elementor' ),
'type' => Controls_Manager::TEXTAREA,
'default' => __( "Quickly build an effective pricing table for your potential customers.", 'dreamsrent_elementor' ),
'rows' => 3,
] );
// Repeater for list items with token support {car_limit} and {duration_days}
$repeater = new Repeater();
$repeater->add_control( 'text', [
'label' => __( 'List Item', 'dreamsrent_elementor' ),
'type' => Controls_Manager::TEXT,
'default' => __( 'Cars limit: {car_limit}', 'dreamsrent_elementor' ),
] );
$this->add_control( 'list_items', [
'label' => __( 'List Items', 'dreamsrent_elementor' ),
'type' => Controls_Manager::REPEATER,
'fields'=> $repeater->get_controls(),
'default' => [
[ 'text' => __( 'Cars limit: {car_limit}', 'dreamsrent_elementor' ) ],
[ 'text' => __( 'Duration (days): {duration_days}', 'dreamsrent_elementor' ) ],
],
'title_field' => '{{{ text }}}',
] );
$this->end_controls_section();
// Style controls
$this->start_controls_section( 'section_style', [
'label' => __( 'Styles', 'dreamsrent_elementor' ),
'tab' => Controls_Manager::TAB_STYLE,
] );
$this->add_control( 'subtitle_color', [
'label' => __( 'Subtitle Color', 'dreamsrent_elementor' ),
'type' => Controls_Manager::COLOR,
'default' => '#6c757d',
] );
$this->add_control( 'title_color', [
'label' => __( 'Title Color', 'dreamsrent_elementor' ),
'type' => Controls_Manager::COLOR,
'default' => '#111827',
] );
$this->add_control( 'card_bg_color', [
'label' => __( 'Card Background', 'dreamsrent_elementor' ),
'type' => Controls_Manager::COLOR,
'default' => '#ffffff',
] );
$this->add_control( 'card_text_color', [
'label' => __( 'Card Text', 'dreamsrent_elementor' ),
'type' => Controls_Manager::COLOR,
'default' => '#1f2937',
] );
$this->add_control( 'button_bg_color', [
'label' => __( 'Button Background', 'dreamsrent_elementor' ),
'type' => Controls_Manager::COLOR,
'default' => '#0d6efd',
] );
$this->add_control( 'button_text_color', [
'label' => __( 'Button Text', 'dreamsrent_elementor' ),
'type' => Controls_Manager::COLOR,
'default' => '#ffffff',
] );
$this->add_control( 'current_badge_bg', [
'label' => __( 'Current Badge Background', 'dreamsrent_elementor' ),
'type' => Controls_Manager::COLOR,
'default' => '#6c757d',
] );
$this->add_control( 'current_badge_text', [
'label' => __( 'Current Badge Text', 'dreamsrent_elementor' ),
'type' => Controls_Manager::COLOR,
'default' => '#ffffff',
] );
// Typography controls
$this->add_group_control( Group_Control_Typography::get_type(), [
'name' => 'subtitle_typography',
'label' => __( 'Subtitle Typography', 'dreamsrent_elementor' ),
'selector' => '{{WRAPPER}} .pricing-table-subtitle',
] );
$this->add_group_control( Group_Control_Typography::get_type(), [
'name' => 'title_typography',
'label' => __( 'Title Typography', 'dreamsrent_elementor' ),
'selector' => '{{WRAPPER}} .pricing-table-title',
] );
$this->add_group_control( Group_Control_Typography::get_type(), [
'name' => 'card_text_typography',
'label' => __( 'Card Text Typography', 'dreamsrent_elementor' ),
'selector' => '{{WRAPPER}} .pricing-card, {{WRAPPER}} .pricing-card .card-body, {{WRAPPER}} .pricing-plan-features li, {{WRAPPER}} .pricing-plan-title',
] );
$this->add_group_control( Group_Control_Typography::get_type(), [
'name' => 'button_typography',
'label' => __( 'Button Typography', 'dreamsrent_elementor' ),
'selector' => '{{WRAPPER}} .pricing-plan-purchase-btn',
] );
// Card header styles
$this->add_control( 'card_header_bg_color', [
'label' => __( 'Card Header Background', 'dreamsrent_elementor' ),
'type' => Controls_Manager::COLOR,
'default' => '#f8f9fa',
] );
$this->add_control( 'card_header_text_color', [
'label' => __( 'Card Header Text', 'dreamsrent_elementor' ),
'type' => Controls_Manager::COLOR,
'default' => '#111827',
] );
$this->add_group_control( Group_Control_Typography::get_type(), [
'name' => 'card_header_typography',
'label' => __( 'Card Header Typography', 'dreamsrent_elementor' ),
'selector' => '{{WRAPPER}} .pricing-card .card-header .pricing-plan-title',
] );
// List styles
$this->add_control( 'list_text_color', [
'label' => __( 'List Text Color', 'dreamsrent_elementor' ),
'type' => Controls_Manager::COLOR,
'default' => '#1f2937',
] );
$this->add_group_control( Group_Control_Typography::get_type(), [
'name' => 'list_typography',
'label' => __( 'List Typography', 'dreamsrent_elementor' ),
'selector' => '{{WRAPPER}} .pricing-plan-features li',
] );
// Price styles
$this->add_control( 'price_color', [
'label' => __( 'Price Color', 'dreamsrent_elementor' ),
'type' => Controls_Manager::COLOR,
'default' => '#111827',
] );
$this->add_group_control( Group_Control_Typography::get_type(), [
'name' => 'price_typography',
'label' => __( 'Price Typography', 'dreamsrent_elementor' ),
'selector' => '{{WRAPPER}} .pricing-plan-cost',
] );
$this->end_controls_section();
}
protected function render() {
$settings = $this->get_settings_for_display();
$cols = max(1, min(4, intval($settings['columns'])));
if ( ! function_exists('wc_get_products') ) {
echo '<div class="alert alert-info">' . esc_html__('WooCommerce not active.', 'dreamsrent_elementor') . '</div>';
return;
}
$args = [
'type' => ['dreamsrent_package'],
'status' => 'publish',
'limit' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'return' => 'objects',
];
$products = wc_get_products($args);
if (empty($products)) {
echo '<div class="alert alert-info">' . esc_html__('No packages available at the moment.', 'dreamsrent_elementor') . '</div>';
return;
}
$colClass = 'col-md-' . (12 / $cols);
$checkout_url = function_exists('wc_get_checkout_url') ? wc_get_checkout_url() : home_url('/checkout');
$user_id = get_current_user_id();
$active = function_exists('dr_get_vendor_active_package') ? dr_get_vendor_active_package($user_id) : [];
$active_id = is_array($active) && !empty($active['product_id']) ? intval($active['product_id']) : 0;
$subtitle_style = !empty($settings['subtitle_color']) ? 'style="color:'. esc_attr($settings['subtitle_color']) .'"' : '';
$title_style = !empty($settings['title_color']) ? 'style="color:'. esc_attr($settings['title_color']) .'"' : '';
?>
<div class="pricing-header px-3 py-3 pt-md-5 pb-md-4 mx-auto text-center">
<h1 class="display-4 pricing-table-title" <?php echo $title_style; ?>><?php echo esc_html( $settings['header_title'] ); ?></h1>
<p class="lead pricing-table-subtitle" <?php echo $subtitle_style; ?>><?php echo esc_html( $settings['header_description'] ); ?></p>
</div>
<div class="container pricing-header">
<div class="row">
<?php foreach ($products as $product):
$pid = $product->get_id();
$title = $product->get_name();
// Try to render WooCommerce price HTML (supports regular/sale); fallback to raw
$price_html = $product->get_price_html();
if ($price_html === '') {
$raw_price = $product->get_price();
$price_html = ($raw_price !== '') ? wc_price( $raw_price ) : '';
}
$car_limit = get_post_meta($pid, '_dr_pkg_car_limit', true);
$duration = get_post_meta($pid, '_dr_pkg_duration_days', true);
$buy_url = add_query_arg('add-to-cart', $pid, $checkout_url);
$is_active = ($pid === $active_id);
$card_style = '';
$icon_color = '';
if (!empty($settings['card_bg_color'])) {
$card_style .= 'background-color:'. esc_attr($settings['card_bg_color']) .';';
}
if (!empty($settings['card_text_color'])) { $card_style .= 'color:'. esc_attr($settings['card_text_color']) .';'; }
$card_header_style = '';
if (!empty($settings['card_header_bg_color'])) {
$icon_color .= 'color:'. esc_attr($settings['card_header_bg_color']) .';';
$card_header_style .= 'background-color:'. esc_attr($settings['card_header_bg_color']) .';'; }
if (!empty($settings['card_header_text_color'])) { $card_header_style .= 'color:'. esc_attr($settings['card_header_text_color']) .';'; }
$btn_style = '';
if (!empty($settings['button_bg_color'])) { $btn_style .= 'background-color:'. esc_attr($settings['button_bg_color']) .';'; }
if (!empty($settings['button_text_color'])) { $btn_style .= 'color:'. esc_attr($settings['button_text_color']) .';'; }
$badge_style = '';
if (!empty($settings['current_badge_bg'])) { $badge_style .= 'background-color:'. esc_attr($settings['current_badge_bg']) .';'; }
if (!empty($settings['current_badge_text'])) { $badge_style .= 'color:'. esc_attr($settings['current_badge_text']) .';'; }
$price_style = '';
if (!empty($settings['price_color'])) { $price_style .= 'color:'. esc_attr($settings['price_color']) .';'; }
$list_style = '';
if (!empty($settings['list_text_color'])) { $list_style .= 'color:'. esc_attr($settings['list_text_color']) .';'; }
?>
<div class="<?php echo esc_attr($colClass); ?>">
<div class="card mb-4 box-shadow pricing-card" <?php echo $card_style ? 'style="'. $card_style .'"' : ''; ?>>
<div class="card-header" <?php echo $card_header_style ? 'style="'. $card_header_style .'"' : ''; ?>>
<span class="my-0 pricing-plan-title"><?php echo esc_html($title); ?></span>
<h1 class="card-title pricing-card-title pricing-plan-cost" <?php echo $price_style ? 'style="'. $price_style .'"' : ''; ?>>
<?php echo wp_kses_post( $price_html ? $price_html : esc_html__('FREE','dreamsrent_elementor') ); ?>
<small class="text-muted"></small>
</h1>
</div>
<div class="card-body">
<?php if ($is_active): ?>
<span class="badge" <?php echo $badge_style ? 'style="'. $badge_style .'"' : ''; ?>><?php echo esc_html__('Current Plan','dreamsrent_elementor'); ?></span>
<?php endif; ?>
<ul class="list-unstyled mb-4 pricing-plan-features" <?php echo $list_style ? 'style="'. $list_style .'"' : ''; ?>>
<?php
$items = isset($settings['list_items']) && is_array($settings['list_items']) ? $settings['list_items'] : array();
foreach ($items as $it) {
$text = isset($it['text']) ? $it['text'] : '';
$text = str_replace(array('{car_limit}','{duration_days}'), array(
($car_limit === '' || intval($car_limit) === 0) ? __('Unlimited','dreamsrent_elementor') : intval($car_limit),
($duration === '' || intval($duration) === 0) ? __('No expiry','dreamsrent_elementor') : intval($duration),
), $text);
if ($text !== '') {
echo '<li class="mb-2"> <i class="fa fa-square-check me-2" style="'. $icon_color .'"></i> '. esc_html($text) .'</li>';
}
}
?>
</ul>
<?php if ($is_active): ?>
<a href="#" class="btn btn-lg btn-block pricing-plan-purchase-btn disabled w-100" <?php echo $btn_style ? 'style="'. $btn_style .'"' : ''; ?>><?php echo esc_html__('Current Plan','dreamsrent_elementor'); ?></a>
<?php else: ?>
<a href="<?php echo esc_url($buy_url); ?>" class="btn btn-lg btn-block pricing-plan-purchase-btn w-100" <?php echo $btn_style ? 'style="'. $btn_style .'"' : ''; ?>><?php echo esc_html__('Get started','dreamsrent_elementor'); ?></a>
<?php endif; ?>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
<?php
}
}