PluginProbe
Tiered Pricing Table for WooCommerce / 6.4.0
Tiered Pricing Table for WooCommerce v6.4.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 2.3.3 All 91 releases
tier-pricing-table / src / Managers / FormatPriceManager.php

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

142 lines 5.3 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 $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['html'] ) {
62 $lowestPrice = wc_price( $lowestPrice );
63 }
64 if ( $args['with_suffix'] ) {
65 $lowestPrice .= $product->get_price_suffix();
66 }
67 if ( $args['with_lowest_prefix'] ) {
68 $lowestPrice = self::getLowestPrefix() . ' ' . $lowestPrice;
69 }
70 return $lowestPrice;
71 }
72
73 public static function getPriceRange( WC_Product $product, array $args = array() ) : ?string {
74 $args = wp_parse_args( $args, array(
75 'for_display' => true,
76 'with_suffix' => true,
77 'with_default_price' => true,
78 ) );
79 /**
80 * Variable type declaration for PHPStorm
81 *
82 * @var WC_Product_Variable $product
83 */
84 $lowestPrice = self::getLowestPrice( $product, array(
85 'html' => false,
86 'for_display' => false,
87 'with_lowest_prefix' => false,
88 'with_default_price' => false,
89 'with_suffix' => false,
90 ) );
91 if ( is_null( $lowestPrice ) ) {
92 if ( $args['with_default_price'] ) {
93 return ( $args['html'] ? $product->get_price_html() : $product->get_price() );
94 } else {
95 return null;
96 }
97 }
98 if ( TierPricingTablePlugin::isVariableProductSupported( $product ) ) {
99 $highestPrice = $product->get_variation_price( 'max' );
100 } elseif ( TierPricingTablePlugin::isSimpleProductSupported( $product ) ) {
101 $highestPrice = $product->get_price();
102 } else {
103 return ( $args['with_default_price'] ? $product->get_price() : null );
104 }
105 if ( is_null( $highestPrice ) || $highestPrice === $lowestPrice ) {
106 return ( $args['with_default_price'] ? $product->get_price() : null );
107 }
108 if ( $args['for_display'] ) {
109 $lowestPrice = wc_get_price_to_display( $product, array(
110 'price' => $lowestPrice,
111 ) );
112 $highestPrice = wc_get_price_to_display( $product, array(
113 'price' => $highestPrice,
114 ) );
115 }
116 $lowestPrice = wc_price( $lowestPrice );
117 $highestPrice = wc_price( $highestPrice );
118 if ( $lowestPrice === $highestPrice ) {
119 return ( $args['with_default_price'] ? $product->get_price() : null );
120 }
121 $range = $lowestPrice . ' - ' . $highestPrice;
122 if ( $args['with_suffix'] ) {
123 $range .= $product->get_price_suffix();
124 }
125 if ( isset( $args['html'] ) && !$args['html'] ) {
126 $range = strip_tags( $range );
127 }
128 return $range;
129 }
130
131 public static function getLowestPrefix() : string {
132 $settings = ServiceContainer::getInstance()->getSettings();
133 return (string) $settings->get( 'lowest_prefix', __( 'From', 'tier-pricing-table' ) );
134 }
135
136 public static function getDisplayType() : string {
137 $settings = ServiceContainer::getInstance()->getSettings();
138 return ( $settings->get( 'tiered_price_at_catalog_type', 'range' ) === 'range' ? 'range' : 'lowest' );
139 }
140
141 }
142