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

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