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

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

- Page: https://pluginprobe.com/plugins/tier-pricing-table/2.0/code/src/Frontend/CartPriceManager.php
- Raw: https://pluginprobe.com/plugins/tier-pricing-table/2.0/raw/src/Frontend/CartPriceManager.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/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,
            1
        );
    }
    
    /**
     * 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 ) {
                
                if ( $cart_item['data'] instanceof WC_Product ) {
                    $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 );
                    }
                }
            
            }
        }
    }

}
```
