tier-pricing-table
/
src
/
Settings
/
Sections
/
GeneralSection
/
Subsections
/
CalculationSubsection.php
CalculationSubsection.php in Tiered Pricing Table for WooCommerce 8.0.2, at src/Settings/Sections/GeneralSection/Subsections/CalculationSubsection.php
| 1 | <?php namespace TierPricingTable\Settings\Sections\GeneralSection\Subsections; |
| 2 | |
| 3 | use TierPricingTable\Settings\CustomOptions\TPTSwitchOption; |
| 4 | use TierPricingTable\Settings\Sections\SubsectionAbstract; |
| 5 | use TierPricingTable\Settings\Settings; |
| 6 | |
| 7 | /** |
| 8 | * How tiered prices are calculated. Formerly the "Calculations" tab; the option ids are unchanged. |
| 9 | */ |
| 10 | class CalculationSubsection extends SubsectionAbstract { |
| 11 | |
| 12 | public function getTitle(): string { |
| 13 | return __( 'Price Calculation', 'tier-pricing-table' ); |
| 14 | } |
| 15 | |
| 16 | public function getDescription(): string { |
| 17 | return __( 'How tiered prices are calculated for variable products, sale prices and global rules.', 'tier-pricing-table' ); |
| 18 | } |
| 19 | |
| 20 | public function getSlug(): string { |
| 21 | return 'calculation'; |
| 22 | } |
| 23 | |
| 24 | public function getSettings(): array { |
| 25 | $price = function ( $amount, ?int $decimals = null ) { |
| 26 | $args = null === $decimals ? array() : array( 'decimals' => $decimals ); |
| 27 | |
| 28 | return html_entity_decode( wp_strip_all_tags( wc_price( $amount, $args ) ), ENT_QUOTES, 'UTF-8' ); |
| 29 | }; |
| 30 | |
| 31 | $settings = apply_filters( 'tiered_pricing_table/settings/calculation_logic', array( |
| 32 | array( |
| 33 | 'title' => __( 'Combine variations for quantity calculation', 'tier-pricing-table' ), |
| 34 | 'id' => Settings::SETTINGS_PREFIX . 'summarize_variations', |
| 35 | 'type' => TPTSwitchOption::FIELD_TYPE, |
| 36 | 'default' => 'no', |
| 37 | 'extended_description' => __( 'Treat all variations of a variable product as the same item when calculating the total cart quantity for tiered pricing rules.', 'tier-pricing-table' ) |
| 38 | . $this->example( 'tiers', |
| 39 | array( |
| 40 | 'scene' => __( 'Red × 3 and Blue × 4 in the cart count separately: 3 and 4.', 'tier-pricing-table' ), |
| 41 | 'result' => __( 'Below the 5+ tier, no discount', 'tier-pricing-table' ), |
| 42 | 'tone' => 'bad', |
| 43 | ), |
| 44 | array( |
| 45 | 'scene' => __( 'Red × 3 and Blue × 4 in the cart count together: 7.', 'tier-pricing-table' ), |
| 46 | 'result' => __( '5+ tier reached: 10% off', 'tier-pricing-table' ), |
| 47 | 'tone' => 'good', |
| 48 | ) |
| 49 | ), |
| 50 | 'desc_tip' => true, |
| 51 | ), |
| 52 | array( |
| 53 | 'title' => __( 'Combine variations for minimum quantity', 'tier-pricing-table' ), |
| 54 | 'id' => Settings::SETTINGS_PREFIX . 'mix_and_match_minimum', |
| 55 | 'type' => TPTSwitchOption::FIELD_TYPE, |
| 56 | 'default' => 'no', |
| 57 | 'extended_description' => __( 'Determine whether the minimum quantity requirement applies to each variation separately, or to the combined total of all variations in the cart.', 'tier-pricing-table' ) |
| 58 | . $this->example( 'minimum', |
| 59 | array( |
| 60 | 'scene' => __( 'Minimum 10. Red × 6 and Blue × 4 each need 10 on their own.', 'tier-pricing-table' ), |
| 61 | 'result' => __( 'Minimum not met, cannot add to cart', 'tier-pricing-table' ), |
| 62 | 'tone' => 'bad', |
| 63 | ), |
| 64 | array( |
| 65 | 'scene' => __( 'Minimum 10. Red × 6 and Blue × 4 add up to 10.', 'tier-pricing-table' ), |
| 66 | 'result' => __( 'Minimum met', 'tier-pricing-table' ), |
| 67 | 'tone' => 'good', |
| 68 | ) |
| 69 | ), |
| 70 | 'desc_tip' => true, |
| 71 | ), |
| 72 | array( |
| 73 | 'title' => __( 'Calculate percentage discounts from regular price', 'tier-pricing-table' ), |
| 74 | 'id' => Settings::SETTINGS_PREFIX . 'calculate_discount_based_on_regular_price', |
| 75 | 'type' => TPTSwitchOption::FIELD_TYPE, |
| 76 | 'extended_description' => __( 'Calculate percentage discounts using the product\'s regular price, ignoring any active sale prices.', 'tier-pricing-table' ) |
| 77 | . $this->example( 'flow', |
| 78 | array( |
| 79 | /* translators: 1: regular price, 2: sale price */ |
| 80 | 'scene' => sprintf( __( 'Regular price %1$s, on sale for %2$s. The 20%% tier starts from the sale price.', 'tier-pricing-table' ), $price( 100 ), $price( 90 ) ), |
| 81 | /* translators: %s: tier price */ |
| 82 | 'result' => sprintf( __( 'Tier price %s', 'tier-pricing-table' ), $price( 72 ) ), |
| 83 | ), |
| 84 | array( |
| 85 | /* translators: 1: regular price, 2: sale price */ |
| 86 | 'scene' => sprintf( __( 'Regular price %1$s, on sale for %2$s. The 20%% tier starts from the regular price.', 'tier-pricing-table' ), $price( 100 ), $price( 90 ) ), |
| 87 | /* translators: %s: tier price */ |
| 88 | 'result' => sprintf( __( 'Tier price %s', 'tier-pricing-table' ), $price( 80 ) ), |
| 89 | ) |
| 90 | ), |
| 91 | 'default' => 'no', |
| 92 | ), |
| 93 | array( |
| 94 | 'title' => __( 'Round calculated prices', 'tier-pricing-table' ), |
| 95 | 'id' => Settings::SETTINGS_PREFIX . 'round_price', |
| 96 | 'type' => TPTSwitchOption::FIELD_TYPE, |
| 97 | 'default' => 'yes', |
| 98 | 'extended_description' => __( 'Round calculated percentage discounts to prevent minor display discrepancies with standard WooCommerce pricing.', 'tier-pricing-table' ) |
| 99 | . $this->example( 'round', |
| 100 | array( |
| 101 | /* translators: %s: price */ |
| 102 | 'scene' => sprintf( __( '%s with a 15%% tier comes to 16.9915.', 'tier-pricing-table' ), $price( 19.99 ) ), |
| 103 | 'result' => __( 'The exact amount is used', 'tier-pricing-table' ), |
| 104 | ), |
| 105 | array( |
| 106 | /* translators: %s: price */ |
| 107 | 'scene' => sprintf( __( '%s with a 15%% tier comes to 16.9915.', 'tier-pricing-table' ), $price( 19.99 ) ), |
| 108 | /* translators: %s: rounded price */ |
| 109 | 'result' => sprintf( __( 'Rounded to %s', 'tier-pricing-table' ), $price( 16.99 ) ), |
| 110 | ) |
| 111 | ), |
| 112 | 'desc_tip' => true, |
| 113 | ), |
| 114 | array( |
| 115 | 'title' => __( 'Prioritize global pricing rules', 'tier-pricing-table' ), |
| 116 | 'id' => Settings::SETTINGS_PREFIX . 'override_prices_by_global_rules', |
| 117 | 'extended_description' => __( 'Apply global pricing rules before product-level rules. If disabled, individual product rules take precedence over global rules.', 'tier-pricing-table' ) |
| 118 | . $this->example( 'rules', |
| 119 | array( |
| 120 | 'scene' => __( 'A product rule gives 10% off from 10 items, a global rule 15% off from 10 items.', 'tier-pricing-table' ), |
| 121 | 'result' => __( 'The product rule wins: 10% off', 'tier-pricing-table' ), |
| 122 | ), |
| 123 | array( |
| 124 | 'scene' => __( 'A product rule gives 10% off from 10 items, a global rule 15% off from 10 items.', 'tier-pricing-table' ), |
| 125 | 'result' => __( 'The global rule wins: 15% off', 'tier-pricing-table' ), |
| 126 | ) |
| 127 | ), |
| 128 | 'type' => TPTSwitchOption::FIELD_TYPE, |
| 129 | 'default' => 'no', |
| 130 | ), |
| 131 | ) ); |
| 132 | |
| 133 | // one group inside the General tab: no nested titles from the old tab's layout |
| 134 | return array_values( array_filter( $settings, function ( $field ) { |
| 135 | return ! in_array( $field['type'] ?? '', array( 'title', 'sectionend' ), true ); |
| 136 | } ) ); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * An "Off / On" comparison under an option: the same situation with the option off and on, one |
| 141 | * sentence each plus the outcome. The settings script highlights the case matching the switch. |
| 142 | * |
| 143 | * @param string $type example type (adds a modifier class) |
| 144 | * @param array $off scene (text), result (text), tone (good|bad|null) |
| 145 | * @param array $on same shape |
| 146 | */ |
| 147 | protected function example( string $type, array $off, array $on ): string { |
| 148 | $case = function ( array $case, string $label, bool $on ) { |
| 149 | $tone = $case['tone'] ?? ''; |
| 150 | |
| 151 | return '<div class="tpt-example__case ' . ( $on ? 'tpt-example__case--on' : 'tpt-example__case--off' ) . '">' |
| 152 | . '<span class="tpt-example__badge">' . esc_html( $label ) . '</span>' |
| 153 | . '<span class="tpt-example__text">' . esc_html( $case['scene'] ) . '</span>' |
| 154 | . '<span class="tpt-example__result' . ( $tone ? ' tpt-example__result--' . $tone : '' ) . '">' . esc_html( $case['result'] ?? '' ) . '</span>' |
| 155 | . '</div>'; |
| 156 | }; |
| 157 | |
| 158 | return '<div class="tpt-example tpt-example--' . esc_attr( $type ) . '">' |
| 159 | . $case( $off, __( 'Off', 'tier-pricing-table' ), false ) |
| 160 | . $case( $on, __( 'On', 'tier-pricing-table' ), true ) |
| 161 | . '</div>'; |
| 162 | } |
| 163 | |
| 164 | } |
| 165 |