| 1 |
<?php |
| 2 |
|
| 3 |
use TierPricingTable\Forms\Form; |
| 4 |
|
| 5 |
if ( ! defined( 'WPINC' ) ) { |
| 6 |
die; |
| 7 |
} |
| 8 |
|
| 9 |
/** |
| 10 |
* @var string $entity_id |
| 11 |
* @var ?string $role |
| 12 |
* @var ?string $loop |
| 13 |
* @var ?string $custom_prefix |
| 14 |
* @var string $tiered_pricing_type |
| 15 |
* @var array $percentage_rules |
| 16 |
* @var array $fixed_rules |
| 17 |
*/ |
| 18 |
|
| 19 |
// Ensure arrays and apply filters |
| 20 |
$percentage_rules = is_array( $percentage_rules ) ? $percentage_rules : array(); |
| 21 |
$fixed_rules = is_array( $fixed_rules ) ? $fixed_rules : array(); |
| 22 |
$fieldsWidth = apply_filters( 'tiered_pricing_table/admin/tiered_pricing_rules_form/inputs_width', 50 ); |
| 23 |
|
| 24 |
// Define types to loop through for the UI blocks |
| 25 |
$pricing_blocks = array( |
| 26 |
'fixed' => $fixed_rules, |
| 27 |
'percentage' => $percentage_rules, |
| 28 |
); |
| 29 |
|
| 30 |
/** |
| 31 |
* Helper function to render a single rule row. |
| 32 |
*/ |
| 33 |
if ( ! function_exists( 'tpt_render_pricing_row' ) ) { |
| 34 |
function tpt_render_pricing_row( |
| 35 |
$amount, |
| 36 |
$value, |
| 37 |
$type, |
| 38 |
$role, |
| 39 |
$loop, |
| 40 |
$custom_prefix, |
| 41 |
$fieldsWidth, |
| 42 |
$entity_id |
| 43 |
) { |
| 44 |
$is_fixed = ( 'fixed' === $type ); |
| 45 |
|
| 46 |
// Map field keys based on type |
| 47 |
$qty_key = $is_fixed ? 'fixed_quantities' : 'percentage_quantities'; |
| 48 |
$val_key = $is_fixed ? 'fixed_prices' : 'percentage_discounts'; |
| 49 |
|
| 50 |
$qty_name = Form::getFieldName( $qty_key, $role, $loop, $custom_prefix ) . '[]'; |
| 51 |
$val_name = Form::getFieldName( $val_key, $role, $loop, $custom_prefix ) . '[]'; |
| 52 |
|
| 53 |
// Format value and attributes |
| 54 |
$val_display = $is_fixed ? wc_format_localized_price( $value ) : $value; |
| 55 |
$placeholder = $is_fixed ? __( 'Price', 'tier-pricing-table' ) : __( 'Percentage discount', |
| 56 |
'tier-pricing-table' ); |
| 57 |
?> |
| 58 |
<div class="tiered-pricing-pricing-rules-form-row"> |
| 59 |
<div class="tiered-pricing-pricing-rules-form-row__inputs" |
| 60 |
style="width: <?php echo esc_attr( $fieldsWidth ); ?>%;"> |
| 61 |
|
| 62 |
<input type="number" |
| 63 |
min="2" |
| 64 |
value="<?php echo esc_attr( $amount ); ?>" |
| 65 |
placeholder="<?php esc_attr_e( 'Quantity', 'tier-pricing-table' ); ?>" |
| 66 |
name="<?php echo esc_attr( $qty_name ); ?>"> |
| 67 |
|
| 68 |
<input type="<?php echo $is_fixed ? 'text' : 'number'; ?>" |
| 69 |
<?php echo ! $is_fixed ? 'max="99"' : ''; ?> |
| 70 |
step="any" |
| 71 |
class="<?php echo $is_fixed ? 'wc_input_price' : ''; ?>" |
| 72 |
value="<?php echo esc_attr( $val_display ); ?>" |
| 73 |
placeholder="<?php echo esc_attr( $placeholder ); ?>" |
| 74 |
name="<?php echo esc_attr( $val_name ); ?>"> |
| 75 |
|
| 76 |
<?php |
| 77 |
do_action( 'tiered_pricing_table/admin/tiered_pricing_rules_form/inputs', $entity_id, $amount, |
| 78 |
$role, $loop, $custom_prefix, $type ); |
| 79 |
?> |
| 80 |
</div> |
| 81 |
<div class="tiered-pricing-pricing-rules-form-row__actions"> |
| 82 |
<button type="button" class="notice-dismiss tiered-pricing-pricing-rules-form__remove"></button> |
| 83 |
</div> |
| 84 |
</div> |
| 85 |
<?php |
| 86 |
} |
| 87 |
} |
| 88 |
?> |
| 89 |
|
| 90 |
<div class="tiered-pricing-rules-form"> |
| 91 |
|
| 92 |
<?php do_action( 'tiered_pricing_table/admin/tiered_pricing_rules_form/form_begin', $entity_id, $role, $loop, |
| 93 |
$custom_prefix ); ?> |
| 94 |
|
| 95 |
<div class="<?php echo esc_attr( is_null( $loop ) ? 'tiered-pricing-form-block' : 'tiered-pricing-form-variation-block' ); ?> tiered-pricing-rules-form__type"> |
| 96 |
|
| 97 |
<label for="<?php echo esc_attr( Form::getFieldName( 'type', $role, $loop, $custom_prefix ) ); ?>"> |
| 98 |
<?php esc_html_e( 'Tiered pricing type', 'tier-pricing-table' ); ?> |
| 99 |
</label> |
| 100 |
|
| 101 |
<select name="<?php echo esc_attr( Form::getFieldName( 'type', $role, $loop, $custom_prefix ) ); ?>" |
| 102 |
id="<?php echo esc_attr( Form::getFieldName( 'type', $role, $loop, $custom_prefix ) ); ?>"> |
| 103 |
|
| 104 |
<option value="fixed" <?php selected( 'fixed', $tiered_pricing_type ); ?>> |
| 105 |
<?php esc_html_e( 'Fixed prices', 'tier-pricing-table' ); ?> |
| 106 |
</option> |
| 107 |
|
| 108 |
<option value="percentage" <?php selected( 'percentage', |
| 109 |
$tiered_pricing_type ); ?> <?php disabled( ! tpt_fs()->can_use_premium_code() ); ?>> |
| 110 |
<?php echo tpt_fs()->can_use_premium_code() ? esc_html__( 'Percentage discounts', |
| 111 |
'tier-pricing-table' ) : esc_html__( 'Percentage discounts (only in premium version)', |
| 112 |
'tier-pricing-table' ); ?> |
| 113 |
</option> |
| 114 |
</select> |
| 115 |
</div> |
| 116 |
|
| 117 |
<?php do_action( 'tiered_pricing_table/admin/tiered_pricing_rules_form/after_pricing_type', $entity_id, $role, |
| 118 |
$loop, $custom_prefix ); ?> |
| 119 |
|
| 120 |
<?php foreach ( $pricing_blocks as $type => $rules ) : ?> |
| 121 |
<div class="<?php echo esc_attr( is_null( $loop ) ? 'tiered-pricing-form-block' : 'tiered-pricing-form-variation-block' ); ?> |
| 122 |
tiered-pricing-rules-form__<?php echo esc_attr( $type ); ?> |
| 123 |
<?php echo $type !== $tiered_pricing_type ? 'hidden' : ''; ?>"> |
| 124 |
|
| 125 |
<label><?php esc_html_e( 'Tiered price', 'tier-pricing-table' ); ?></label> |
| 126 |
|
| 127 |
<div class="tiered-pricing-pricing-rules-form" role="form"> |
| 128 |
<div class="tiered-pricing-pricing-rules-form__rules"> |
| 129 |
<?php |
| 130 |
foreach ( $rules as $amount => $value ) { |
| 131 |
tpt_render_pricing_row( $amount, $value, $type, $role, $loop, $custom_prefix, |
| 132 |
$fieldsWidth, $entity_id ); |
| 133 |
} |
| 134 |
tpt_render_pricing_row( '', '', $type, $role, $loop, $custom_prefix, $fieldsWidth, |
| 135 |
$entity_id ); |
| 136 |
?> |
| 137 |
</div> |
| 138 |
|
| 139 |
<div class="tiered-pricing-pricing-rules-form__buttons"> |
| 140 |
<button type="button" class="button tiered-pricing-pricing-rules-form__add-new"> |
| 141 |
<?php esc_html_e( 'New tier', 'tier-pricing-table' ); ?> |
| 142 |
</button> |
| 143 |
</div> |
| 144 |
</div> |
| 145 |
</div> |
| 146 |
<?php endforeach; ?> |
| 147 |
</div> |
| 148 |
<?php |
| 149 |
|