PluginProbe
Tiered Pricing Table for WooCommerce / 2.3.4
Tiered Pricing Table for WooCommerce v2.3.4
7.1.7 7.1.5 7.1.4 7.1.1 6.5.0 6.4.0 6.1.0 trunk 1.0 1.1 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 All 90 releases
tier-pricing-table / src / Frontend / CartPriceManager.php

CartPriceManager.php in Tiered Pricing Table for WooCommerce 2.3.4, at src/Frontend/CartPriceManager.php

122 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 }