PluginProbe
Tiered Pricing Table for WooCommerce / 6.1.0
Tiered Pricing Table for WooCommerce v6.1.0
8.0.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 All 91 releases
tier-pricing-table / src / Addons / RoleBasedPricing / RoleBasedPriceManager.php

RoleBasedPriceManager.php in Tiered Pricing Table for WooCommerce 6.1.0, at src/Addons/RoleBasedPricing/RoleBasedPriceManager.php

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