PluginProbe
Tiered Pricing Table for WooCommerce / 6.1.0
Tiered Pricing Table for WooCommerce v6.1.0
8.0.2 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 All 91 releases
tier-pricing-table / src / Frontend / CartPriceManager.php

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

145 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace TierPricingTable\Frontend;
4
5 use TierPricingTable\Core\ServiceContainerTrait ;
6 use TierPricingTable\PriceManager ;
7 use WC_Cart ;
8 use WC_Product ;
9 /**
10 * Class CartPriceManager
11 *
12 * @package TierPricingTable
13 */
14 class CartPriceManager
15 {
16 use ServiceContainerTrait ;
17 /**
18 * CatalogPriceManager constructor.
19 */
20 public function __construct()
21 {
22 // Calculate product price in cart by pricing rules
23 add_action(
24 'woocommerce_before_calculate_totals',
25 array( $this, 'calculateTotals' ),
26 10,
27 3
28 );
29 add_action(
30 'woocommerce_before_mini_cart_contents',
31 array( $this, 'miniCartSubTotal' ),
32 9999,
33 3
34 );
35 add_filter(
36 'woocommerce_cart_item_price',
37 array( $this, 'calculateItemPrice' ),
38 999,
39 2
40 );
41 }
42
43 /**
44 * Calculate price by quantity rules
45 *
46 * @param WC_Cart $cart
47 */
48 public function calculateTotals( WC_Cart $cart )
49 {
50 if ( !empty($cart->cart_contents) ) {
51 foreach ( $cart->cart_contents as $key => $cartItem ) {
52 $needPriceRecalculation = apply_filters(
53 'tiered_pricing_table/cart/need_price_recalculation',
54 true,
55 $cartItem,
56 $cart
57 );
58
59 if ( $cartItem['data'] instanceof WC_Product && $needPriceRecalculation ) {
60 $product_id = ( !empty($cartItem['variation_id']) ? $cartItem['variation_id'] : $cartItem['product_id'] );
61 $new_price = PriceManager::getPriceByRules(
62 $this->getTotalProductCount( $cartItem ),
63 $product_id,
64 'calculation',
65 'cart'
66 );
67 $new_price = apply_filters(
68 'tiered_pricing_table/cart/product_cart_price',
69 $new_price,
70 $cartItem,
71 $key
72 );
73
74 if ( false !== $new_price ) {
75 $cartItem['data']->set_price( $new_price );
76 $cartItem['data']->add_meta_data( 'tiered_pricing_cart_price_calculated', 'yes' );
77 }
78
79 }
80
81 }
82 }
83 }
84
85 public function miniCartSubTotal()
86 {
87 $cart = wc()->cart;
88 $cart->calculate_totals();
89 }
90
91 /**
92 * Get total product count depend on user's settings
93 *
94 * @param array $cartItem
95 *
96 * @return int
97 */
98 public function getTotalProductCount( $cartItem )
99 {
100
101 if ( $this->getContainer()->getSettings()->get( 'summarize_variations', 'no' ) !== 'yes' ) {
102 $count = $cartItem['quantity'];
103 } else {
104 $count = 0;
105 foreach ( wc()->cart->cart_contents as $cart_content ) {
106 if ( $cart_content['product_id'] == $cartItem['product_id'] ) {
107 $count += $cart_content['quantity'];
108 }
109 }
110 }
111
112 return (int) apply_filters( 'tiered_pricing_table/cart/total_product_count', $count, $cartItem );
113 }
114
115 /**
116 * Calculate price in mini cart
117 *
118 * @param string $price
119 * @param array $cartItem
120 *
121 * @return string
122 */
123 public function calculateItemPrice( $price, $cartItem )
124 {
125 $needPriceRecalculation = apply_filters( 'tiered_pricing_table/cart/need_price_recalculation/item', true, $cartItem );
126
127 if ( $cartItem['data'] instanceof WC_Product && $needPriceRecalculation ) {
128 $new_price = PriceManager::getPriceByRules(
129 $this->getTotalProductCount( $cartItem ),
130 $cartItem['data']->get_id(),
131 'view',
132 'cart'
133 );
134 // To get real product price
135 $product = wc_get_product( $cartItem['data']->get_id() );
136 $new_price = apply_filters( 'tiered_pricing_table/cart/product_cart_price/item', $new_price, $cartItem );
137 if ( false !== $new_price ) {
138 return wc_price( $new_price );
139 }
140 }
141
142 return $price;
143 }
144
145 }