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

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

166 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace TierPricingTable;
2
3 use ArrayIterator;
4 use TierPricingTable\Core\ServiceContainerTrait;
5
6 /**
7 * Class CartUpsellManager
8 *
9 * @package TierPricingTable
10 */
11 class CartUpsellManager {
12
13 use ServiceContainerTrait;
14
15 /**
16 * CatalogPriceManager constructor.
17 */
18 public function __construct() {
19 add_action( 'woocommerce_after_cart_item_name', array( $this, 'showUpsell' ), 1, 3 );
20 }
21
22 public function showUpsell( $cartItem ) {
23 if ( ! $this->isCartUpsellEnabled() ) {
24 return;
25 }
26
27 $upsellString = $this->formatUpsellString( $cartItem );
28
29 if ( ! $upsellString ) {
30 return;
31 }
32
33 ?>
34 <div>
35 <small style="color: <?php echo esc_attr( $this->getUpsellColor() ); ?>"><?php echo wp_kses_post( $upsellString ); ?></small>
36 </div>
37 <?php
38 }
39
40 protected function formatUpsellString( $cartItem ) {
41
42 $nextPriceData = $this->getNextPriceData( $cartItem );
43
44 if ( empty( $nextPriceData ) ) {
45 return false;
46 }
47
48 $template = $this->getTemplate();
49
50 return strtr( $template, array(
51 '{tp_required_quantity}' => $nextPriceData['next_quantity'],
52 '{tp_next_price}' => wc_price( $nextPriceData['next_price'] ),
53 '{tp_next_discount}' => number_format( $nextPriceData['next_discount'], wc_get_price_decimals(), wc_get_price_decimal_separator(), wc_get_price_thousand_separator() ),
54 '{tp_actual_discount}' => number_format( $nextPriceData['actual_discount'], wc_get_price_decimals(), wc_get_price_decimal_separator(), wc_get_price_thousand_separator() ),
55 ) );
56 }
57
58 protected function getNextPriceData( $cartItem ) {
59 $pricingRules = $this->getPricingRules( $cartItem );
60
61 if ( ! empty( $pricingRules ) ) {
62
63 $iterator = new ArrayIterator( array_reverse( $pricingRules['rules'], true ) );
64 $currentQuantity = $cartItem['quantity'];
65
66 while ( $iterator->valid() ) {
67 if ( $currentQuantity < $iterator->key() ) {
68
69 $prevQuantity = $iterator->key();
70 $prevPrice = $iterator->current();
71
72 $iterator->next();
73
74 if ( $iterator->valid() && $currentQuantity >= $iterator->key() ) {
75
76 $product = wc_get_product( $this->getProductId( $cartItem ) );
77
78 if ( $pricingRules['type'] === 'fixed' ) {
79 $nextPrice = $prevPrice;
80 $currentPrice = $iterator->current();
81 } else {
82 $nextPrice = PriceManager::getPriceByPercentDiscount( $product->get_price(), $prevPrice );
83 $currentPrice = PriceManager::getPriceByPercentDiscount( $product->get_price(), $iterator->current() );
84 }
85
86 $currentPrice = PriceManager::getPriceWithTaxes( $currentPrice, $product, 'cart' );
87 $nextPrice = PriceManager::getPriceWithTaxes( $nextPrice, $product, 'cart' );
88
89 return array(
90 'next_price' => $nextPrice,
91 'next_discount' => PriceManager::calculateDiscount( $currentPrice, $nextPrice ),
92 'actual_discount' => $pricingRules['type'] === 'percentage' ? $prevPrice : PriceManager::calculateDiscount( $currentPrice, $product->get_price( 'edit' ) ),
93 'next_quantity' => $prevQuantity - $currentQuantity,
94 );
95 } else if ( ! $iterator->valid() ) {
96 $product = wc_get_product( $this->getProductId( $cartItem ) );
97
98 if ( $pricingRules['type'] === 'fixed' ) {
99 $nextPrice = $prevPrice;
100 $currentPrice = $product->get_price();
101 } else {
102 $nextPrice = PriceManager::getPriceByPercentDiscount( $product->get_price(), $prevPrice );
103 $currentPrice = $product->get_price();
104 }
105
106 $nextPrice = PriceManager::getPriceWithTaxes( $nextPrice, $product, 'cart' );
107
108 return array(
109 'next_price' => $nextPrice,
110 'next_discount' => PriceManager::calculateDiscount( $currentPrice, $nextPrice ),
111 'actual_discount' => $pricingRules['type'] === 'percentage' ? $prevPrice : PriceManager::calculateDiscount( $currentPrice, $product->get_price( 'edit' ) ),
112 'next_quantity' => $prevQuantity - $currentQuantity,
113 );
114 }
115 } else {
116 $iterator->next();
117 }
118 }
119 }
120
121 return array();
122 }
123
124 /**
125 * Get pricing rules from cart item
126 *
127 * @param array $cartItem
128 *
129 * @return array
130 */
131 protected function getPricingRules( $cartItem ) {
132
133 $productId = $this->getProductId( $cartItem );
134
135 if ( $productId ) {
136 $pricingRules = PriceManager::getPriceRules( $productId );
137 $pricingType = PriceManager::getPricingType( $productId );
138
139 if ( ! empty( $pricingRules ) ) {
140 return array(
141 'rules' => $pricingRules,
142 'type' => $pricingType
143 );
144 }
145 }
146
147 return array();
148 }
149
150 protected function getProductId( $cartItem ) {
151 return ! empty( $cartItem['variation_id'] ) ? $cartItem['variation_id'] : $cartItem['product_id'];
152 }
153
154 protected function getTemplate() {
155 return $this->getContainer()->getSettings()->get( 'cart_upsell_template', __( 'Buy <b>{tp_required_quantity}</b> more to get <b>{tp_next_price}</b> each', 'tier-pricing-table' ) );
156 }
157
158 protected function getUpsellColor() {
159 return $this->getContainer()->getSettings()->get( 'cart_upsell_color', '#96598A' );
160 }
161
162 protected function isCartUpsellEnabled() {
163 return $this->getContainer()->getSettings()->get( 'cart_upsell_enabled', 'no' ) === 'yes';
164 }
165 }
166