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

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