| 1 |
<?php namespace TierPricingTable\Addons\GlobalTieredPricing\CPT\Form; |
| 2 |
|
| 3 |
use TierPricingTable\Addons\GlobalTieredPricing\CPT\Form\Tabs\ProductAndCategories; |
| 4 |
use TierPricingTable\Addons\GlobalTieredPricing\CPT\Form\Tabs\Quantity; |
| 5 |
use TierPricingTable\Addons\GlobalTieredPricing\CPT\Form\Tabs\Pricing; |
| 6 |
use TierPricingTable\Addons\GlobalTieredPricing\CPT\Form\Tabs\Settings; |
| 7 |
use TierPricingTable\Addons\GlobalTieredPricing\CPT\Form\Tabs\UsersAndRoles; |
| 8 |
use TierPricingTable\Addons\GlobalTieredPricing\CPT\GlobalTieredPricingCPT; |
| 9 |
use TierPricingTable\Addons\GlobalTieredPricing\GlobalPricingRule; |
| 10 |
use TierPricingTable\Core\ServiceContainerTrait; |
| 11 |
use WP_Post; |
| 12 |
|
| 13 |
class Form { |
| 14 |
|
| 15 |
use ServiceContainerTrait; |
| 16 |
|
| 17 |
/** |
| 18 |
* Tabs |
| 19 |
* |
| 20 |
* @var FormTab[] |
| 21 |
*/ |
| 22 |
protected $tabs; |
| 23 |
|
| 24 |
protected $defaultTab = 'pricing'; |
| 25 |
|
| 26 |
protected $pricingRuleInstance = null; |
| 27 |
|
| 28 |
public function __construct() { |
| 29 |
|
| 30 |
add_action( 'init', function () { |
| 31 |
$this->tabs = apply_filters( 'tiered_pricing_table/global_pricing/form_tabs', array( |
| 32 |
new Pricing( $this ), |
| 33 |
new ProductAndCategories( $this ), |
| 34 |
new UsersAndRoles( $this ), |
| 35 |
new Quantity( $this ), |
| 36 |
new Settings( $this ), |
| 37 |
) ); |
| 38 |
} ); |
| 39 |
|
| 40 |
add_action( 'edit_form_after_title', function ( WP_Post $post ) { |
| 41 |
if ( GlobalTieredPricingCPT::SLUG !== $post->post_type ) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
$this->render( $post ); |
| 46 |
} ); |
| 47 |
|
| 48 |
new UpgradeTip(); |
| 49 |
} |
| 50 |
|
| 51 |
protected function includeAssets() { |
| 52 |
?> |
| 53 |
<style> |
| 54 |
/** |
| 55 |
* Externals |
| 56 |
*/ |
| 57 |
/* do not display any notices on rule creation */ |
| 58 |
.wrap .notice:not(.notice-success, .tpt__admin__feedback-discount-banner) { |
| 59 |
display: none |
| 60 |
} |
| 61 |
|
| 62 |
.tpt-global-pricing-rule-form .woocommerce-help-tip { |
| 63 |
margin-left: 5px; |
| 64 |
} |
| 65 |
|
| 66 |
/* tab hint */ |
| 67 |
.tpt-global-pricing-rule-hint { |
| 68 |
display: flex; |
| 69 |
align-items: center; |
| 70 |
padding: 10px 10px; |
| 71 |
border: 1px solid #eee5ed; |
| 72 |
background: #faf6f9; |
| 73 |
color: #814c77 !important; |
| 74 |
margin-bottom: 20px; |
| 75 |
} |
| 76 |
|
| 77 |
.tpt-global-pricing-rule-hint--top-level { |
| 78 |
margin-top: 10px; |
| 79 |
border: 1px solid #888; |
| 80 |
} |
| 81 |
|
| 82 |
.tpt-global-pricing-rule-hint__icon { |
| 83 |
margin-right: 10px; |
| 84 |
} |
| 85 |
|
| 86 |
.tpt-global-pricing-rule-form { |
| 87 |
margin: 20px 0; |
| 88 |
display: flex; |
| 89 |
overflow: hidden; |
| 90 |
border-radius: 3px; |
| 91 |
flex-wrap: nowrap; |
| 92 |
} |
| 93 |
|
| 94 |
.tpt-global-pricing-rule-form__tabs { |
| 95 |
width: 30%; |
| 96 |
max-width: 300px; |
| 97 |
min-width: 250px; |
| 98 |
} |
| 99 |
|
| 100 |
.tpt-global-pricing-rule-form-tab { |
| 101 |
background: #fff; |
| 102 |
border-bottom: 1px solid #e8e8e8; |
| 103 |
border-left: 1px solid #e8e8e8; |
| 104 |
overflow: hidden; |
| 105 |
cursor: pointer; |
| 106 |
display: flex; |
| 107 |
align-items: center; |
| 108 |
padding: 15px 10px; |
| 109 |
} |
| 110 |
|
| 111 |
.tpt-global-pricing-rule-form-tab:first-child { |
| 112 |
border-top: 1px solid #e8e8e8; |
| 113 |
} |
| 114 |
|
| 115 |
.tpt-global-pricing-rule-form-tab:hover:not(.tpt-global-pricing-rule-form-tab--active) { |
| 116 |
background: #fbfbfb; |
| 117 |
} |
| 118 |
|
| 119 |
.tpt-global-pricing-rule-form-tab--active { |
| 120 |
cursor: default; |
| 121 |
background: #faf6f9; |
| 122 |
} |
| 123 |
|
| 124 |
.tpt-global-pricing-rule-form-tab__icon { |
| 125 |
transition: all .1s; |
| 126 |
margin-right: 10px; |
| 127 |
height: 40px; |
| 128 |
aspect-ratio: 1/1; |
| 129 |
border-radius: 50%; |
| 130 |
background: #faf6f9; |
| 131 |
text-align: center; |
| 132 |
color: #814c77; |
| 133 |
font-size: 20px; |
| 134 |
font-weight: bold; |
| 135 |
display: flex; |
| 136 |
justify-content: center; |
| 137 |
align-items: center; |
| 138 |
} |
| 139 |
|
| 140 |
.tpt-global-pricing-rule-form-tab--active h3, |
| 141 |
.tpt-global-pricing-rule-form-tab--active div { |
| 142 |
color: #814c77 !important; |
| 143 |
} |
| 144 |
|
| 145 |
.tpt-global-pricing-rule-form-tab--active .tpt-global-pricing-rule-form-tab__icon { |
| 146 |
background: #fff; |
| 147 |
} |
| 148 |
|
| 149 |
.tpt-global-pricing-rule-form-tab__title h3 { |
| 150 |
font-size: 1.1em; |
| 151 |
margin: 0; |
| 152 |
} |
| 153 |
|
| 154 |
.tpt-global-pricing-rule-form-tab__title div { |
| 155 |
margin-top: 5px; |
| 156 |
color: #777; |
| 157 |
} |
| 158 |
|
| 159 |
.tpt-global-pricing-rule-form-tab-content { |
| 160 |
display: none; |
| 161 |
} |
| 162 |
|
| 163 |
.tpt-global-pricing-rule-form-tab-content--active { |
| 164 |
display: block; |
| 165 |
} |
| 166 |
|
| 167 |
.tpt-global-pricing-rule-form__content { |
| 168 |
width: 70%; |
| 169 |
background: #fff; |
| 170 |
flex-grow: 1; |
| 171 |
padding: 10px; |
| 172 |
border: 1px solid #e8e8e8; |
| 173 |
box-shadow: 0 0 8px rgba(0, 0, 0, .1); |
| 174 |
} |
| 175 |
|
| 176 |
.tpt-global-pricing-rule-form input[type="text"], |
| 177 |
.tpt-global-pricing-rule-form input[type="number"], |
| 178 |
.tpt-global-pricing-rule-form .tiered-pricing-pricing-rules-form-row__inputs { |
| 179 |
width: 75% !important; |
| 180 |
} |
| 181 |
|
| 182 |
.tpt-global-pricing-rule-form #tiered_pricing_type { |
| 183 |
max-width: 75%; |
| 184 |
width: 75% !important; |
| 185 |
} |
| 186 |
|
| 187 |
@media screen and (max-width: 1248px) { |
| 188 |
|
| 189 |
.tpt-global-pricing-rule-form input[type="text"], |
| 190 |
.tpt-global-pricing-rule-form input[type="number"], |
| 191 |
.tpt-global-pricing-rule-form .tiered-pricing-pricing-rules-form-row__inputs { |
| 192 |
width: 100% !important; |
| 193 |
} |
| 194 |
|
| 195 |
.tpt-global-pricing-rule-form #tiered_pricing_type { |
| 196 |
max-width: 100%; |
| 197 |
width: 100% !important; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
.tpt-global-pricing-rule-form { |
| 202 |
flex-wrap: wrap; |
| 203 |
} |
| 204 |
|
| 205 |
.tpt-global-pricing-rule-form__tabs { |
| 206 |
display: flex; |
| 207 |
max-width: 100%; |
| 208 |
width: 100%; |
| 209 |
} |
| 210 |
|
| 211 |
.tpt-global-pricing-rule-form-tab__icon { |
| 212 |
display: none; |
| 213 |
} |
| 214 |
|
| 215 |
.tpt-global-pricing-rule-form-tab--active { |
| 216 |
border-bottom: 3px solid #814c77; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
@media screen and (max-width: 500px) { |
| 221 |
.tiered-pricing-form-block { |
| 222 |
padding: 5px 20px !important; |
| 223 |
} |
| 224 |
} |
| 225 |
</style> |
| 226 |
<script> |
| 227 |
jQuery(document).ready(function () { |
| 228 |
let tabs = jQuery('.tpt-global-pricing-rule-form-tab'); |
| 229 |
let tabsContent = jQuery('.tpt-global-pricing-rule-form-tab-content'); |
| 230 |
|
| 231 |
tabs.click(function (e) { |
| 232 |
e.preventDefault(); |
| 233 |
|
| 234 |
tabsContent.removeClass('tpt-global-pricing-rule-form-tab-content--active'); |
| 235 |
tabs.removeClass('tpt-global-pricing-rule-form-tab--active'); |
| 236 |
|
| 237 |
jQuery(this).addClass('tpt-global-pricing-rule-form-tab--active'); |
| 238 |
|
| 239 |
const target = jQuery(this).data('target'); |
| 240 |
|
| 241 |
jQuery('#' + target).addClass('tpt-global-pricing-rule-form-tab-content--active'); |
| 242 |
}); |
| 243 |
}); |
| 244 |
</script> |
| 245 |
<?php |
| 246 |
} |
| 247 |
|
| 248 |
protected function render( WP_Post $post ) { |
| 249 |
|
| 250 |
$this->includeAssets(); |
| 251 |
|
| 252 |
$rulesCount = (int) wp_count_posts( GlobalTieredPricingCPT::SLUG )->publish; |
| 253 |
|
| 254 |
if ( $this->isNewRule() && $rulesCount < 1 ) { |
| 255 |
$this->renderHelpingSteps(); |
| 256 |
} |
| 257 |
|
| 258 |
if ( ! $this->isNewRule() && ! $this->getPricingRuleInstance( $post )->isValidPricing() ) { |
| 259 |
$this->tabs[0]->renderHint( __( 'The pricing rule does not affect either prices or product quantity limits. The rule will be skipped.', |
| 260 |
'tier-pricing-table' ), array( 'custom_class' => 'tpt-global-pricing-rule-hint--top-level' ) ); |
| 261 |
} |
| 262 |
|
| 263 |
?> |
| 264 |
<div class="tpt-global-pricing-rule-form"> |
| 265 |
|
| 266 |
<nav class="tpt-global-pricing-rule-form__tabs"> |
| 267 |
<?php foreach ( $this->tabs as $tab ) : ?> |
| 268 |
<div class="tpt-global-pricing-rule-form-tab <?php echo esc_attr( $tab->getId() === $this->defaultTab ? 'tpt-global-pricing-rule-form-tab--active' : '' ); ?>" |
| 269 |
data-target="tpt-global-pricing-rule-form-tab-<?php echo esc_attr( $tab->getId() ); ?>"> |
| 270 |
|
| 271 |
<div class="tpt-global-pricing-rule-form-tab__icon" style=""> |
| 272 |
<?php if ( $tab->getIcon() === '$' ) : ?> |
| 273 |
<span>$</span> |
| 274 |
<?php else : ?> |
| 275 |
<span class="dashicons <?php echo esc_attr( $tab->getIcon() ); ?>"></span> |
| 276 |
<?php endif; ?> |
| 277 |
</div> |
| 278 |
|
| 279 |
<div class="tpt-global-pricing-rule-form-tab__title"> |
| 280 |
<h3> |
| 281 |
<?php echo esc_html( $tab->getTitle() ); ?> |
| 282 |
</h3> |
| 283 |
<div><?php echo esc_html( $tab->getDescription() ); ?></div> |
| 284 |
</div> |
| 285 |
</div> |
| 286 |
<?php endforeach; ?> |
| 287 |
</nav> |
| 288 |
|
| 289 |
<section class="tpt-global-pricing-rule-form__content woocommerce_options_panel"> |
| 290 |
<?php foreach ( $this->tabs as $tab ) : ?> |
| 291 |
<div class="tpt-global-pricing-rule-form-tab-content <?php echo esc_attr( $tab->getId() === $this->defaultTab ? 'tpt-global-pricing-rule-form-tab-content--active' : '' ); ?>" |
| 292 |
id="tpt-global-pricing-rule-form-tab-<?php echo esc_attr( $tab->getId() ); ?>"> |
| 293 |
<?php |
| 294 |
$tab->render( $this->getPricingRuleInstance( $post ) ); |
| 295 |
|
| 296 |
do_action( 'tiered_pricing_table/global_pricing/form/tab_end', $tab, |
| 297 |
$this->getPricingRuleInstance( $post ) ); |
| 298 |
?> |
| 299 |
</div> |
| 300 |
<?php endforeach; ?> |
| 301 |
</section> |
| 302 |
</div> |
| 303 |
<?php |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Get pricing rule instance |
| 308 |
* |
| 309 |
* @param WP_Post $post |
| 310 |
* |
| 311 |
* @return GlobalPricingRule |
| 312 |
*/ |
| 313 |
public function getPricingRuleInstance( WP_Post $post ): GlobalPricingRule { |
| 314 |
if ( empty( $this->pricingRuleInstance ) ) { |
| 315 |
$this->pricingRuleInstance = GlobalPricingRule::build( $post->ID ); |
| 316 |
} |
| 317 |
|
| 318 |
return $this->pricingRuleInstance; |
| 319 |
} |
| 320 |
|
| 321 |
public function renderHelpingSteps() { |
| 322 |
?> |
| 323 |
<style> |
| 324 |
.tpt-global-pricing-rule-helping { |
| 325 |
background: #fff; |
| 326 |
border: 1px solid #e8e8e8; |
| 327 |
padding: 20px; |
| 328 |
position: relative; |
| 329 |
text-align: center; |
| 330 |
margin: 20px 0; |
| 331 |
} |
| 332 |
|
| 333 |
.tpt-global-pricing-rule-helping__close { |
| 334 |
position: absolute; |
| 335 |
top: 10px; |
| 336 |
width: 26px; |
| 337 |
height: 26px; |
| 338 |
background: #faf6f9; |
| 339 |
color: #814c77; |
| 340 |
text-align: center; |
| 341 |
line-height: 24px; |
| 342 |
right: 10px; |
| 343 |
font-weight: bold; |
| 344 |
border-radius: 50%; |
| 345 |
} |
| 346 |
|
| 347 |
.tpt-global-pricing-rule-helping__close:hover { |
| 348 |
background: #f3ddee; |
| 349 |
cursor: pointer; |
| 350 |
} |
| 351 |
|
| 352 |
.tpt-global-pricing-rule-helping__title { |
| 353 |
font-size: 1.5em; |
| 354 |
font-weight: bold; |
| 355 |
margin-bottom: 15px; |
| 356 |
} |
| 357 |
|
| 358 |
.tpt-global-pricing-rule-helping__steps { |
| 359 |
justify-content: center; |
| 360 |
gap: 20px; |
| 361 |
display: flex; |
| 362 |
align-items: center; |
| 363 |
margin-top: 30px |
| 364 |
} |
| 365 |
|
| 366 |
.tpt-global-pricing-rule-helping-step--arrow { |
| 367 |
padding: 5px; |
| 368 |
border-radius: 50%; |
| 369 |
background: #faf6f9; |
| 370 |
color: #814c77; |
| 371 |
} |
| 372 |
|
| 373 |
.tpt-global-pricing-rule-helping-step__title { |
| 374 |
font-size: 1.4em; |
| 375 |
font-weight: 600; |
| 376 |
} |
| 377 |
|
| 378 |
.tpt-global-pricing-rule-helping-step__description { |
| 379 |
margin-top: 10px; |
| 380 |
} |
| 381 |
|
| 382 |
.tpt-global-pricing-rule-helping-step__icon, |
| 383 |
.tpt-global-pricing-rule-helping-step__icon span { |
| 384 |
width: 50px; |
| 385 |
height: 50px; |
| 386 |
border-radius: 50%; |
| 387 |
background: #faf6f9; |
| 388 |
margin: 0 auto 15px; |
| 389 |
text-align: center; |
| 390 |
color: #814c77; |
| 391 |
font-size: 25px; |
| 392 |
font-weight: bold; |
| 393 |
line-height: 50px; |
| 394 |
} |
| 395 |
</style> |
| 396 |
<script> |
| 397 |
jQuery(document).ready(function () { |
| 398 |
jQuery('.tpt-global-pricing-rule-helping__close').click(function () { |
| 399 |
jQuery(this).parent().hide(); |
| 400 |
}) |
| 401 |
}) |
| 402 |
</script> |
| 403 |
<?php |
| 404 |
$steps = array( |
| 405 |
array( |
| 406 |
'title' => 'Add pricing', |
| 407 |
'description' => 'Set up custom regular and/or tiered pricing.', |
| 408 |
'icon' => '$', |
| 409 |
'has_next_step' => true, |
| 410 |
), |
| 411 |
array( |
| 412 |
'title' => 'Select products', |
| 413 |
'description' => 'Select products or product categories that the rule will apply to.', |
| 414 |
'icon' => '<span class="dashicons dashicons-archive"></span>', |
| 415 |
'has_next_step' => true, |
| 416 |
), |
| 417 |
array( |
| 418 |
'title' => 'Select users', |
| 419 |
'description' => 'Select users or user roles that the rule will apply to.', |
| 420 |
'icon' => '<span class="dashicons dashicons-admin-users"></span>', |
| 421 |
'has_next_step' => true, |
| 422 |
), |
| 423 |
array( |
| 424 |
'title' => 'Specify quantity', |
| 425 |
'description' => 'Specify the minimum, maximum, and quantity step for products.', |
| 426 |
'icon' => '<span class="dashicons dashicons-database"></span>', |
| 427 |
'has_next_step' => false, |
| 428 |
), |
| 429 |
) |
| 430 |
?> |
| 431 |
<div class="tpt-global-pricing-rule-helping"> |
| 432 |
<div class="tpt-global-pricing-rule-helping__title"> |
| 433 |
<?php esc_html_e( 'How global pricing rules work', 'tier-pricing-table' ); ?> |
| 434 |
</div> |
| 435 |
<p> |
| 436 |
<?php |
| 437 |
esc_html_e( 'Global rules are useful when you need to set custom pricing for multiple products and apply it to a specific group of users.', |
| 438 |
'tier-pricing-table' ); |
| 439 |
?> |
| 440 |
</p> |
| 441 |
|
| 442 |
<p> |
| 443 |
<?php |
| 444 |
esc_html_e( 'Please note that depending on the priority settings, the global pricing rule may override product-level tiered pricing or quantity limits.', |
| 445 |
'tier-pricing-table' ); |
| 446 |
?> |
| 447 |
</p> |
| 448 |
|
| 449 |
<div class="tpt-global-pricing-rule-helping__steps"> |
| 450 |
|
| 451 |
<?php foreach ( $steps as $step ) : ?> |
| 452 |
|
| 453 |
<div class="tpt-global-pricing-rule-helping-step"> |
| 454 |
<div class="tpt-global-pricing-rule-helping-step__icon"> |
| 455 |
<?php echo wp_kses_post( $step['icon'] ); ?> |
| 456 |
</div> |
| 457 |
|
| 458 |
<div class="tpt-global-pricing-rule-helping-step__title"> |
| 459 |
<?php echo esc_html( $step['title'] ); ?> |
| 460 |
</div> |
| 461 |
|
| 462 |
<div class="tpt-global-pricing-rule-helping-step__description"> |
| 463 |
<?php echo esc_html( $step['description'] ); ?> |
| 464 |
</div> |
| 465 |
</div> |
| 466 |
|
| 467 |
<?php if ( $step['has_next_step'] ) : ?> |
| 468 |
<div class="tpt-global-pricing-rule-helping-step tpt-global-pricing-rule-helping-step--arrow"> |
| 469 |
<span class="dashicons dashicons-arrow-right-alt"></span> |
| 470 |
</div> |
| 471 |
<?php endif; ?> |
| 472 |
<?php endforeach; ?> |
| 473 |
</div> |
| 474 |
<div class="tpt-global-pricing-rule-helping__close"> |
| 475 |
× |
| 476 |
</div> |
| 477 |
</div> |
| 478 |
<?php |
| 479 |
} |
| 480 |
|
| 481 |
public function isNewRule(): bool { |
| 482 |
global $pagenow; |
| 483 |
|
| 484 |
return 'post-new.php' == $pagenow; |
| 485 |
} |
| 486 |
} |