PluginProbe
Tiered Pricing Table for WooCommerce / 2.2.2
Tiered Pricing Table for WooCommerce v2.2.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 2.3.4 All 90 releases
tier-pricing-table / src / Frontend / CartPriceManager.php

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

90 lines 2.2 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 if ( $cart_item['data'] instanceof WC_Product ) {
46 $product_id = ! empty( $cart_item['variation_id'] ) ? $cart_item['variation_id'] : $cart_item['product_id'];
47
48 $new_price = PriceManager::getPriceByRules( $cart_item['quantity'], $product_id );
49
50 if ( $new_price !== false ) {
51 $cart_item['data']->set_price( $new_price );
52 }
53 }
54 }
55 }
56 }
57
58 public function miniCartSubTotal() {
59 $cart = wc()->cart;
60 $cart->calculate_totals();
61 }
62
63 /**
64 * Calculate price in mini cart
65 *
66 * @param string $price
67 * @param array $cart_item
68 *
69 * @return string
70 */
71 public function calculateItemPrice( $price, $cart_item ) {
72 if ( $cart_item['data'] instanceof WC_Product ) {
73
74 $new_price = PriceManager::getPriceByRules( $cart_item['quantity'], $cart_item['data']->get_id() );
75
76 // To get real product price
77 $product = wc_get_product( $cart_item['data']->get_id() );
78
79 if ( $new_price !== false ) {
80 if ( $this->settings->getAll()['show_discount_in_cart'] === 'yes' && tpt_fs()->can_use_premium_code() ) {
81 return '<del> ' . wc_price( $product->get_price() ) . ' </del> <ins> ' . wc_price( $new_price ) . ' </ins>';
82 }
83
84 return wc_price( $new_price );
85 }
86 }
87
88 return $price;
89 }
90 }