| 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\Modules\Templating\Bricks\BricksLoader; |
| 9 |
use FluentCart\App\Services\Renderer\ProductRenderer; |
| 10 |
|
| 11 |
if (!defined('ABSPATH')) exit; // Exit if accessed directly |
| 12 |
|
| 13 |
class BuySection extends Element |
| 14 |
{ |
| 15 |
public $category = 'fluent-cart'; |
| 16 |
public $name = 'fct-product-buy-section'; |
| 17 |
public $icon = 'ti-bag fluent-cart-element-icon'; |
| 18 |
|
| 19 |
public function enqueue_scripts() |
| 20 |
{ |
| 21 |
AssetLoader::loadSingleProductAssets(); |
| 22 |
} |
| 23 |
|
| 24 |
public function get_label() |
| 25 |
{ |
| 26 |
return esc_html__('Buy Section', 'fluent-cart'); |
| 27 |
} |
| 28 |
|
| 29 |
public function set_control_groups() |
| 30 |
{ |
| 31 |
$this->control_groups['buyButton'] = [ |
| 32 |
'title' => esc_html__('Direct Buy Button', 'fluent-cart'), |
| 33 |
'tab' => 'content', |
| 34 |
]; |
| 35 |
|
| 36 |
$this->control_groups['cartButton'] = [ |
| 37 |
'title' => esc_html__('Add to Cart Button', 'fluent-cart'), |
| 38 |
'tab' => 'content', |
| 39 |
]; |
| 40 |
|
| 41 |
} |
| 42 |
|
| 43 |
public function set_controls() |
| 44 |
{ |
| 45 |
$this->controls['queryType'] = [ |
| 46 |
'tab' => 'content', |
| 47 |
'type' => 'select', |
| 48 |
'label' => esc_html__('Query Type', 'fluent-cart'), |
| 49 |
'options' => [ |
| 50 |
'default' => esc_html__('Default', 'fluent-cart'), |
| 51 |
'custom' => esc_html__('Custom', 'fluent-cart'), |
| 52 |
], |
| 53 |
'default' => 'default', |
| 54 |
'inline' => true, |
| 55 |
]; |
| 56 |
|
| 57 |
$this->controls['productId'] = [ |
| 58 |
'tab' => 'content', |
| 59 |
'type' => 'select', |
| 60 |
'label' => esc_html__('Product', 'fluent-cart'), |
| 61 |
'options' => BricksLoader::getProductOptions(), |
| 62 |
'placeholder' => esc_html__('Select a product', 'fluent-cart'), |
| 63 |
'searchable' => true, |
| 64 |
'rerender' => true, |
| 65 |
'required' => ['queryType', '=', 'custom'], |
| 66 |
]; |
| 67 |
|
| 68 |
$this->controls['manualProductId'] = [ |
| 69 |
'tab' => 'content', |
| 70 |
'type' => 'text', |
| 71 |
'label' => esc_html__('Manual Product ID', 'fluent-cart'), |
| 72 |
'description' => esc_html__('Use this if the product is not available in dropdown.', 'fluent-cart'), |
| 73 |
'required' => [['queryType', '=', 'custom'], ['productId', '=', '']], |
| 74 |
]; |
| 75 |
|
| 76 |
// BUTTONS |
| 77 |
$this->controls['buttonInfo'] = [ |
| 78 |
'tab' => 'content', |
| 79 |
'group' => 'cartButton', |
| 80 |
'type' => 'info', |
| 81 |
'content' => esc_html__('Add to Cart Button will be shown only for non-subscribable products.', 'fluent-cart'), |
| 82 |
]; |
| 83 |
$this->controls['buttonText'] = [ |
| 84 |
'tab' => 'content', |
| 85 |
'group' => 'cartButton', |
| 86 |
'type' => 'text', |
| 87 |
'inline' => true, |
| 88 |
'label' => esc_html__('Button Text', 'fluent-cart'), |
| 89 |
'placeholder' => esc_html__('Add To Cart', 'fluent-cart'), |
| 90 |
]; |
| 91 |
$this->controls['buttonMargin'] = [ |
| 92 |
'tab' => 'content', |
| 93 |
'group' => 'cartButton', |
| 94 |
'label' => esc_html__('Buttons Margin', 'fluent-cart'), |
| 95 |
'type' => 'spacing', |
| 96 |
'css' => [ |
| 97 |
[ |
| 98 |
'selector' => '.fct-product-buttons-wrap .fluent-cart-add-to-cart-button', |
| 99 |
'property' => 'margin', |
| 100 |
], |
| 101 |
], |
| 102 |
]; |
| 103 |
$this->controls['buttonPadding'] = [ |
| 104 |
'tab' => 'content', |
| 105 |
'group' => 'cartButton', |
| 106 |
'label' => esc_html__('Padding', 'fluent-cart'), |
| 107 |
'type' => 'spacing', |
| 108 |
'css' => [ |
| 109 |
[ |
| 110 |
'selector' => '.fct-product-buttons-wrap .fluent-cart-add-to-cart-button', |
| 111 |
'property' => 'padding', |
| 112 |
], |
| 113 |
], |
| 114 |
]; |
| 115 |
$this->controls['buttonWidth'] = [ |
| 116 |
'tab' => 'content', |
| 117 |
'group' => 'cartButton', |
| 118 |
'label' => esc_html__('Width', 'fluent-cart'), |
| 119 |
'type' => 'number', |
| 120 |
'units' => true, |
| 121 |
'css' => [ |
| 122 |
[ |
| 123 |
'selector' => '.fct-product-buttons-wrap .fluent-cart-add-to-cart-button', |
| 124 |
'property' => 'min-width', |
| 125 |
], |
| 126 |
], |
| 127 |
]; |
| 128 |
$this->controls['buttonBackgroundColor'] = [ |
| 129 |
'tab' => 'content', |
| 130 |
'group' => 'cartButton', |
| 131 |
'label' => esc_html__('Background color', 'fluent-cart'), |
| 132 |
'type' => 'color', |
| 133 |
'css' => [ |
| 134 |
[ |
| 135 |
'selector' => '.fct-product-buttons-wrap .fluent-cart-add-to-cart-button', |
| 136 |
'property' => 'background-color', |
| 137 |
], |
| 138 |
], |
| 139 |
]; |
| 140 |
$this->controls['buttonBorder'] = [ |
| 141 |
'tab' => 'content', |
| 142 |
'group' => 'cartButton', |
| 143 |
'label' => esc_html__('Border', 'fluent-cart'), |
| 144 |
'type' => 'border', |
| 145 |
'css' => [ |
| 146 |
[ |
| 147 |
'property' => 'border', |
| 148 |
'selector' => '.fct-product-buttons-wrap .fluent-cart-add-to-cart-button', |
| 149 |
], |
| 150 |
], |
| 151 |
]; |
| 152 |
$this->controls['buttonTypography'] = [ |
| 153 |
'tab' => 'content', |
| 154 |
'group' => 'cartButton', |
| 155 |
'label' => esc_html__('Typography', 'fluent-cart'), |
| 156 |
'type' => 'typography', |
| 157 |
'css' => [ |
| 158 |
[ |
| 159 |
'selector' => '.fct-product-buttons-wrap .', |
| 160 |
'property' => 'font', |
| 161 |
], |
| 162 |
], |
| 163 |
]; |
| 164 |
|
| 165 |
$this->controls['icon'] = [ |
| 166 |
'tab' => 'content', |
| 167 |
'group' => 'cartButton', |
| 168 |
'label' => esc_html__('Icon', 'fluent-cart'), |
| 169 |
'type' => 'icon', |
| 170 |
'rerender' => true, |
| 171 |
]; |
| 172 |
|
| 173 |
$this->controls['iconTypography'] = [ |
| 174 |
'tab' => 'content', |
| 175 |
'group' => 'cartButton', |
| 176 |
'label' => esc_html__('Icon typography', 'fluent-cart'), |
| 177 |
'type' => 'typography', |
| 178 |
'css' => [ |
| 179 |
[ |
| 180 |
'property' => 'font', |
| 181 |
'selector' => '.icon', |
| 182 |
], |
| 183 |
], |
| 184 |
]; |
| 185 |
|
| 186 |
$this->controls['iconOnly'] = [ |
| 187 |
'tab' => 'content', |
| 188 |
'group' => 'cartButton', |
| 189 |
'label' => esc_html__('Icon only', 'fluent-cart'), |
| 190 |
'type' => 'checkbox', |
| 191 |
'inline' => true, |
| 192 |
'placeholder' => esc_html__('Yes', 'fluent-cart'), |
| 193 |
'required' => ['icon', '!=', ''], |
| 194 |
]; |
| 195 |
|
| 196 |
$this->controls['iconPosition'] = [ |
| 197 |
'tab' => 'content', |
| 198 |
'group' => 'cartButton', |
| 199 |
'label' => esc_html__('Icon position', 'fluent-cart'), |
| 200 |
'type' => 'select', |
| 201 |
'options' => $this->control_options['iconPosition'], |
| 202 |
'inline' => true, |
| 203 |
'placeholder' => esc_html__('Left', 'fluent-cart'), |
| 204 |
'required' => [ |
| 205 |
['icon', '!=', ''], |
| 206 |
['iconOnly', '=', ''], |
| 207 |
], |
| 208 |
]; |
| 209 |
|
| 210 |
// Buy Now BUTTON |
| 211 |
$this->controls['directButtonText'] = [ |
| 212 |
'tab' => 'content', |
| 213 |
'group' => 'buyButton', |
| 214 |
'type' => 'text', |
| 215 |
'inline' => true, |
| 216 |
'label' => esc_html__('Button Text', 'fluent-cart'), |
| 217 |
'placeholder' => esc_html__('Buy Now', 'fluent-cart'), |
| 218 |
]; |
| 219 |
$this->controls['directButtonMargin'] = [ |
| 220 |
'tab' => 'content', |
| 221 |
'group' => 'buyButton', |
| 222 |
'label' => esc_html__('Buttons Margin', 'fluent-cart'), |
| 223 |
'type' => 'spacing', |
| 224 |
'css' => [ |
| 225 |
[ |
| 226 |
'selector' => '.fct-product-buttons-wrap .fluent-cart-direct-checkout-button', |
| 227 |
'property' => 'margin', |
| 228 |
], |
| 229 |
], |
| 230 |
]; |
| 231 |
$this->controls['directButtonPadding'] = [ |
| 232 |
'tab' => 'content', |
| 233 |
'group' => 'buyButton', |
| 234 |
'label' => esc_html__('Padding', 'fluent-cart'), |
| 235 |
'type' => 'spacing', |
| 236 |
'css' => [ |
| 237 |
[ |
| 238 |
'selector' => '.fct-product-buttons-wrap .fluent-cart-direct-checkout-button', |
| 239 |
'property' => 'padding', |
| 240 |
], |
| 241 |
], |
| 242 |
]; |
| 243 |
$this->controls['directButtonWidth'] = [ |
| 244 |
'tab' => 'content', |
| 245 |
'group' => 'buyButton', |
| 246 |
'label' => esc_html__('Width', 'fluent-cart'), |
| 247 |
'type' => 'number', |
| 248 |
'units' => true, |
| 249 |
'css' => [ |
| 250 |
[ |
| 251 |
'selector' => '.fct-product-buttons-wrap .fluent-cart-direct-checkout-button', |
| 252 |
'property' => 'min-width', |
| 253 |
], |
| 254 |
], |
| 255 |
]; |
| 256 |
$this->controls['directButtonBackgroundColor'] = [ |
| 257 |
'tab' => 'content', |
| 258 |
'group' => 'buyButton', |
| 259 |
'label' => esc_html__('Background color', 'fluent-cart'), |
| 260 |
'type' => 'color', |
| 261 |
'css' => [ |
| 262 |
[ |
| 263 |
'selector' => '.fct-product-buttons-wrap .fluent-cart-direct-checkout-button', |
| 264 |
'property' => 'background-color', |
| 265 |
], |
| 266 |
], |
| 267 |
]; |
| 268 |
$this->controls['directButtonBorder'] = [ |
| 269 |
'tab' => 'content', |
| 270 |
'group' => 'buyButton', |
| 271 |
'label' => esc_html__('Border', 'fluent-cart'), |
| 272 |
'type' => 'border', |
| 273 |
'css' => [ |
| 274 |
[ |
| 275 |
'property' => 'border', |
| 276 |
'selector' => '.fct-product-buttons-wrap .fluent-cart-direct-checkout-button', |
| 277 |
], |
| 278 |
], |
| 279 |
]; |
| 280 |
$this->controls['directButtonTypography'] = [ |
| 281 |
'tab' => 'content', |
| 282 |
'group' => 'buyButton', |
| 283 |
'label' => esc_html__('Typography', 'fluent-cart'), |
| 284 |
'type' => 'typography', |
| 285 |
'css' => [ |
| 286 |
[ |
| 287 |
'selector' => '.fct-product-buttons-wrap .fluent-cart-direct-checkout-button', |
| 288 |
'property' => 'font', |
| 289 |
], |
| 290 |
], |
| 291 |
]; |
| 292 |
|
| 293 |
$this->controls['directButtonIcon'] = [ |
| 294 |
'tab' => 'content', |
| 295 |
'group' => 'buyButton', |
| 296 |
'label' => esc_html__('Icon', 'fluent-cart'), |
| 297 |
'type' => 'icon', |
| 298 |
'rerender' => true, |
| 299 |
]; |
| 300 |
|
| 301 |
$this->controls['directButtonIconTypography'] = [ |
| 302 |
'tab' => 'content', |
| 303 |
'group' => 'buyButton', |
| 304 |
'label' => esc_html__('Icon typography', 'fluent-cart'), |
| 305 |
'type' => 'typography', |
| 306 |
'css' => [ |
| 307 |
[ |
| 308 |
'property' => 'font', |
| 309 |
'selector' => '.icon', |
| 310 |
], |
| 311 |
], |
| 312 |
]; |
| 313 |
|
| 314 |
$this->controls['directButtonIconOnly'] = [ |
| 315 |
'tab' => 'content', |
| 316 |
'group' => 'buyButton', |
| 317 |
'label' => esc_html__('Icon only', 'fluent-cart'), |
| 318 |
'type' => 'checkbox', |
| 319 |
'inline' => true, |
| 320 |
'placeholder' => esc_html__('Yes', 'fluent-cart'), |
| 321 |
'required' => ['directButtonIcon', '!=', ''], |
| 322 |
]; |
| 323 |
|
| 324 |
$this->controls['directButtonIconPosition'] = [ |
| 325 |
'tab' => 'content', |
| 326 |
'group' => 'buyButton', |
| 327 |
'label' => esc_html__('Icon position', 'fluent-cart'), |
| 328 |
'type' => 'select', |
| 329 |
'options' => $this->control_options['iconPosition'], |
| 330 |
'inline' => true, |
| 331 |
'placeholder' => esc_html__('Left', 'fluent-cart'), |
| 332 |
'required' => [ |
| 333 |
['directButtonIcon', '!=', ''], |
| 334 |
['directButtonIconOnly', '=', ''], |
| 335 |
], |
| 336 |
]; |
| 337 |
} |
| 338 |
|
| 339 |
public function render() |
| 340 |
{ |
| 341 |
$settings = $this->settings; |
| 342 |
$queryType = $settings['queryType'] ?? 'default'; |
| 343 |
|
| 344 |
if ($queryType === 'default') { |
| 345 |
$productId = $this->post_id; |
| 346 |
} else { |
| 347 |
$productId = !empty($settings['productId']) ? \intval($settings['productId']) : 0; |
| 348 |
$manualProductId = !empty($settings['manualProductId']) ? \intval($settings['manualProductId']) : 0; |
| 349 |
|
| 350 |
if (!$productId && $manualProductId) { |
| 351 |
$productId = $manualProductId; |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
$product = ProductDataSetup::getProductModel($productId); |
| 356 |
|
| 357 |
if (!$product) { |
| 358 |
return $this->render_element_placeholder([ |
| 359 |
'title' => esc_html__('Select a product', 'fluent-cart'), |
| 360 |
]); |
| 361 |
} |
| 362 |
|
| 363 |
?> |
| 364 |
<div |
| 365 |
<?php |
| 366 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- render_attributes() handles escaping internally |
| 367 |
echo $this->render_attributes( '_root' ); |
| 368 |
?> |
| 369 |
> |
| 370 |
<?php (new ProductRenderer($product))->renderBuySection([ |
| 371 |
'button_atts' => array_filter([ |
| 372 |
'buy_now_text' => $this->getBuyNowText(), |
| 373 |
'add_to_cart_text' => $this->getAddToCartText(), |
| 374 |
'is_icon_only' => isset($this->settings['iconOnly']), |
| 375 |
]), |
| 376 |
'variation_atts' => [ |
| 377 |
'wrapper_class' => 'bricks-fct-variation-swatches' |
| 378 |
], |
| 379 |
]); ?> |
| 380 |
</div> |
| 381 |
|
| 382 |
<?php |
| 383 |
} |
| 384 |
|
| 385 |
public function getAddToCartText() |
| 386 |
{ |
| 387 |
$settings = $this->settings; |
| 388 |
$buttonText = !empty($settings['buttonText']) ? $settings['buttonText'] : __('Add To Cart', 'fluent-cart'); |
| 389 |
|
| 390 |
$icon = !empty($settings['icon']) ? self::render_icon($settings['icon'], ['icon']) : false; |
| 391 |
$icon_position = isset($settings['iconPosition']) ? $settings['iconPosition'] : 'left'; |
| 392 |
$icon_only = isset($settings['iconOnly']); |
| 393 |
|
| 394 |
// Build HTML |
| 395 |
$output = ''; |
| 396 |
|
| 397 |
if ($icon_only && $icon) { |
| 398 |
// Icon only (@since 1.12.2) |
| 399 |
$output = $icon; |
| 400 |
} else { |
| 401 |
if (!$icon) { |
| 402 |
$output = $buttonText; |
| 403 |
} else if ($icon_position === 'left') { |
| 404 |
$output .= $icon; |
| 405 |
$output .= "<span>$buttonText</span>"; |
| 406 |
} else if ($icon_position === 'right') { |
| 407 |
$output .= "<span>$buttonText</span>" . $icon; |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
return $output; |
| 412 |
} |
| 413 |
|
| 414 |
public function getBuyNowText() |
| 415 |
{ |
| 416 |
$settings = $this->settings; |
| 417 |
$buttonText = !empty($settings['directButtonText']) ? $settings['directButtonText'] : __('Buy Now', 'fluent-cart'); |
| 418 |
|
| 419 |
$icon = !empty($settings['directButtonIcon']) ? self::render_icon($settings['directButtonIcon'], ['icon']) : false; |
| 420 |
$icon_position = isset($settings['directButtonIconPosition']) ? $settings['directButtonIconPosition'] : 'left'; |
| 421 |
$icon_only = isset($settings['directButtonIconOnly']); |
| 422 |
|
| 423 |
// Build HTML |
| 424 |
$output = ''; |
| 425 |
|
| 426 |
if ($icon_only && $icon) { |
| 427 |
// Icon only (@since 1.12.2) |
| 428 |
$output = $icon; |
| 429 |
} else { |
| 430 |
if (!$icon) { |
| 431 |
$output = $buttonText; |
| 432 |
} else if ($icon_position === 'left') { |
| 433 |
$output .= $icon; |
| 434 |
$output .= "<span>$buttonText</span>"; |
| 435 |
} else if ($icon_position === 'right') { |
| 436 |
$output .= "<span>$buttonText</span>" . $icon; |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
return $output; |
| 441 |
} |
| 442 |
} |
| 443 |
|