PluginProbe
Tiered Pricing Table for WooCommerce / 2.2.3
Tiered Pricing Table for WooCommerce v2.2.3
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.2.3, at src/PriceManager.php

185 lines 5.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;
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 if ( $_amount <= $quantity ) {
74 $product_price = wc_get_price_to_display( wc_get_product( $product_id ), [
75 'price' => $price,
76 'qty' => 1,
77 ] );
78 }
79 }
80 }
81
82 if ( $type === 'percentage' ) {
83 $product = wc_get_product( $product_id );
84 foreach ( array_reverse( $rules, true ) as $_amount => $percentDiscount ) {
85 if ( $_amount <= $quantity ) {
86 $product_price = wc_get_price_to_display( $product, [
87 'price' => self::getPriceByPercentDiscount( $product->get_price(), $percentDiscount ),
88 'qty' => 1,
89 ] );
90 }
91 }
92 }
93
94 $product_price = ( isset( $product_price ) ? $product_price : false );
95 return apply_filters(
96 'tier_pricing_table/price/price_by_rules',
97 $product_price,
98 $quantity,
99 $product_id
100 );
101 }
102
103 /**
104 * Calculate price using percentage discount
105 *
106 * @param float|int $price
107 * @param float|int $discount
108 *
109 * @return bool|float|int
110 */
111 public static function getPriceByPercentDiscount( $price, $discount )
112 {
113
114 if ( $price > 0 && $discount <= 100 ) {
115 $discount_amount = $price / 100 * $discount;
116 return $price - $discount_amount;
117 }
118
119 return false;
120 }
121
122 /**
123 * Get pricing type of product. Available: fixed or percentage
124 *
125 * @param int $product_id
126 *
127 * @param string $default
128 *
129 * @return string
130 */
131 public static function getPricingType( $product_id, $default = 'fixed' )
132 {
133 return 'fixed';
134 }
135
136 /**
137 * Update price rules
138 *
139 * @param array $amounts
140 * @param array $prices
141 * @param int $post_id
142 */
143 public static function updateFixedPriceRules( $amounts, $prices, $post_id )
144 {
145 $rules = [];
146 foreach ( $amounts as $key => $amount ) {
147 if ( !empty($amount) && !empty($prices[$key]) && !key_exists( $amount, $rules ) ) {
148 $rules[$amount] = wc_format_decimal( $prices[$key] );
149 }
150 }
151 update_post_meta( $post_id, '_fixed_price_rules', $rules );
152 }
153
154 /**
155 * Update price rules
156 *
157 * @param array $amounts
158 * @param array $percents
159 * @param int $post_id
160 */
161 public static function updatePercentagePriceRules( $amounts, $percents, $post_id )
162 {
163 $rules = [];
164 foreach ( $amounts as $key => $amount ) {
165 if ( !empty($amount) && !empty($percents[$key]) && !key_exists( $amount, $rules ) && $percents[$key] < 99 ) {
166 $rules[$amount] = $percents[$key];
167 }
168 }
169 update_post_meta( $post_id, '_percentage_price_rules', $rules );
170 }
171
172 /**
173 * Update product pricing type
174 *
175 * @param int $post_id
176 * @param string $type
177 */
178 public static function updatePriceRulesType( $post_id, $type )
179 {
180 if ( in_array( $type, [ 'percentage', 'fixed' ] ) ) {
181 update_post_meta( $post_id, '_tiered_price_rules_type', $type );
182 }
183 }
184
185 }