PluginProbe
Tiered Pricing Table for WooCommerce / trunk
Tiered Pricing Table for WooCommerce vtrunk
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 / Addons / UserBasedPricing / UserBasedPriceManager.php

UserBasedPriceManager.php in Tiered Pricing Table for WooCommerce trunk, at src/Addons/UserBasedPricing/UserBasedPriceManager.php

206 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace TierPricingTable\Addons\UserBasedPricing;
2
3 use TierPricingTable\Forms\Form;
4
5 class UserBasedPriceManager {
6
7 public static function userHasRules( int $userId, int $productId, string $context = 'view' ): bool {
8
9 $metadataToCheck = apply_filters( 'tiered_pricing_table/user_based_rules/rule_exists_meta', array(
10 '_tiered_price_rules_type',
11 '_tiered_price_pricing_type',
12 ), $userId );
13
14 $productUserRulesExists = false;
15
16 foreach ( $metadataToCheck as $metaKey ) {
17 if ( metadata_exists( 'post', $productId, "_user_{$userId}{$metaKey}" ) ) {
18 $productUserRulesExists = true;
19
20 break;
21 }
22 }
23
24 return $productUserRulesExists;
25 }
26
27 public static function deleteAllDataForUser( $productId, $userId ) {
28
29 delete_post_meta( $productId, "_user_{$userId}_tiered_price_regular_price" );
30 delete_post_meta( $productId, "_user_{$userId}_tiered_price_sale_price" );
31 delete_post_meta( $productId, "_user_{$userId}_tiered_price_discount" );
32 delete_post_meta( $productId, "_user_{$userId}_tiered_price_discount_type" );
33 delete_post_meta( $productId, "_user_{$userId}_tiered_price_pricing_type" );
34
35 delete_post_meta( $productId, "_user_{$userId}_percentage_price_rules" );
36 delete_post_meta( $productId, "_user_{$userId}_fixed_price_rules" );
37 delete_post_meta( $productId, "_user_{$userId}_tiered_price_rules_type" );
38
39 delete_post_meta( $productId, "_user_{$userId}_tiered_price_minimum_qty" );
40
41 delete_post_meta( $productId, "_user_{$userId}_tiered_price_tax_status" );
42 delete_post_meta( $productId, "_user_{$userId}_tiered_price_tax_class" );
43
44 do_action( 'tiered_pricing_table/user_based_rules/delete_user_rule', $productId, $userId );
45 }
46
47 /**
48 * Return empty array if rules do not exist.
49 */
50 public static function getFixedPriceRules( int $productId, int $userId, string $context = 'view' ): array {
51 return self::getPriceRules( $productId, $userId, 'fixed', $context );
52 }
53
54 /**
55 * Return an empty array if rules do not exist.
56 */
57 public static function getPercentagePriceRules( int $productId, int $userId, string $context = 'view' ): array {
58 return self::getPriceRules( $productId, $userId, 'percentage', $context );
59 }
60
61 public static function getPriceRules(
62 int $productId,
63 int $userId,
64 ?string $type = null,
65 string $context = 'view'
66 ): array {
67
68 $type = $type ? $type : self::getPricingType( $productId, $userId, 'fixed', $context );
69
70 if ( 'fixed' === $type ) {
71 $rules = (array) get_post_meta( $productId, "_user_{$userId}_fixed_price_rules", true );
72 } else {
73 $rules = (array) get_post_meta( $productId, "_user_{$userId}_percentage_price_rules", true );
74 }
75
76 $rules = ! empty( $rules ) ? array_filter( $rules ) : array();
77 ksort( $rules );
78
79 if ( 'edit' !== $context ) {
80
81 $rules = apply_filters( 'tiered_pricing_table/user_based_rules/price/product_price_rules', $rules,
82 $productId, $type );
83 }
84
85 return $rules;
86 }
87
88 public static function getPricingType(
89 int $productId,
90 int $userId,
91 string $default = 'fixed',
92 string $context = 'view'
93 ): string {
94
95 $type = get_post_meta( $productId, "_user_{$userId}_tiered_price_rules_type", true );
96
97 $type = in_array( $type, array( 'fixed', 'percentage' ) ) ? $type : $default;
98
99 if ( 'edit' !== $context ) {
100 return apply_filters( 'tiered_pricing_table/user_based_rules/price/type', $type, $userId, $productId );
101 }
102
103 return $type;
104 }
105
106 public static function getProductQtyMin( int $productId, int $userId, string $context = 'view' ): ?int {
107
108 $minimum = get_post_meta( $productId, "_user_{$userId}_tiered_price_minimum_qty", true );
109 $minimum = ! Form::isEmpty( $minimum ) ? intval( $minimum ) : null;
110
111 if ( 'view' === $context ) {
112 return apply_filters( 'tiered_pricing_table/user_based_rules/price/minimum', $minimum, $userId, $productId );
113 }
114
115 return $minimum;
116 }
117
118 public static function getProductRegularUserPrice(
119 int $productId,
120 int $userId,
121 string $context = 'view'
122 ): ?float {
123
124 $price = get_post_meta( $productId, "_user_{$userId}_tiered_price_regular_price", true );
125
126 $price = ! Form::isEmpty( $price ) ? floatval( $price ) : null;
127
128 if ( 'edit' !== $context ) {
129 return apply_filters( 'tiered_pricing_table/user_based_rules/price/regular_price', $price, $userId,
130 $productId );
131 }
132
133 return $price;
134 }
135
136 public static function getProductSaleUserPrice( int $productId, int $userId, string $context = 'view' ): ?float {
137
138 $price = get_post_meta( $productId, "_user_{$userId}_tiered_price_sale_price", true );
139
140 $price = ! Form::isEmpty( $price ) ? floatval( $price ) : null;
141
142 if ( 'edit' !== $context ) {
143 return apply_filters( 'tiered_pricing_table/user_based_rules/price/sale_price', $price, $userId, $productId );
144 }
145
146 return $price;
147 }
148
149 public static function getProductDiscount( int $productId, int $userId, string $context = 'view' ): ?float {
150 $discount = get_post_meta( $productId, "_user_{$userId}_tiered_price_discount", true );
151
152 $discount = ! Form::isEmpty( $discount ) ? floatval( $discount ) : null;
153
154 if ( 'edit' !== $context ) {
155 return apply_filters( 'tiered_pricing_table/user_based_rules/price/discount', $discount, $userId,
156 $productId );
157 }
158
159 return $discount;
160 }
161
162 public static function getProductDiscountType( int $productId, int $userId, string $context = 'view' ): string {
163 $discountType = get_post_meta( $productId, "_user_{$userId}_tiered_price_discount_type", true );
164
165 if ( 'edit' !== $context ) {
166 return apply_filters( 'tiered_pricing_table/user_based_rules/price/discount_type', $discountType, $userId,
167 $productId );
168 }
169
170 return in_array( $discountType, array( 'sale_price', 'regular_price' ) ) ? $discountType : 'sale_price';
171 }
172
173 public static function getProductPricingType( int $productId, int $userId, string $context = 'view' ): string {
174 $pricingType = get_post_meta( $productId, "_user_{$userId}_tiered_price_pricing_type", true );
175
176 $pricingType = in_array( $pricingType, array( 'flat', 'percentage' ) ) ? $pricingType : 'flat';
177
178 if ( 'edit' !== $context ) {
179 return apply_filters( 'tiered_pricing_table/user_based_rules/price/pricing_type', $pricingType, $userId,
180 $productId );
181 }
182
183 return $pricingType;
184 }
185
186 public static function getProductTaxStatus( int $productId, int $userId, string $context = 'view' ): string {
187 $taxStatus = get_post_meta( $productId, "_user_{$userId}_tiered_price_tax_status", true );
188
189 if ( 'edit' !== $context ) {
190 return apply_filters( 'tiered_pricing_table/user_based_rules/price/tax_status', $taxStatus, $userId, $productId );
191 }
192
193 return $taxStatus;
194 }
195
196 public static function getProductTaxClass( int $productId, int $userId, string $context = 'view' ): string {
197 $taxClass = get_post_meta( $productId, "_user_{$userId}_tiered_price_tax_class", true );
198
199 if ( 'edit' !== $context ) {
200 return apply_filters( 'tiered_pricing_table/user_based_rules/price/tax_class', $taxClass, $userId, $productId );
201 }
202
203 return $taxClass;
204 }
205 }
206