| @@ -1,322 +1,174 @@ | ||
| 1 | -<?php namespace TierPricingTable; | |
| 1 | +<?php | |
| 2 | 2 | |
| 3 | -use WC_Product; | |
| 3 | +namespace TierPricingTable; | |
| 4 | 4 | |
| 5 | -class PriceManager { | |
| 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 | + } | |
| 6 | 173 | |
| 7 | - /** | |
| 8 | - * Return fixed price rules or empty array if not exist rules | |
| 9 | - * | |
| 10 | - * @param $product_id | |
| 11 | - * @param string $context | |
| 12 | - * | |
| 13 | - * @return array | |
| 14 | - */ | |
| 15 | - public static function getFixedPriceRules( $product_id, $context = 'view' ) { | |
| 16 | - return self::getPriceRules( $product_id, 'fixed', $context ); | |
| 17 | - } | |
| 18 | - | |
| 19 | - /** | |
| 20 | - * Return percentage price rules or empty array if not exist rules | |
| 21 | - * | |
| 22 | - * @param $product_id | |
| 23 | - * @param string $context | |
| 24 | - * | |
| 25 | - * @return array | |
| 26 | - */ | |
| 27 | - public static function getPercentagePriceRules( $product_id, $context = 'view' ) { | |
| 28 | - return self::getPriceRules( $product_id, 'percentage', $context ); | |
| 29 | - } | |
| 30 | - | |
| 31 | - /** | |
| 32 | - * Get product pricing rules | |
| 33 | - * | |
| 34 | - * @param int $product_id | |
| 35 | - * @param bool $type | |
| 36 | - * @param string $context | |
| 37 | - * | |
| 38 | - * @return array | |
| 39 | - */ | |
| 40 | - public static function getPriceRules( $product_id, $type = false, $context = 'view' ) { | |
| 41 | - | |
| 42 | - $type = $type ? $type : self::getPricingType( $product_id, 'fixed', $context ); | |
| 43 | - | |
| 44 | - if ( 'fixed' === $type ) { | |
| 45 | - $rules = get_post_meta( $product_id, '_fixed_price_rules', true ); | |
| 46 | - } else { | |
| 47 | - $rules = get_post_meta( $product_id, '_percentage_price_rules', true ); | |
| 48 | - } | |
| 49 | - | |
| 50 | - // If no rules for variation check for product level rules. | |
| 51 | - if ( 'view' === $context && self::variationHasNoOwnRules( $product_id, $rules ) ) { | |
| 52 | - | |
| 53 | - $product = wc_get_product( $product_id ); | |
| 54 | - | |
| 55 | - $product_id = $product->get_parent_id(); | |
| 56 | - | |
| 57 | - $type = self::getPricingType( $product_id ); | |
| 58 | - | |
| 59 | - if ( 'fixed' === $type ) { | |
| 60 | - $rules = get_post_meta( $product_id, '_fixed_price_rules', true ); | |
| 61 | - } else { | |
| 62 | - $rules = get_post_meta( $product_id, '_percentage_price_rules', true ); | |
| 63 | - } | |
| 64 | - } | |
| 65 | - | |
| 66 | - $rules = ! empty( $rules ) ? $rules : array(); | |
| 67 | - | |
| 68 | - ksort( $rules ); | |
| 69 | - | |
| 70 | - return apply_filters( 'tier_pricing_table/price/product_price_rules', $rules, $product_id, $type ); | |
| 71 | - } | |
| 72 | - | |
| 73 | - /** | |
| 74 | - * Get price by product quantity | |
| 75 | - * | |
| 76 | - * @param int $quantity | |
| 77 | - * @param int $product_id | |
| 78 | - * @param string $context | |
| 79 | - * @param string $place | |
| 80 | - * | |
| 81 | - * @return bool|float|int | |
| 82 | - */ | |
| 83 | - public static function getPriceByRules( $quantity, $product_id, $context = 'view', $place = 'shop' ) { | |
| 84 | - | |
| 85 | - $rules = self::getPriceRules( $product_id ); | |
| 86 | - | |
| 87 | - $type = self::getPricingType( $product_id ); | |
| 88 | - | |
| 89 | - if ( 'fixed' === $type ) { | |
| 90 | - foreach ( array_reverse( $rules, true ) as $_amount => $price ) { | |
| 91 | - if ( $_amount <= $quantity ) { | |
| 92 | - | |
| 93 | - $product_price = $price; | |
| 94 | - | |
| 95 | - if ( 'view' === $context ) { | |
| 96 | - $product = wc_get_product( $product_id ); | |
| 97 | - | |
| 98 | - $product_price = self::getPriceWithTaxes( $product_price, $product, $place ); | |
| 99 | - } | |
| 100 | - | |
| 101 | - break; | |
| 102 | - } | |
| 103 | - } | |
| 104 | - } | |
| 105 | - | |
| 106 | - if ( 'percentage' === $type ) { | |
| 107 | - $product = wc_get_product( $product_id ); | |
| 108 | - | |
| 109 | - foreach ( array_reverse( $rules, true ) as $_amount => $percentDiscount ) { | |
| 110 | - if ( $_amount <= $quantity ) { | |
| 111 | - | |
| 112 | - $product_price = self::getPriceByPercentDiscount( $product->get_price(), $percentDiscount ); | |
| 113 | - | |
| 114 | - if ( 'view' === $context ) { | |
| 115 | - | |
| 116 | - $product = wc_get_product( $product_id ); | |
| 117 | - | |
| 118 | - $product_price = self::getPriceWithTaxes( $product_price, $product, $place ); | |
| 119 | - | |
| 120 | - } | |
| 121 | - | |
| 122 | - break; | |
| 123 | - } | |
| 124 | - } | |
| 125 | - } | |
| 126 | - | |
| 127 | - $product_price = isset( $product_price ) ? $product_price : false; | |
| 128 | - | |
| 129 | - return apply_filters( 'tier_pricing_table/price/price_by_rules', $product_price, $quantity, $product_id ); | |
| 130 | - } | |
| 131 | - | |
| 132 | - /** | |
| 133 | - * Calculate displayed price depend on taxes | |
| 134 | - * | |
| 135 | - * @param float $price | |
| 136 | - * @param WC_Product $product | |
| 137 | - * @param string $place | |
| 138 | - * | |
| 139 | - * @return float | |
| 140 | - */ | |
| 141 | - public static function getPriceWithTaxes( $price, $product, $place = 'shop' ) { | |
| 142 | - | |
| 143 | - if ( wc_tax_enabled() ) { | |
| 144 | - | |
| 145 | - if ( 'cart' === $place ) { | |
| 146 | - $price = 'incl' === get_option( 'woocommerce_tax_display_cart' ) ? | |
| 147 | - | |
| 148 | - wc_get_price_including_tax( $product, array( | |
| 149 | - 'qty' => 1, | |
| 150 | - 'price' => $price | |
| 151 | - ) | |
| 152 | - ) : | |
| 153 | - | |
| 154 | - wc_get_price_excluding_tax( $product, array( | |
| 155 | - 'qty' => 1, | |
| 156 | - 'price' => $price | |
| 157 | - ) | |
| 158 | - ); | |
| 159 | - } else { | |
| 160 | - $price = wc_get_price_to_display( $product, array( | |
| 161 | - 'price' => $price, | |
| 162 | - 'qty' => 1, | |
| 163 | - ) ); | |
| 164 | - } | |
| 165 | - } | |
| 166 | - | |
| 167 | - return $price; | |
| 168 | - } | |
| 169 | - | |
| 170 | - | |
| 171 | - /** | |
| 172 | - * Calculate price using percentage discount | |
| 173 | - * | |
| 174 | - * @param float|int $price | |
| 175 | - * @param float|int $discount | |
| 176 | - * | |
| 177 | - * @return bool|float|int | |
| 178 | - */ | |
| 179 | - public static function getPriceByPercentDiscount( $price, $discount ) { | |
| 180 | - if ( $price > 0 && $discount <= 100 ) { | |
| 181 | - $discount_amount = ( $price / 100 ) * $discount; | |
| 182 | - | |
| 183 | - return $price - $discount_amount; | |
| 184 | - } | |
| 185 | - | |
| 186 | - return false; | |
| 187 | - } | |
| 188 | - | |
| 189 | - /** | |
| 190 | - * Get pricing type of product. Available: fixed or percentage | |
| 191 | - * | |
| 192 | - * @param int $product_id | |
| 193 | - * @param string $default | |
| 194 | - * @param string $context | |
| 195 | - * | |
| 196 | - * @return string | |
| 197 | - */ | |
| 198 | - public static function getPricingType( $product_id, $default = 'fixed', $context = 'view' ) { | |
| 199 | - | |
| 200 | - $type = get_post_meta( $product_id, '_tiered_price_rules_type', true ); | |
| 201 | - | |
| 202 | - if ( 'view' === $context && self::variationHasNoOwnRules( $product_id ) ) { | |
| 203 | - $product = wc_get_product( $product_id ); | |
| 204 | - | |
| 205 | - $type = get_post_meta( $product->get_parent_id(), '_tiered_price_rules_type', true ); | |
| 206 | - } | |
| 207 | - | |
| 208 | - $type = in_array( $type, array( 'fixed', 'percentage' ) ) ? $type : $default; | |
| 209 | - | |
| 210 | - return apply_filters( 'tier_pricing_table/price/type', $type, $product_id ); | |
| 211 | - } | |
| 212 | - | |
| 213 | - /** | |
| 214 | - * Update price rules | |
| 215 | - * | |
| 216 | - * @param array $amounts | |
| 217 | - * @param array $prices | |
| 218 | - * @param int $product_id | |
| 219 | - */ | |
| 220 | - public static function updateFixedPriceRules( $amounts, $prices, $product_id ) { | |
| 221 | - $rules = array(); | |
| 222 | - | |
| 223 | - foreach ( $amounts as $key => $amount ) { | |
| 224 | - if ( ! empty( $amount ) && ! empty( $prices[ $key ] ) && ! key_exists( $amount, $rules ) ) { | |
| 225 | - $rules[ $amount ] = wc_format_decimal( $prices[ $key ] ); | |
| 226 | - } | |
| 227 | - } | |
| 228 | - | |
| 229 | - update_post_meta( $product_id, '_fixed_price_rules', $rules ); | |
| 230 | - } | |
| 231 | - | |
| 232 | - /** | |
| 233 | - * Update price rules | |
| 234 | - * | |
| 235 | - * @param array $amounts | |
| 236 | - * @param array $percents | |
| 237 | - * @param int $product_id | |
| 238 | - */ | |
| 239 | - public static function updatePercentagePriceRules( $amounts, $percents, $product_id ) { | |
| 240 | - $rules = array(); | |
| 241 | - | |
| 242 | - foreach ( $amounts as $key => $amount ) { | |
| 243 | - if ( ! empty( $amount ) && ! empty( $percents[ $key ] ) && ! key_exists( $amount, | |
| 244 | - $rules ) && $percents[ $key ] < 99 ) { | |
| 245 | - $rules[ $amount ] = $percents[ $key ]; | |
| 246 | - } | |
| 247 | - } | |
| 248 | - | |
| 249 | - update_post_meta( $product_id, '_percentage_price_rules', $rules ); | |
| 250 | - } | |
| 251 | - | |
| 252 | - /** | |
| 253 | - * Update product pricing type | |
| 254 | - * | |
| 255 | - * @param int $product_id | |
| 256 | - * @param string $type | |
| 257 | - */ | |
| 258 | - public static function updatePriceRulesType( $product_id, $type ) { | |
| 259 | - if ( in_array( $type, array( 'percentage', 'fixed' ) ) ) { | |
| 260 | - update_post_meta( $product_id, '_tiered_price_rules_type', $type ); | |
| 261 | - } | |
| 262 | - } | |
| 263 | - | |
| 264 | - /** | |
| 265 | - * Get minimum product qty for table | |
| 266 | - * | |
| 267 | - * @param int $product_id | |
| 268 | - * @param string $context | |
| 269 | - * | |
| 270 | - * @return int | |
| 271 | - */ | |
| 272 | - public static function getProductQtyMin( $product_id, $context = 'view' ) { | |
| 273 | - | |
| 274 | - if ( 'view' === $context && self::variationHasNoOwnRules( $product_id ) ) { | |
| 275 | - $product = wc_get_product( $product_id ); | |
| 276 | - $product_id = $product->get_parent_id(); | |
| 277 | - } | |
| 278 | - | |
| 279 | - $min = get_post_meta( $product_id, '_tiered_price_minimum_qty', true ); | |
| 280 | - | |
| 281 | - $min = $min ? intval( $min ) : 1; | |
| 282 | - | |
| 283 | - return apply_filters( 'tier_pricing_table/price/minimum', $min, $product_id ); | |
| 284 | - } | |
| 285 | - | |
| 286 | - /** | |
| 287 | - * Update product min | |
| 288 | - * | |
| 289 | - * @param int $product_id | |
| 290 | - * @param int $min | |
| 291 | - */ | |
| 292 | - public static function updateProductQtyMin( $product_id, $min ) { | |
| 293 | - | |
| 294 | - $min = intval( $min ); | |
| 295 | - | |
| 296 | - if ( $min > 0 ) { | |
| 297 | - update_post_meta( $product_id, '_tiered_price_minimum_qty', $min ); | |
| 298 | - } | |
| 299 | - } | |
| 300 | - | |
| 301 | - /** | |
| 302 | - * Check if variation has no own rules | |
| 303 | - * | |
| 304 | - * @param int $product_id | |
| 305 | - * @param bool $rules | |
| 306 | - * | |
| 307 | - * @return bool | |
| 308 | - */ | |
| 309 | - protected static function variationHasNoOwnRules( $product_id, $rules = false ) { | |
| 310 | - | |
| 311 | - $rules = $rules ? $rules : self::getPriceRules( $product_id, false, 'edit' ); | |
| 312 | - | |
| 313 | - if ( empty( $rules ) ) { | |
| 314 | - | |
| 315 | - $product = wc_get_product( $product_id ); | |
| 316 | - | |
| 317 | - return $product->is_type( 'variation' ); | |
| 318 | - } | |
| 319 | - | |
| 320 | - return false; | |
| 321 | - } | |
| 322 | -} | |
| 174 | +} | |