PluginProbe
Tiered Pricing Table for WooCommerce / trunk
Tiered Pricing Table for WooCommerce vtrunk
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 2.3.3 2.3.4 All 90 releases
tier-pricing-table / src / Managers / FormatPriceManager.php

FormatPriceManager.php in Tiered Pricing Table for WooCommerce trunk, at src/Managers/FormatPriceManager.php

181 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace TierPricingTable\Managers;
4
5 use TierPricingTable\Core\ServiceContainer;
6 use TierPricingTable\PriceManager;
7 use TierPricingTable\TierPricingTablePlugin;
8 use WC_Product;
9 use WC_Product_Variable;
10 /**
11 * Class FormatPriceManager
12 *
13 * @package TierPricingTable\Managers
14 */
15 class FormatPriceManager {
16 public static function getFormattedPrice( WC_Product $product, array $args = array() ) : ?string {
17 $args = wp_parse_args( $args, array(
18 'html' => true,
19 'display_type' => null,
20 'use_cache' => true,
21 'for_display' => true,
22 'with_suffix' => true,
23 'with_lowest_prefix' => true,
24 'with_default_price' => true,
25 ) );
26 $GLOBALS['TPT_DISABLE_CATALOG_PRICE_FORMAT'] = true;
27 try {
28 $priceHTML = 'default';
29 if ( 'default' === $priceHTML ) {
30 if ( $args['with_default_price'] ) {
31 $priceHTML = ( $args['html'] ? $product->get_price_html() : $product->get_price() );
32 } else {
33 return null;
34 }
35 }
36 if ( isset( $args['html'] ) && !$args['html'] ) {
37 $priceHTML = strip_tags( $priceHTML );
38 }
39 return $priceHTML;
40 } finally {
41 unset($GLOBALS['TPT_DISABLE_CATALOG_PRICE_FORMAT']);
42 }
43 }
44
45 public static function getLowestPrice( WC_Product $product, $args = array() ) : ?string {
46 $args = wp_parse_args( $args, array(
47 'html' => true,
48 'for_display' => true,
49 'with_suffix' => true,
50 'with_lowest_prefix' => true,
51 'with_default_price' => true,
52 ) );
53 $lowestPrice = null;
54 $regularPrice = null;
55 // Handle a case when there are no pricing rules
56 if ( !is_numeric( $lowestPrice ) || $lowestPrice < 0 || $lowestPrice >= $regularPrice ) {
57 if ( !$args['with_default_price'] ) {
58 return null;
59 }
60 $lowest = $product->get_price();
61 $lowestPrice = ( $args['for_display'] ? wc_get_price_to_display( $product, array(
62 'price' => $lowest,
63 ) ) : $lowest );
64 return ( $args['html'] ? $product->get_price_html() : $lowestPrice );
65 }
66 if ( $args['html'] ) {
67 $lowestPrice = wc_price( $lowestPrice );
68 }
69 if ( $args['with_suffix'] ) {
70 $lowestPrice .= $product->get_price_suffix();
71 }
72 if ( $args['with_lowest_prefix'] ) {
73 $lowestPrice = self::getLowestPrefix() . ' ' . $lowestPrice;
74 }
75 return $lowestPrice;
76 }
77
78 public static function getPriceRange( WC_Product $product, array $args = array() ) : ?string {
79 $args = wp_parse_args( $args, array(
80 'for_display' => true,
81 'with_suffix' => true,
82 'with_default_price' => true,
83 ) );
84 /**
85 * Variable type declaration for PHPStorm
86 *
87 * @var WC_Product_Variable $product
88 */
89 $lowestPrice = self::getLowestPrice( $product, array(
90 'html' => false,
91 'for_display' => false,
92 'with_lowest_prefix' => false,
93 'with_default_price' => false,
94 'with_suffix' => false,
95 ) );
96 if ( is_null( $lowestPrice ) ) {
97 if ( $args['with_default_price'] ) {
98 return ( $args['html'] ? $product->get_price_html() : $product->get_price() );
99 } else {
100 return null;
101 }
102 }
103 if ( TierPricingTablePlugin::isVariableProductSupported( $product ) ) {
104 $highestPrice = $product->get_variation_price( 'max' );
105 } elseif ( TierPricingTablePlugin::isSimpleProductSupported( $product ) ) {
106 $highestPrice = $product->get_price();
107 } else {
108 return ( $args['with_default_price'] ? $product->get_price() : null );
109 }
110 if ( is_null( $highestPrice ) || $highestPrice === $lowestPrice ) {
111 return ( $args['with_default_price'] ? $product->get_price() : null );
112 }
113 if ( $args['for_display'] ) {
114 $lowestPrice = wc_get_price_to_display( $product, array(
115 'price' => $lowestPrice,
116 ) );
117 $highestPrice = wc_get_price_to_display( $product, array(
118 'price' => $highestPrice,
119 ) );
120 }
121 $lowestPrice = wc_price( $lowestPrice );
122 $highestPrice = wc_price( $highestPrice );
123 if ( $lowestPrice === $highestPrice ) {
124 return ( $args['with_default_price'] ? $product->get_price() : null );
125 }
126 $range = $lowestPrice . ' - ' . $highestPrice;
127 if ( $args['with_suffix'] ) {
128 $range .= $product->get_price_suffix();
129 }
130 if ( isset( $args['html'] ) && !$args['html'] ) {
131 $range = strip_tags( $range );
132 }
133 return $range;
134 }
135
136 public static function getCustomPrice( WC_Product $product, array $args = array() ) : ?string {
137 $template = ServiceContainer::getInstance()->getSettings()->get( 'tiered_price_at_catalog_custom_template', __( 'From {tp_lowest_price} instead of {tp_original_price}', 'tier-pricing-table' ) );
138 if ( empty( $template ) ) {
139 return null;
140 }
141 $lowestPrice = self::getLowestPrice( $product, array(
142 'html' => $args['html'],
143 'for_display' => $args['for_display'],
144 'with_suffix' => $args['with_suffix'],
145 'with_lowest_prefix' => false,
146 'with_default_price' => false,
147 ) );
148 if ( is_null( $lowestPrice ) ) {
149 return null;
150 }
151 $priceRange = self::getPriceRange( $product, array(
152 'for_display' => $args['for_display'],
153 'with_suffix' => $args['with_suffix'],
154 'with_default_price' => false,
155 'html' => $args['html'],
156 ) );
157 $originalPrice = ( $args['html'] ? $product->get_price_html() : (( $args['for_display'] ? wc_get_price_to_display( $product ) : $product->get_price() )) );
158 $replacements = array(
159 '{tp_lowest_price}' => $lowestPrice,
160 '{tp_prices_range}' => ( $priceRange ? $priceRange : $lowestPrice ),
161 '{tp_original_price}' => $originalPrice,
162 );
163 return str_replace( array_keys( $replacements ), array_values( $replacements ), $template );
164 }
165
166 public static function getLowestPrefix() : string {
167 $settings = ServiceContainer::getInstance()->getSettings();
168 return (string) $settings->get( 'lowest_prefix', __( 'From', 'tier-pricing-table' ) );
169 }
170
171 public static function getDisplayType() : string {
172 $settings = ServiceContainer::getInstance()->getSettings();
173 $type = $settings->get( 'tiered_price_at_catalog_type', 'range' );
174 if ( 'custom' === $type ) {
175 return 'custom';
176 }
177 return ( $type === 'range' ? 'range' : 'lowest' );
178 }
179
180 }
181