| @@ -1,63 +1,90 @@ | ||
| 1 | -<?php | |
| 1 | +<?php namespace TierPricingTable\Frontend; | |
| 2 | 2 | |
| 3 | -namespace TierPricingTable\Frontend; | |
| 3 | +use TierPricingTable\Settings\Settings; | |
| 4 | +use TierPricingTable\PriceManager; | |
| 5 | +use WC_Product; | |
| 4 | 6 | |
| 5 | -use TierPricingTable\Settings\Settings ; | |
| 6 | -use TierPricingTable\PriceManager ; | |
| 7 | -use WC_Product ; | |
| 8 | 7 | /** |
| 9 | 8 | * Class CartPriceManager |
| 10 | 9 | * |
| 11 | 10 | * @package TierPricingTable |
| 12 | 11 | */ |
| 13 | -class CartPriceManager | |
| 14 | -{ | |
| 15 | - /** | |
| 16 | - * @var Settings | |
| 17 | - */ | |
| 18 | - private $settings ; | |
| 19 | - /** | |
| 20 | - * CatalogPriceManager constructor. | |
| 21 | - * | |
| 22 | - * @param Settings $settings | |
| 23 | - */ | |
| 24 | - public function __construct( Settings $settings ) | |
| 25 | - { | |
| 26 | - $this->settings = $settings; | |
| 27 | - $this->hooks(); | |
| 28 | - } | |
| 29 | - | |
| 30 | - protected function hooks() | |
| 31 | - { | |
| 32 | - // Calculate product price in cart by pricing rules | |
| 33 | - add_action( | |
| 34 | - 'woocommerce_before_calculate_totals', | |
| 35 | - [ $this, 'calculateTotals' ], | |
| 36 | - 10, | |
| 37 | - 1 | |
| 38 | - ); | |
| 39 | - } | |
| 40 | - | |
| 41 | - /** | |
| 42 | - * Calculate price by quantity rules | |
| 43 | - * | |
| 44 | - * @param WC_Cart $cart | |
| 45 | - */ | |
| 46 | - public function calculateTotals( $cart ) | |
| 47 | - { | |
| 48 | - if ( !empty($cart->cart_contents) ) { | |
| 49 | - foreach ( $cart->cart_contents as $key => $cart_item ) { | |
| 50 | - | |
| 51 | - if ( $cart_item['data'] instanceof WC_Product ) { | |
| 52 | - $product_id = ( !empty($cart_item['variation_id']) ? $cart_item['variation_id'] : $cart_item['product_id'] ); | |
| 53 | - $new_price = PriceManager::getPriceByRules( $cart_item['quantity'], $product_id ); | |
| 54 | - if ( $new_price !== false ) { | |
| 55 | - $cart_item['data']->set_price( $new_price ); | |
| 56 | - } | |
| 57 | - } | |
| 58 | - | |
| 59 | - } | |
| 60 | - } | |
| 61 | - } | |
| 12 | +class CartPriceManager { | |
| 62 | 13 | |
| 14 | + /** | |
| 15 | + * @var Settings | |
| 16 | + */ | |
| 17 | + private $settings; | |
| 18 | + | |
| 19 | + /** | |
| 20 | + * CatalogPriceManager constructor. | |
| 21 | + * | |
| 22 | + * @param Settings $settings | |
| 23 | + */ | |
| 24 | + public function __construct( Settings $settings ) { | |
| 25 | + $this->settings = $settings; | |
| 26 | + $this->hooks(); | |
| 27 | + } | |
| 28 | + | |
| 29 | + protected function hooks() { | |
| 30 | + // Calculate product price in cart by pricing rules | |
| 31 | + add_action( 'woocommerce_before_calculate_totals', [ $this, 'calculateTotals' ], 10, 3 ); | |
| 32 | + add_action( 'woocommerce_before_mini_cart_contents', [ $this, 'miniCartSubTotal' ], 10, 3 ); | |
| 33 | + add_filter( 'woocommerce_cart_item_price', [ $this, 'calculateItemPrice' ], 10, 2 ); | |
| 34 | + } | |
| 35 | + | |
| 36 | + /** | |
| 37 | + * Calculate price by quantity rules | |
| 38 | + * | |
| 39 | + * @param WC_Cart $cart | |
| 40 | + */ | |
| 41 | + public function calculateTotals( $cart ) { | |
| 42 | + | |
| 43 | + if ( ! empty( $cart->cart_contents ) ) { | |
| 44 | + foreach ( $cart->cart_contents as $key => $cart_item ) { | |
| 45 | + if ( $cart_item['data'] instanceof WC_Product ) { | |
| 46 | + $product_id = ! empty( $cart_item['variation_id'] ) ? $cart_item['variation_id'] : $cart_item['product_id']; | |
| 47 | + | |
| 48 | + $new_price = PriceManager::getPriceByRules( $cart_item['quantity'], $product_id ); | |
| 49 | + | |
| 50 | + if ( $new_price !== false ) { | |
| 51 | + $cart_item['data']->set_price( $new_price ); | |
| 52 | + } | |
| 53 | + } | |
| 54 | + } | |
| 55 | + } | |
| 56 | + } | |
| 57 | + | |
| 58 | + public function miniCartSubTotal() { | |
| 59 | + $cart = wc()->cart; | |
| 60 | + $cart->calculate_totals(); | |
| 61 | + } | |
| 62 | + | |
| 63 | + /** | |
| 64 | + * Calculate price in mini cart | |
| 65 | + * | |
| 66 | + * @param string $price | |
| 67 | + * @param array $cart_item | |
| 68 | + * | |
| 69 | + * @return string | |
| 70 | + */ | |
| 71 | + public function calculateItemPrice( $price, $cart_item ) { | |
| 72 | + if ( $cart_item['data'] instanceof WC_Product ) { | |
| 73 | + | |
| 74 | + $new_price = PriceManager::getPriceByRules( $cart_item['quantity'], $cart_item['data']->get_id() ); | |
| 75 | + | |
| 76 | + // To get real product price | |
| 77 | + $product = wc_get_product( $cart_item['data']->get_id() ); | |
| 78 | + | |
| 79 | + if ( $new_price !== false ) { | |
| 80 | + if ( $this->settings->getAll()['show_discount_in_cart'] === 'yes' && tpt_fs()->can_use_premium_code() ) { | |
| 81 | + return '<del> ' . wc_price( $product->get_price() ) . ' </del> <ins> ' . wc_price( $new_price ) . ' </ins>'; | |
| 82 | + } | |
| 83 | + | |
| 84 | + return wc_price( $new_price ); | |
| 85 | + } | |
| 86 | + } | |
| 87 | + | |
| 88 | + return $price; | |
| 89 | + } | |
| 63 | 90 | } |