# tier-pricing-table/2.0/src/PriceManager.php

Tiered Pricing Table for WooCommerce, version 2.0. 168 lines.

- Page: https://pluginprobe.com/plugins/tier-pricing-table/2.0/code/src/PriceManager.php
- Raw: https://pluginprobe.com/plugins/tier-pricing-table/2.0/raw/src/PriceManager.php
- Modified: 2019-03-18T08:11:10+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/tier-pricing-table/2.0/code/src/PriceManager.php#L10-L20`.

```php
<?php

namespace TierPricingTable;

class PriceManager
{
    /**
     * Return fixed price rules or empty array if not exist rules
     *
     * @param $product_id
     *
     * @return array
     */
    public static function getFixedPriceRules( $product_id )
    {
        return self::getPriceRules( $product_id, 'fixed' );
    }
    
    /**
     * Return percentage price rules or empty array if not exist rules
     *
     * @param $product_id
     *
     * @return array
     */
    public static function getPercentagePriceRules( $product_id )
    {
        return self::getPriceRules( $product_id, 'percentage' );
    }
    
    /**
     * Get product pricing rules
     *
     * @param int $product_id
     * @param bool $type
     *
     * @return array
     */
    public static function getPriceRules( $product_id, $type = false )
    {
        $type = ( $type ? $type : self::getPricingType( $product_id ) );
        
        if ( $type === 'fixed' ) {
            $rules = get_post_meta( $product_id, '_fixed_price_rules', true );
        } else {
            $rules = get_post_meta( $product_id, '_percentage_price_rules', true );
        }
        
        $rules = ( !empty($rules) ? $rules : [] );
        ksort( $rules );
        return $rules;
    }
    
    /**
     * Get price by product quantity
     *
     * @param int $quantity
     * @param int $product_id
     *
     * @return bool|float|int
     */
    public static function getPriceByRules( $quantity, $product_id )
    {
        $rules = self::getPriceRules( $product_id );
        $type = self::getPricingType( $product_id );
        if ( $type === 'fixed' ) {
            foreach ( array_reverse( $rules, true ) as $_amount => $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 );
        }
    }

}
```
