| 1 |
<?php namespace TierPricingTable\Frontend; |
| 2 |
|
| 3 |
use TierPricingTable\Settings\Settings; |
| 4 |
use TierPricingTable\PriceManager; |
| 5 |
use WC_Product; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class CartPriceManager |
| 9 |
* |
| 10 |
* @package TierPricingTable |
| 11 |
*/ |
| 12 |
class CartPriceManager { |
| 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 |
|
| 46 |
$needPriceRecalculation = apply_filters( 'tier_pricing_table/cart/need_price_recalculation', true, $cart_item ); |
| 47 |
|
| 48 |
if ( $cart_item['data'] instanceof WC_Product && $needPriceRecalculation ) { |
| 49 |
|
| 50 |
$product_id = ! empty( $cart_item['variation_id'] ) ? $cart_item['variation_id'] : $cart_item['product_id']; |
| 51 |
|
| 52 |
$new_price = PriceManager::getPriceByRules( $this->getTotalProductCount($cart_item), $product_id, 'cart' ); |
| 53 |
|
| 54 |
if ( $new_price !== false ) { |
| 55 |
$cart_item['data']->set_price( $new_price ); |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Get total product count depend on user's settings |
| 64 |
* |
| 65 |
* @param array $cart_item |
| 66 |
* |
| 67 |
* @return int |
| 68 |
*/ |
| 69 |
public function getTotalProductCount( $cart_item ) { |
| 70 |
|
| 71 |
if ( $this->settings->get( 'summarize_variations', 'no' ) !== 'yes' ) { |
| 72 |
return $cart_item['quantity']; |
| 73 |
} |
| 74 |
|
| 75 |
$count = 0; |
| 76 |
|
| 77 |
foreach ( wc()->cart->cart_contents as $cart_content ) { |
| 78 |
if ( $cart_content['product_id'] == $cart_item['product_id'] ) { |
| 79 |
$count += $cart_content['quantity']; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
return (int) apply_filters( 'tier_pricing_table/cart/total_product_count', $count ); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
public function miniCartSubTotal() { |
| 88 |
$cart = wc()->cart; |
| 89 |
$cart->calculate_totals(); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Calculate price in mini cart |
| 94 |
* |
| 95 |
* @param string $price |
| 96 |
* @param array $cart_item |
| 97 |
* |
| 98 |
* @return string |
| 99 |
*/ |
| 100 |
public function calculateItemPrice( $price, $cart_item ) { |
| 101 |
|
| 102 |
$needPriceRecalculation = apply_filters( 'tier_pricing_table/cart/need_price_recalculation/item', true, $cart_item ); |
| 103 |
|
| 104 |
if ( $cart_item['data'] instanceof WC_Product && $needPriceRecalculation ) { |
| 105 |
|
| 106 |
$new_price = PriceManager::getPriceByRules( $this->getTotalProductCount($cart_item), $cart_item['data']->get_id(), 'cart' ); |
| 107 |
|
| 108 |
// To get real product price |
| 109 |
$product = wc_get_product( $cart_item['data']->get_id() ); |
| 110 |
|
| 111 |
if ( $new_price !== false ) { |
| 112 |
if ( $this->settings->getAll()['show_discount_in_cart'] === 'yes' && tpt_fs()->can_use_premium_code() ) { |
| 113 |
return '<del> ' . wc_price( $product->get_price() ) . ' </del> <ins> ' . wc_price( $new_price ) . ' </ins>'; |
| 114 |
} |
| 115 |
|
| 116 |
return wc_price( $new_price ); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
return $price; |
| 121 |
} |
| 122 |
} |