# tier-pricing-table/2.3.0/src/Frontend/CartPriceManager.php

Tiered Pricing Table for WooCommerce, version 2.3.0. 97 lines.

- Page: https://pluginprobe.com/plugins/tier-pricing-table/2.3.0/code/src/Frontend/CartPriceManager.php
- Raw: https://pluginprobe.com/plugins/tier-pricing-table/2.3.0/raw/src/Frontend/CartPriceManager.php
- Modified: 2019-07-19T13:02:34+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.3.0/code/src/Frontend/CartPriceManager.php#L10-L20`.

```php
<?php namespace TierPricingTable\Frontend;

use TierPricingTable\Settings\Settings;
use TierPricingTable\PriceManager;
use WC_Product;

/**
 * Class CartPriceManager
 *
 * @package TierPricingTable
 */
class CartPriceManager {

	/**
	 * @var Settings
	 */
	private $settings;

	/**
	 * CatalogPriceManager constructor.
	 *
	 * @param Settings $settings
	 */
	public function __construct( Settings $settings ) {
		$this->settings = $settings;
		$this->hooks();
	}

	protected function hooks() {
		// Calculate product price in cart by pricing rules
		add_action( 'woocommerce_before_calculate_totals', [ $this, 'calculateTotals' ], 10, 3 );
		add_action( 'woocommerce_before_mini_cart_contents', [ $this, 'miniCartSubTotal' ], 10, 3 );
		add_filter( 'woocommerce_cart_item_price', [ $this, 'calculateItemPrice' ], 10, 2 );
	}

	/**
	 * Calculate price by quantity rules
	 *
	 * @param WC_Cart $cart
	 */
	public function calculateTotals( $cart ) {

		if ( ! empty( $cart->cart_contents ) ) {
			foreach ( $cart->cart_contents as $key => $cart_item ) {

				$needPriceRecalculation = apply_filters( 'tier_pricing_table/cart/need_price_recalculation', true, $cart_item );

				if ( $cart_item['data'] instanceof WC_Product && $needPriceRecalculation ) {

					$product_id = ! empty( $cart_item['variation_id'] ) ? $cart_item['variation_id'] : $cart_item['product_id'];

					$new_price = PriceManager::getPriceByRules( $cart_item['quantity'], $product_id );

					if ( $new_price !== false ) {
						$cart_item['data']->set_price( $new_price );
					}
				}
			}
		}
	}

	public function miniCartSubTotal() {
		$cart = wc()->cart;
		$cart->calculate_totals();
	}

	/**
	 * Calculate price in mini cart
	 *
	 * @param string $price
	 * @param array $cart_item
	 *
	 * @return string
	 */
	public function calculateItemPrice( $price, $cart_item ) {

		$needPriceRecalculation = apply_filters( 'tier_pricing_table/cart/need_price_recalculation/item', true, $cart_item );

		if ( $cart_item['data'] instanceof WC_Product && $needPriceRecalculation ) {

			$new_price = PriceManager::getPriceByRules( $cart_item['quantity'], $cart_item['data']->get_id() );

			// To get real product price
			$product = wc_get_product( $cart_item['data']->get_id() );

			if ( $new_price !== false ) {
				if ( $this->settings->getAll()['show_discount_in_cart'] === 'yes' && tpt_fs()->can_use_premium_code() ) {
					return '<del> ' . wc_price( $product->get_price() ) . ' </del> <ins> ' . wc_price( $new_price ) . ' </ins>';
				}

				return wc_price( $new_price );
			}
		}

		return $price;
	}
}
```
