| 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 |
/** |
| 17 |
* Cache key for a formatted price. |
| 18 |
* |
| 19 |
* The formatted output depends on the display type and formatting flags, and several callers (for example, SEO |
| 20 |
* integrations building schema or title variables) request a plain, prefix-less version early in the request. |
| 21 |
* Keying by these arguments keeps each variant cached separately instead of letting the first caller win. |
| 22 |
* |
| 23 |
* @param string $displayType |
| 24 |
* @param array $args |
| 25 |
* |
| 26 |
* @return string |
| 27 |
*/ |
| 28 |
protected static function getPriceCacheKey( string $displayType, array $args ) : string { |
| 29 |
return 'price_html_' . md5( wp_json_encode( array( |
| 30 |
$displayType, |
| 31 |
(bool) $args['html'], |
| 32 |
(bool) $args['for_display'], |
| 33 |
(bool) $args['with_suffix'], |
| 34 |
(bool) $args['with_lowest_prefix'] |
| 35 |
) ) ); |
| 36 |
} |
| 37 |
|
| 38 |
public static function getFormattedPrice( WC_Product $product, array $args = array() ) : ?string { |
| 39 |
$args = wp_parse_args( $args, array( |
| 40 |
'html' => true, |
| 41 |
'display_type' => null, |
| 42 |
'use_cache' => true, |
| 43 |
'for_display' => true, |
| 44 |
'with_suffix' => true, |
| 45 |
'with_lowest_prefix' => true, |
| 46 |
'with_default_price' => true, |
| 47 |
) ); |
| 48 |
$GLOBALS['TPT_DISABLE_CATALOG_PRICE_FORMAT'] = true; |
| 49 |
try { |
| 50 |
$priceHTML = 'default'; |
| 51 |
if ( 'default' === $priceHTML ) { |
| 52 |
if ( $args['with_default_price'] ) { |
| 53 |
$priceHTML = ( $args['html'] ? $product->get_price_html() : $product->get_price() ); |
| 54 |
} else { |
| 55 |
return null; |
| 56 |
} |
| 57 |
} |
| 58 |
if ( isset( $args['html'] ) && !$args['html'] ) { |
| 59 |
$priceHTML = strip_tags( $priceHTML ); |
| 60 |
} |
| 61 |
return $priceHTML; |
| 62 |
} finally { |
| 63 |
unset($GLOBALS['TPT_DISABLE_CATALOG_PRICE_FORMAT']); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
public static function getLowestPrice( WC_Product $product, $args = array() ) : ?string { |
| 68 |
$args = wp_parse_args( $args, array( |
| 69 |
'html' => true, |
| 70 |
'for_display' => true, |
| 71 |
'with_suffix' => true, |
| 72 |
'with_lowest_prefix' => true, |
| 73 |
'with_default_price' => true, |
| 74 |
) ); |
| 75 |
$lowestPrice = null; |
| 76 |
$regularPrice = null; |
| 77 |
// Handle a case when there are no pricing rules |
| 78 |
if ( !is_numeric( $lowestPrice ) || $lowestPrice < 0 || $lowestPrice >= $regularPrice ) { |
| 79 |
if ( !$args['with_default_price'] ) { |
| 80 |
return null; |
| 81 |
} |
| 82 |
$lowest = $product->get_price(); |
| 83 |
$lowestPrice = ( $args['for_display'] ? wc_get_price_to_display( $product, array( |
| 84 |
'price' => $lowest, |
| 85 |
) ) : $lowest ); |
| 86 |
return ( $args['html'] ? $product->get_price_html() : $lowestPrice ); |
| 87 |
} |
| 88 |
if ( $args['html'] ) { |
| 89 |
$lowestPrice = wc_price( $lowestPrice ); |
| 90 |
} |
| 91 |
if ( $args['with_suffix'] ) { |
| 92 |
$lowestPrice .= $product->get_price_suffix(); |
| 93 |
} |
| 94 |
if ( $args['with_lowest_prefix'] ) { |
| 95 |
$lowestPrice = self::getLowestPrefix() . ' ' . $lowestPrice; |
| 96 |
} |
| 97 |
return $lowestPrice; |
| 98 |
} |
| 99 |
|
| 100 |
public static function getPriceRange( WC_Product $product, array $args = array() ) : ?string { |
| 101 |
$args = wp_parse_args( $args, array( |
| 102 |
'for_display' => true, |
| 103 |
'with_suffix' => true, |
| 104 |
'with_default_price' => true, |
| 105 |
) ); |
| 106 |
/** |
| 107 |
* Variable type declaration for PHPStorm |
| 108 |
* |
| 109 |
* @var WC_Product_Variable $product |
| 110 |
*/ |
| 111 |
$lowestPrice = self::getLowestPrice( $product, array( |
| 112 |
'html' => false, |
| 113 |
'for_display' => false, |
| 114 |
'with_lowest_prefix' => false, |
| 115 |
'with_default_price' => false, |
| 116 |
'with_suffix' => false, |
| 117 |
) ); |
| 118 |
if ( is_null( $lowestPrice ) ) { |
| 119 |
if ( $args['with_default_price'] ) { |
| 120 |
return ( $args['html'] ? $product->get_price_html() : $product->get_price() ); |
| 121 |
} else { |
| 122 |
return null; |
| 123 |
} |
| 124 |
} |
| 125 |
if ( TierPricingTablePlugin::isVariableProductSupported( $product ) ) { |
| 126 |
$highestPrice = $product->get_variation_price( 'max' ); |
| 127 |
} elseif ( TierPricingTablePlugin::isSimpleProductSupported( $product ) ) { |
| 128 |
$highestPrice = $product->get_price(); |
| 129 |
} else { |
| 130 |
return ( $args['with_default_price'] ? $product->get_price() : null ); |
| 131 |
} |
| 132 |
if ( is_null( $highestPrice ) || $highestPrice === $lowestPrice ) { |
| 133 |
return ( $args['with_default_price'] ? $product->get_price() : null ); |
| 134 |
} |
| 135 |
if ( $args['for_display'] ) { |
| 136 |
$lowestPrice = wc_get_price_to_display( $product, array( |
| 137 |
'price' => $lowestPrice, |
| 138 |
) ); |
| 139 |
$highestPrice = wc_get_price_to_display( $product, array( |
| 140 |
'price' => $highestPrice, |
| 141 |
) ); |
| 142 |
} |
| 143 |
$lowestPrice = wc_price( $lowestPrice ); |
| 144 |
$highestPrice = wc_price( $highestPrice ); |
| 145 |
if ( $lowestPrice === $highestPrice ) { |
| 146 |
return ( $args['with_default_price'] ? $product->get_price() : null ); |
| 147 |
} |
| 148 |
$range = $lowestPrice . ' - ' . $highestPrice; |
| 149 |
if ( $args['with_suffix'] ) { |
| 150 |
$range .= $product->get_price_suffix(); |
| 151 |
} |
| 152 |
if ( isset( $args['html'] ) && !$args['html'] ) { |
| 153 |
$range = strip_tags( $range ); |
| 154 |
} |
| 155 |
return $range; |
| 156 |
} |
| 157 |
|
| 158 |
public static function getCustomPrice( WC_Product $product, array $args = array() ) : ?string { |
| 159 |
$template = ServiceContainer::getInstance()->getSettings()->get( 'tiered_price_at_catalog_custom_template', __( 'From {tp_lowest_price} instead of {tp_original_price}', 'tier-pricing-table' ) ); |
| 160 |
if ( empty( $template ) ) { |
| 161 |
return null; |
| 162 |
} |
| 163 |
$lowestPrice = self::getLowestPrice( $product, array( |
| 164 |
'html' => $args['html'], |
| 165 |
'for_display' => $args['for_display'], |
| 166 |
'with_suffix' => $args['with_suffix'], |
| 167 |
'with_lowest_prefix' => false, |
| 168 |
'with_default_price' => false, |
| 169 |
) ); |
| 170 |
if ( is_null( $lowestPrice ) ) { |
| 171 |
return null; |
| 172 |
} |
| 173 |
$priceRange = self::getPriceRange( $product, array( |
| 174 |
'for_display' => $args['for_display'], |
| 175 |
'with_suffix' => $args['with_suffix'], |
| 176 |
'with_default_price' => false, |
| 177 |
'html' => $args['html'], |
| 178 |
) ); |
| 179 |
$originalPrice = ( $args['html'] ? $product->get_price_html() : (( $args['for_display'] ? wc_get_price_to_display( $product ) : $product->get_price() )) ); |
| 180 |
$replacements = array( |
| 181 |
'{tp_lowest_price}' => $lowestPrice, |
| 182 |
'{tp_prices_range}' => ( $priceRange ? $priceRange : $lowestPrice ), |
| 183 |
'{tp_original_price}' => $originalPrice, |
| 184 |
); |
| 185 |
return str_replace( array_keys( $replacements ), array_values( $replacements ), $template ); |
| 186 |
} |
| 187 |
|
| 188 |
public static function getLowestPrefix() : string { |
| 189 |
$settings = ServiceContainer::getInstance()->getSettings(); |
| 190 |
return (string) $settings->get( 'lowest_prefix', __( 'From', 'tier-pricing-table' ) ); |
| 191 |
} |
| 192 |
|
| 193 |
public static function getDisplayType() : string { |
| 194 |
$settings = ServiceContainer::getInstance()->getSettings(); |
| 195 |
$type = $settings->get( 'tiered_price_at_catalog_type', 'range' ); |
| 196 |
if ( 'custom' === $type ) { |
| 197 |
return 'custom'; |
| 198 |
} |
| 199 |
return ( $type === 'range' ? 'range' : 'lowest' ); |
| 200 |
} |
| 201 |
|
| 202 |
} |
| 203 |
|