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 / CartPriceManager.php

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

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