| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Modules\Templating\Bricks\Elements; |
| 4 |
|
| 5 |
use Bricks\Element; |
| 6 |
use FluentCart\App\Modules\Data\ProductDataSetup; |
| 7 |
use FluentCart\App\Modules\Templating\AssetLoader; |
| 8 |
use FluentCart\App\Services\Renderer\ProductRenderer; |
| 9 |
use FluentCart\Framework\Support\Arr; |
| 10 |
use FluentCart\App\Modules\Templating\Bricks\BricksLoader; |
| 11 |
|
| 12 |
if (!defined('ABSPATH')) exit; // Exit if accessed directly |
| 13 |
|
| 14 |
class PriceRange extends Element |
| 15 |
{ |
| 16 |
|
| 17 |
public $category = 'fluent-cart'; |
| 18 |
public $name = 'fct-price-range'; |
| 19 |
public $icon = 'ti-money fluent-cart-element-icon'; |
| 20 |
|
| 21 |
public function enqueue_scripts() |
| 22 |
{ |
| 23 |
AssetLoader::loadSingleProductAssets(); |
| 24 |
} |
| 25 |
|
| 26 |
public function get_label() |
| 27 |
{ |
| 28 |
return esc_html__('Price Range', 'fluent-cart'); |
| 29 |
} |
| 30 |
|
| 31 |
public function set_controls() |
| 32 |
{ |
| 33 |
$this->controls['queryType'] = [ |
| 34 |
'tab' => 'content', |
| 35 |
'type' => 'select', |
| 36 |
'label' => esc_html__('Query Type', 'fluent-cart'), |
| 37 |
'options' => [ |
| 38 |
'default' => esc_html__('Default', 'fluent-cart'), |
| 39 |
'custom' => esc_html__('Custom', 'fluent-cart'), |
| 40 |
], |
| 41 |
'default' => 'default', |
| 42 |
'inline' => true, |
| 43 |
]; |
| 44 |
|
| 45 |
$this->controls['productId'] = [ |
| 46 |
'tab' => 'content', |
| 47 |
'type' => 'select', |
| 48 |
'label' => esc_html__('Product', 'fluent-cart'), |
| 49 |
'options' => BricksLoader::getProductOptions(), |
| 50 |
'placeholder' => esc_html__('Select a product', 'fluent-cart'), |
| 51 |
'searchable' => true, |
| 52 |
'rerender' => true, |
| 53 |
'required' => ['queryType', '=', 'custom'], |
| 54 |
]; |
| 55 |
|
| 56 |
$this->controls['manualProductId'] = [ |
| 57 |
'tab' => 'content', |
| 58 |
'type' => 'text', |
| 59 |
'label' => esc_html__('Manual Product ID', 'fluent-cart'), |
| 60 |
'description' => esc_html__('Use this if the product is not available in dropdown.', 'fluent-cart'), |
| 61 |
'required' => [['queryType', '=', 'custom'], ['productId', '=', '']], |
| 62 |
]; |
| 63 |
|
| 64 |
$this->controls['hideNoRange'] = [ |
| 65 |
'tab' => 'content', |
| 66 |
'label' => esc_html__('Hide when max and min is same', 'fluent-cart'), |
| 67 |
'type' => 'checkbox' |
| 68 |
]; |
| 69 |
$this->controls['priceRangeTypography'] = [ |
| 70 |
'tab' => 'content', |
| 71 |
'label' => esc_html__('Price Range typography', 'fluent-cart'), |
| 72 |
'type' => 'typography', |
| 73 |
'css' => [ |
| 74 |
[ |
| 75 |
'selector' => '.fct-price-range.fct-product-prices', |
| 76 |
'property' => 'font', |
| 77 |
], |
| 78 |
], |
| 79 |
]; |
| 80 |
} |
| 81 |
|
| 82 |
public function render() |
| 83 |
{ |
| 84 |
$settings = $this->settings; |
| 85 |
$queryType = $settings['queryType'] ?? 'default'; |
| 86 |
|
| 87 |
if ($queryType === 'default') { |
| 88 |
$productId = $this->post_id; |
| 89 |
} else { |
| 90 |
$productId = !empty($settings['productId']) ? \intval($settings['productId']) : 0; |
| 91 |
$manualProductId = !empty($settings['manualProductId']) ? \intval($settings['manualProductId']) : 0; |
| 92 |
|
| 93 |
if (!$productId && $manualProductId) { |
| 94 |
$productId = $manualProductId; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
$product = ProductDataSetup::getProductModel($productId); |
| 99 |
|
| 100 |
if (!$product) { |
| 101 |
return $this->render_element_placeholder([ |
| 102 |
'title' => esc_html__( |
| 103 |
'Select a product', |
| 104 |
'fluent-cart' |
| 105 |
), |
| 106 |
]); |
| 107 |
} |
| 108 |
|
| 109 |
if(Arr::get($settings, 'hideNoRange')) { |
| 110 |
$hasPriceRange = $product->detail->variation_type !== 'simple'; |
| 111 |
if(!$hasPriceRange) { |
| 112 |
$min_price = $product->detail->min_price; |
| 113 |
$max_price = $product->detail->max_price; |
| 114 |
$hasPriceRange = $max_price && $max_price > $min_price; |
| 115 |
} |
| 116 |
if(!$hasPriceRange) { |
| 117 |
return $this->render_element_placeholder( |
| 118 |
[ |
| 119 |
'description' => esc_html__('Product does not have min-max range', 'fluent-cart'), |
| 120 |
] |
| 121 |
); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
?> |
| 126 |
<div |
| 127 |
<?php |
| 128 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- render_attributes() handles escaping internally |
| 129 |
echo $this->render_attributes( '_root' ); |
| 130 |
?> |
| 131 |
> |
| 132 |
<?php (new ProductRenderer($product))->renderPrices(); ?> |
| 133 |
</div> |
| 134 |
|
| 135 |
<?php |
| 136 |
} |
| 137 |
} |
| 138 |
|