PluginProbe
Tiered Pricing Table for WooCommerce / 2.3.2
Tiered Pricing Table for WooCommerce v2.3.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 2.3.4 All 90 releases
tier-pricing-table / src / PriceManager.php

PriceManager.php in Tiered Pricing Table for WooCommerce 2.3.2, at src/PriceManager.php

191 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace TierPricingTable;
4
5 class PriceManager
6 {
7 /**
8 * Return fixed price rules or empty array if not exist rules
9 *
10 * @param $product_id
11 *
12 * @return array
13 */
14 public static function getFixedPriceRules( $product_id )
15 {
16 return self::getPriceRules( $product_id, 'fixed' );
17 }
18
19 /**
20 * Return percentage price rules or empty array if not exist rules
21 *
22 * @param $product_id
23 *
24 * @return array
25 */
26 public static function getPercentagePriceRules( $product_id )
27 {
28 return self::getPriceRules( $product_id, 'percentage' );
29 }
30
31 /**
32 * Get product pricing rules
33 *
34 * @param int $product_id
35 * @param bool $type
36 *
37 * @return array
38 */
39 public static function getPriceRules( $product_id, $type = false )
40 {
41 $type = ( $type ? $type : self::getPricingType( $product_id ) );
42
43 if ( $type === 'fixed' ) {
44 $rules = get_post_meta( $product_id, '_fixed_price_rules', true );
45 } else {
46 $rules = get_post_meta( $product_id, '_percentage_price_rules', true );
47 }
48
49 $rules = ( !empty($rules) ? $rules : [] );
50 ksort( $rules );
51 return apply_filters(
52 'tier_pricing_table/price/product_price_rules',
53 $rules,
54 $product_id,
55 $type
56 );
57 }
58
59 /**
60 * Get price by product quantity
61 *
62 * @param int $quantity
63 * @param int $product_id
64 *
65 * @return bool|float|int
66 */
67 public static function getPriceByRules( $quantity, $product_id )
68 {
69 $rules = self::getPriceRules( $product_id );
70 $type = self::getPricingType( $product_id );
71 if ( $type === 'fixed' ) {
72 foreach ( array_reverse( $rules, true ) as $_amount => $price ) {
73
74 if ( $_amount <= $quantity ) {
75 $product_price = wc_get_price_to_display( wc_get_product( $product_id ), [
76 'price' => $price,
77 'qty' => 1,
78 ] );
79 break;
80 }
81
82 }
83 }
84
85 if ( $type === 'percentage' ) {
86 $product = wc_get_product( $product_id );
87 foreach ( array_reverse( $rules, true ) as $_amount => $percentDiscount ) {
88
89 if ( $_amount <= $quantity ) {
90 $product_price = wc_get_price_to_display( $product, [
91 'price' => self::getPriceByPercentDiscount( $product->get_price(), $percentDiscount ),
92 'qty' => 1,
93 ] );
94 break;
95 }
96
97 }
98 }
99
100 $product_price = ( isset( $product_price ) ? $product_price : false );
101 return apply_filters(
102 'tier_pricing_table/price/price_by_rules',
103 $product_price,
104 $quantity,
105 $product_id
106 );
107 }
108
109 /**
110 * Calculate price using percentage discount
111 *
112 * @param float|int $price
113 * @param float|int $discount
114 *
115 * @return bool|float|int
116 */
117 public static function getPriceByPercentDiscount( $price, $discount )
118 {
119
120 if ( $price > 0 && $discount <= 100 ) {
121 $discount_amount = $price / 100 * $discount;
122 return $price - $discount_amount;
123 }
124
125 return false;
126 }
127
128 /**
129 * Get pricing type of product. Available: fixed or percentage
130 *
131 * @param int $product_id
132 *
133 * @param string $default
134 *
135 * @return string
136 */
137 public static function getPricingType( $product_id, $default = 'fixed' )
138 {
139 return 'fixed';
140 }
141
142 /**
143 * Update price rules
144 *
145 * @param array $amounts
146 * @param array $prices
147 * @param int $post_id
148 */
149 public static function updateFixedPriceRules( $amounts, $prices, $post_id )
150 {
151 $rules = [];
152 foreach ( $amounts as $key => $amount ) {
153 if ( !empty($amount) && !empty($prices[$key]) && !key_exists( $amount, $rules ) ) {
154 $rules[$amount] = wc_format_decimal( $prices[$key] );
155 }
156 }
157 update_post_meta( $post_id, '_fixed_price_rules', $rules );
158 }
159
160 /**
161 * Update price rules
162 *
163 * @param array $amounts
164 * @param array $percents
165 * @param int $post_id
166 */
167 public static function updatePercentagePriceRules( $amounts, $percents, $post_id )
168 {
169 $rules = [];
170 foreach ( $amounts as $key => $amount ) {
171 if ( !empty($amount) && !empty($percents[$key]) && !key_exists( $amount, $rules ) && $percents[$key] < 99 ) {
172 $rules[$amount] = $percents[$key];
173 }
174 }
175 update_post_meta( $post_id, '_percentage_price_rules', $rules );
176 }
177
178 /**
179 * Update product pricing type
180 *
181 * @param int $post_id
182 * @param string $type
183 */
184 public static function updatePriceRulesType( $post_id, $type )
185 {
186 if ( in_array( $type, [ 'percentage', 'fixed' ] ) ) {
187 update_post_meta( $post_id, '_tiered_price_rules_type', $type );
188 }
189 }
190
191 }