| 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 |
$priceHTML = 'default'; |
| 27 |
if ( 'default' === $priceHTML ) { |
| 28 |
if ( $args['with_default_price'] ) { |
| 29 |
$priceHTML = ( $args['html'] ? $product->get_price_html() : $product->get_price() ); |
| 30 |
} else { |
| 31 |
return null; |
| 32 |
} |
| 33 |
} |
| 34 |
if ( isset( $args['html'] ) && !$args['html'] ) { |
| 35 |
$priceHTML = strip_tags( $priceHTML ); |
| 36 |
} |
| 37 |
return $priceHTML; |
| 38 |
} |
| 39 |
|
| 40 |
public static function getLowestPrice( WC_Product $product, $args = array() ) : ?string { |
| 41 |
$args = wp_parse_args( $args, array( |
| 42 |
'html' => true, |
| 43 |
'for_display' => true, |
| 44 |
'with_suffix' => true, |
| 45 |
'with_lowest_prefix' => true, |
| 46 |
'with_default_price' => true, |
| 47 |
) ); |
| 48 |
$lowestPrice = null; |
| 49 |
$regularPrice = null; |
| 50 |
// Handle a case when there are no pricing rules |
| 51 |
if ( !is_numeric( $lowestPrice ) || $lowestPrice < 0 || $lowestPrice >= $regularPrice ) { |
| 52 |
if ( !$args['with_default_price'] ) { |
| 53 |
return null; |
| 54 |
} |
| 55 |
$lowest = $product->get_price(); |
| 56 |
$lowestPrice = ( $args['for_display'] ? wc_get_price_to_display( $product, array( |
| 57 |
'price' => $lowest, |
| 58 |
) ) : $lowest ); |
| 59 |
return ( $args['html'] ? $product->get_price_html() : $lowestPrice ); |
| 60 |
} |
| 61 |
if ( $args['for_display'] ) { |
| 62 |
$lowestPrice = wc_get_price_to_display( $product, array( |
| 63 |
'price' => $lowestPrice, |
| 64 |
) ); |
| 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 getLowestPrefix() : string { |
| 137 |
$settings = ServiceContainer::getInstance()->getSettings(); |
| 138 |
return (string) $settings->get( 'lowest_prefix', __( 'From', 'tier-pricing-table' ) ); |
| 139 |
} |
| 140 |
|
| 141 |
public static function getDisplayType() : string { |
| 142 |
$settings = ServiceContainer::getInstance()->getSettings(); |
| 143 |
return ( $settings->get( 'tiered_price_at_catalog_type', 'range' ) === 'range' ? 'range' : 'lowest' ); |
| 144 |
} |
| 145 |
|
| 146 |
} |
| 147 |
|