| @@ -1,38 +1,168 @@ | ||
| 1 | -<?php namespace TierPricingTable; | |
| 1 | +<?php | |
| 2 | 2 | |
| 3 | -class PriceManager{ | |
| 3 | +namespace TierPricingTable; | |
| 4 | 4 | |
| 5 | +class PriceManager | |
| 6 | +{ | |
| 5 | 7 | /** |
| 6 | - * Return price rules or empty array if not exist rules | |
| 8 | + * Return fixed price rules or empty array if not exist rules | |
| 7 | 9 | * |
| 8 | 10 | * @param $product_id |
| 11 | + * | |
| 9 | 12 | * @return array |
| 10 | 13 | */ |
| 11 | - static function getPriceRules($product_id) | |
| 14 | + public static function getFixedPriceRules( $product_id ) | |
| 12 | 15 | { |
| 13 | - $rules = get_post_meta($product_id, '_price_rules', true); | |
| 14 | - $rules = empty($rules) ? [] : $rules; | |
| 15 | - | |
| 16 | - ksort($rules ); | |
| 17 | - | |
| 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 ); | |
| 18 | 51 | return $rules; |
| 19 | 52 | } |
| 20 | - | |
| 53 | + | |
| 21 | 54 | /** |
| 55 | + * Get price by product quantity | |
| 56 | + * | |
| 22 | 57 | * @param int $quantity |
| 23 | 58 | * @param int $product_id |
| 59 | + * | |
| 24 | 60 | * @return bool|float|int |
| 25 | 61 | */ |
| 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; | |
| 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 | + } | |
| 33 | 71 | } |
| 34 | 72 | } |
| 35 | - | |
| 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 | + | |
| 36 | 83 | return false; |
| 37 | 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 | + | |
| 38 | 168 | } |