PluginProbe
Tiered Pricing Table for WooCommerce / 2.1
Tiered Pricing Table for WooCommerce v2.1
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.1, at src/Frontend/CartPriceManager.php

63 lines 1.5 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\Settings\Settings ;
6 use TierPricingTable\PriceManager ;
7 use WC_Product ;
8 /**
9 * Class CartPriceManager
10 *
11 * @package TierPricingTable
12 */
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 }
62
63 }