PluginProbe
Tiered Pricing Table for WooCommerce / 8.1.0
Tiered Pricing Table for WooCommerce v8.1.0
8.1.0 8.0.2 7.1.7 7.1.5 7.1.4 7.1.1 6.5.0 6.4.0 6.1.0 trunk 1.0 1.1 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 2.3.1 2.3.2 All 92 releases
tier-pricing-table / src / Addons / GlobalTieredPricing / CPT / Columns / Pricing.php

Pricing.php in Tiered Pricing Table for WooCommerce 8.1.0, at src/Addons/GlobalTieredPricing/CPT/Columns/Pricing.php

177 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace TierPricingTable\Addons\GlobalTieredPricing\CPT\Columns;
2
3 use Exception;
4 use TierPricingTable\Addons\GlobalTieredPricing\GlobalPricingRule;
5 use TierPricingTable\Core\ServiceContainerTrait;
6
7 /**
8 * What the rule changes, only when set: base price, the tiers as a small table, quantity limits.
9 */
10 class Pricing {
11
12 use ServiceContainerTrait;
13
14 public function getName(): string {
15 return __( 'Pricing', 'tier-pricing-table' );
16 }
17
18 public function render( GlobalPricingRule $rule ) {
19 try {
20 $rule->validatePricing();
21 } catch ( Exception $e ) {
22 ?>
23 <div class="tpt-rules-list__line tpt-rules-list__warn">
24 <span class="dashicons dashicons-warning"></span>
25 <span><?php echo esc_html( $e->getMessage() ); ?></span>
26 </div>
27 <?php
28 }
29
30 $this->renderBasePrice( $rule );
31 $this->renderTiers( $rule );
32 $this->renderQuantityLimits( $rule );
33 }
34
35 protected function renderBasePrice( GlobalPricingRule $rule ) {
36 $chips = array();
37
38 if ( 'flat' === $rule->getPricingType() ) {
39 if ( $rule->getRegularPrice() ) {
40 $chips[] = __( 'Regular', 'tier-pricing-table' ) . ' <b>' . wc_price( $rule->getRegularPrice() ) . '</b>';
41 }
42 if ( $rule->getSalePrice() ) {
43 $chips[] = __( 'Sale', 'tier-pricing-table' ) . ' <b>' . wc_price( $rule->getSalePrice() ) . '</b>';
44 }
45 } elseif ( $rule->getDiscount() ) {
46 $chips[] = sprintf(
47 /* translators: 1: percentage, 2: "sale price" or "regular price" */
48 __( 'Discount <b>%1$s%%</b> off the %2$s', 'tier-pricing-table' ),
49 esc_html( $rule->getDiscount() ),
50 'sale_price' === $rule->getDiscountType() ? __( 'sale price', 'tier-pricing-table' ) : __( 'regular price', 'tier-pricing-table' )
51 );
52 }
53
54 if ( $chips ) {
55 $this->renderLine( __( 'Base price', 'tier-pricing-table' ), $chips );
56 }
57 }
58
59 /**
60 * The tiers as a small table: quantity ranges (or thresholds, per the plugin's quantity type
61 * setting) and the price or discount, with a base row for the quantities below the first tier.
62 */
63 protected function renderTiers( GlobalPricingRule $rule ) {
64 $percentage = 'percentage' === $rule->getTieredPricingType();
65 $tiers = $percentage ? $rule->getPercentageTieredPricingRules() : $rule->getFixedTieredPricingRules();
66
67 if ( empty( $tiers ) ) {
68 return;
69 }
70
71 ksort( $tiers );
72 $quantities = array_map( 'intval', array_keys( $tiers ) );
73 $values = array_values( $tiers );
74 $minimum = $rule->getMinimum() ? (int) $rule->getMinimum() : 1;
75 $ranges = 'range' === $this->getContainer()->getSettings()->get( 'quantity_type', 'range' );
76
77 // what the customer pays below the first tier
78 $base = __( 'Regular price', 'tier-pricing-table' );
79 if ( 'flat' === $rule->getPricingType() ) {
80 if ( $rule->getSalePrice() ) {
81 $base = wc_price( $rule->getSalePrice() );
82 } elseif ( $rule->getRegularPrice() ) {
83 $base = wc_price( $rule->getRegularPrice() );
84 }
85 } elseif ( $rule->getDiscount() ) {
86 /* translators: %s: percentage */
87 $base = esc_html( sprintf( __( '%s%% off', 'tier-pricing-table' ), $rule->getDiscount() ) );
88 }
89
90 $rows = array();
91 if ( $quantities[0] > $minimum ) {
92 $rows[] = array( $this->quantityLabel( $minimum, $quantities[0] - 1, $ranges ), $base, false );
93 }
94
95 foreach ( $quantities as $index => $quantity ) {
96 $next = $quantities[ $index + 1 ] ?? null;
97 $label = null === $next ? number_format_i18n( $quantity ) . '+' : $this->quantityLabel( $quantity, $next - 1, $ranges );
98 $value = $percentage ? esc_html( $values[ $index ] . '%' ) : wc_price( $values[ $index ] );
99 $rows[] = array( $label, $value, true );
100 }
101
102 $label = sprintf(
103 /* translators: 1: "fixed" or "percentage", 2: "individual" or "mix & match" */
104 __( 'Tiers (%1$s, %2$s)', 'tier-pricing-table' ),
105 $percentage ? __( 'percentage', 'tier-pricing-table' ) : __( 'fixed', 'tier-pricing-table' ),
106 'individual' === $rule->getApplyingType() ? __( 'individual', 'tier-pricing-table' ) : __( 'mix & match', 'tier-pricing-table' )
107 );
108 ?>
109 <div class="tpt-rules-list__line tpt-rules-list__line--chips">
110 <span class="tpt-rules-list__muted"><?php echo esc_html( $label ); ?></span>
111 </div>
112 <table class="tpt-rules-list__tiers">
113 <thead>
114 <tr>
115 <th><?php esc_html_e( 'Quantity', 'tier-pricing-table' ); ?></th>
116 <th><?php echo $percentage ? esc_html__( 'Discount', 'tier-pricing-table' ) : esc_html__( 'Price', 'tier-pricing-table' ); ?></th>
117 </tr>
118 </thead>
119 <tbody>
120 <?php foreach ( $rows as list( $quantityLabel, $value, $isTier ) ) : ?>
121 <tr>
122 <td><?php echo esc_html( $quantityLabel ); ?></td>
123 <td class="<?php echo $isTier ? 'is-tier' : ''; ?>"><?php echo wp_kses_post( $value ); ?></td>
124 </tr>
125 <?php endforeach; ?>
126 </tbody>
127 </table>
128 <?php
129 }
130
131 /**
132 * "10 - 29" with ranges on, "10" otherwise (or when the range is a single quantity).
133 */
134 protected function quantityLabel( int $from, int $to, bool $ranges ): string {
135 if ( $to <= $from || ! $ranges ) {
136 return number_format_i18n( $from );
137 }
138
139 return number_format_i18n( $from ) . ' - ' . number_format_i18n( $to );
140 }
141
142 protected function renderQuantityLimits( GlobalPricingRule $rule ) {
143 $chips = array();
144
145 if ( $rule->getMinimum() ) {
146 /* translators: %s: quantity */
147 $chips[] = sprintf( __( 'Min <b>%s</b>', 'tier-pricing-table' ), esc_html( number_format_i18n( $rule->getMinimum() ) ) );
148 }
149 if ( ! empty( $rule->data['maximum_quantity'] ) ) {
150 /* translators: %s: quantity */
151 $chips[] = sprintf( __( 'Max <b>%s</b>', 'tier-pricing-table' ), esc_html( number_format_i18n( (int) $rule->data['maximum_quantity'] ) ) );
152 }
153 if ( ! empty( $rule->data['group_of_quantity'] ) ) {
154 /* translators: %s: quantity */
155 $chips[] = sprintf( __( 'Step <b>%s</b>', 'tier-pricing-table' ), esc_html( number_format_i18n( (int) $rule->data['group_of_quantity'] ) ) );
156 }
157
158 if ( $chips ) {
159 $this->renderLine( __( 'Quantity', 'tier-pricing-table' ), $chips );
160 }
161 }
162
163 /**
164 * @param string[] $chips Escaped HTML.
165 */
166 protected function renderLine( string $label, array $chips ) {
167 ?>
168 <div class="tpt-rules-list__line tpt-rules-list__line--chips">
169 <span class="tpt-rules-list__muted"><?php echo esc_html( $label ); ?></span>
170 <?php foreach ( $chips as $chip ) : ?>
171 <span class="tpt-rules-list__chip"><?php echo wp_kses_post( $chip ); ?></span>
172 <?php endforeach; ?>
173 </div>
174 <?php
175 }
176 }
177