$price ) { if ( $_amount <= $quantity ) { return $price; } } } if ( $type === 'percentage' ) { $product = wc_get_product( $product_id ); foreach ( array_reverse( $rules, true ) as $_amount => $percentDiscount ) { if ( $_amount <= $quantity ) { return self::getPriceByPercentDiscount( $product->get_price(), $percentDiscount ); } } } return false; } /** * Calculate price using percentage discount * * @param float|int $price * @param float|int $discount * * @return bool|float|int */ public static function getPriceByPercentDiscount( $price, $discount ) { if ( $price > 0 && $discount <= 100 ) { $discount_amount = $price / 100 * $discount; return $price - $discount_amount; } return false; } /** * Get pricing type of product. Available: fixed or percentage * * @param int $product_id * * @param string $default * * @return string */ public static function getPricingType( $product_id, $default = 'fixed' ) { return 'fixed'; } /** * Update price rules * * @param array $amounts * @param array $prices * @param int $post_id */ public static function updateFixedPriceRules( $amounts, $prices, $post_id ) { $rules = []; foreach ( $amounts as $key => $amount ) { if ( !empty($amount) && !empty($prices[$key]) && !key_exists( $amount, $rules ) ) { $rules[$amount] = wc_format_decimal( $prices[$key] ); } } update_post_meta( $post_id, '_fixed_price_rules', $rules ); } /** * Update price rules * * @param array $amounts * @param array $percents * @param int $post_id */ public static function updatePercentagePriceRules( $amounts, $percents, $post_id ) { $rules = []; foreach ( $amounts as $key => $amount ) { if ( !empty($amount) && !empty($percents[$key]) && !key_exists( $amount, $rules ) && $percents[$key] < 99 ) { $rules[$amount] = $percents[$key]; } } update_post_meta( $post_id, '_percentage_price_rules', $rules ); } /** * Update product pricing type * * @param int $post_id * @param string $type */ public static function updatePriceRulesType( $post_id, $type ) { if ( in_array( $type, [ 'percentage', 'fixed' ] ) ) { update_post_meta( $post_id, '_tiered_price_rules_type', $type ); } } }