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

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