PluginProbe
Tiered Pricing Table for WooCommerce / 2.2.2
Tiered Pricing Table for WooCommerce v2.2.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
← All changes | src/PriceManager.php +173 -37 1.02.2.2 View file →
@@ -1,38 +1,174 @@
1 -<?php namespace TierPricingTable;
2 -
3 -class PriceManager{
4 -
5 - /**
6 - * Return price rules or empty array if not exist rules
7 - *
8 - * @param $product_id
9 - * @return array
10 - */
11 - static function getPriceRules($product_id)
12 - {
13 - $rules = get_post_meta($product_id, '_price_rules', true);
14 - $rules = empty($rules) ? [] : $rules;
15 -
16 - ksort($rules );
17 -
18 - return $rules;
19 - }
20 -
21 - /**
22 - * @param int $quantity
23 - * @param int $product_id
24 - * @return bool|float|int
25 - */
26 - public static function getPriceByRules($quantity, $product_id){
27 -
28 - $rules = self::getPriceRules($product_id);
29 -
30 - foreach(array_reverse($rules, true) as $_amount => $price){
31 - if($_amount <= $quantity) {
32 - return $price;
33 - }
34 - }
35 -
36 - return false;
37 - }
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 +
38 174 }