PluginProbe
Tiered Pricing Table for WooCommerce / 8.0.2
Tiered Pricing Table for WooCommerce v8.0.2
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
← All changes | src/Addons/ManualOrders/ManualOrdersAddon.php +71 -53 6.1.0 → 8.0.2 View file →
@@ -7,64 +7,77 @@
7 7 use WC_Order_Item;
8 8 use WC_Order_Item_Product;
9 9
10 10 class ManualOrdersAddon extends AbstractAddon {
11 -
11 +
12 12 protected $items = array();
13 -
13 +
14 14 public function getName(): string {
15 15 return __( 'Tiered pricing for manual orders', 'tier-pricing-table' );
16 16 }
17 -
17 +
18 18 public function getDescription(): string {
19 - return __( 'Enable tiered pricing for admin-created orders from the administrator panel.', 'tier-pricing-table' );
19 + return __( 'Apply tiered pricing to orders created manually in the admin area.', 'tier-pricing-table' );
20 20 }
21 -
21 +
22 + public function getIcon(): string {
23 + return '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M14 2H6c-1.1 0-1.99.9-1.99 2L4 20c0 1.1.89 2 1.99 2H18c1.1 0 2-.9 2-2V8l-6-6zm2 14H8v-2h8v2zm0-4H8v-2h8v2zm-3-5V3.5L18.5 9H13z"/></svg>';
24 + }
25 +
22 26 public function getSlug(): string {
23 27 return 'manual-orders';
24 28 }
25 -
29 +
26 30 public function getItemPrice( WC_Order_Item_Product $item ) {
27 - $productPrice = $item->get_product()->get_price();
28 - $tieredPricingRule = PriceManager::getPricingRule( $item->get_product()->get_id() );
29 -
31 + $product = $item->get_product();
32 +
33 + // Product (or its variation) may have been deleted after the order was created.
34 + if ( ! $product ) {
35 + $quantity = $item->get_quantity();
36 +
37 + return $quantity ? ( (float) $item->get_total() / $quantity ) : (float) $item->get_total();
38 + }
39 +
40 + $productPrice = $product->get_price();
41 + $tieredPricingRule = PriceManager::getPricingRule( $product->get_id() );
42 +
30 43 // Item has no tiered pricing rule. Return regular price (which also can be adjusted by pricing rule, but on the get_price hook)
31 44 if ( ! $tieredPricingRule->getRules() ) {
32 45 return $productPrice;
33 46 }
34 -
47 +
35 48 $tieredPrice = $tieredPricingRule->getTierPrice( $this->getTotalItemQuantity( $item ), false );
36 -
37 - return $tieredPrice ? $tieredPrice : $productPrice;
49 +
50 + return false !== $tieredPrice ? $tieredPrice : $productPrice;
38 51 }
39 -
52 +
40 53 public function getTotalItemQuantity( WC_Order_Item_Product $baseItem ): int {
41 -
54 +
42 55 $quantity = $baseItem->get_quantity();
43 -
56 +
44 57 $orderItems = $baseItem->get_order()->get_items();
45 -
58 +
46 59 if ( empty( $orderItems ) || count( $orderItems ) < 2 ) {
47 60 return $quantity;
48 61 }
49 -
62 +
50 63 $calculatedItems = [ $baseItem->get_id() ];
51 -
64 +
52 65 // Consider different variations
53 66 if ( CalculationLogic::considerProductVariationAsOneProduct() ) {
54 67 foreach ( $orderItems as $item ) {
55 -
68 +
56 69 /**
57 70 * Item type
58 71 *
59 72 * @var WC_Order_Item_Product $item
60 73 */
61 -
74 +
62 75 // Item quantity is already calculated
63 76 if ( in_array( $item->get_id(), $calculatedItems ) ) {
64 77 continue;
65 78 }
66 -
79 +
67 80 if ( $item->get_product_id() === $baseItem->get_product_id() ) {
68 81 $quantity += $item->get_quantity();
69 82 $calculatedItems[] = $item->get_id();
70 83 }
@@ -69,92 +82,97 @@
69 82 $calculatedItems[] = $item->get_id();
70 83 }
71 84 }
72 85 }
73 -
86 +
74 87 $baseItemPricingRule = PriceManager::getPricingRule( $baseItem->get_product_id() );
75 -
88 +
76 89 if ( 'global-rules' !== $baseItemPricingRule->provider ) {
77 90 return $quantity;
78 91 }
79 -
92 +
80 93 if ( empty( $baseItemPricingRule->providerData['applying_type'] ) || 'cross' !== $baseItemPricingRule->providerData['applying_type'] ) {
81 94 return $baseItem->get_quantity();
82 95 }
83 -
96 +
84 97 foreach ( $orderItems as $item ) {
85 -
98 +
86 99 // Item quantity is already calculated
87 100 if ( in_array( $item->get_id(), $calculatedItems ) ) {
88 101 continue;
89 102 }
90 -
103 +
91 104 $pricingRule = PriceManager::getPricingRule( $item->get_product_id() );
92 -
105 +
93 106 if ( 'global-rules' !== $pricingRule->provider ) {
94 107 continue;
95 108 }
96 -
109 +
97 110 if ( empty( $pricingRule->providerData['rule_id'] ) || $pricingRule->providerData['rule_id'] !== $baseItemPricingRule->providerData['rule_id'] ) {
98 111 continue;
99 112 }
100 -
113 +
101 114 $quantity += $item->get_quantity();
102 115 }
103 -
116 +
104 117 return $quantity;
105 118 }
106 -
119 +
107 120 public function run() {
108 -
121 +
109 122 // Store items before calculating
110 123 add_action( 'woocommerce_before_save_order_items', function ( $orderId, $items ) {
111 124 $this->items[ $orderId ] = $items;
112 125 }, 10, 2 );
113 -
126 +
114 127 add_action( 'woocommerce_before_save_order_item', function ( WC_Order_Item $item ) {
115 -
128 +
116 129 if ( ! ( $item instanceof WC_Order_Item_Product ) ) {
117 130 return;
118 131 }
119 -
132 +
133 + if ( ! apply_filters( 'tiered_pricing_table/manual_created_orders/modify_item_price', true, $item ) ) {
134 + return;
135 + }
136 +
120 137 // Need to be calculated with tiered pricing
121 138 if ( ! empty( $this->items[ $item->get_order_id() ]['calculate_tiered_pricing'] ) ) {
122 -
139 +
123 140 $GLOBALS['tpt_current_user_id'] = $item->get_order()->get_customer_id();
124 -
141 +
125 142 $itemPrice = $this->getItemPrice( $item );
126 -
143 +
127 144 $product = $item->get_product() ? $item->get_product() : false;
128 -
145 +
129 146 if ( ! $product ) {
130 147 return;
131 148 }
132 -
149 +
133 150 $itemTotal = wc_get_price_excluding_tax( $product, array(
134 - 'price' => $itemPrice,
135 - 'qty' => $item->get_quantity(),
151 + 'price' => $itemPrice,
152 + 'qty' => $item->get_quantity(),
136 153 ) );
137 -
154 +
138 155 // Nothing to change
139 156 if ( ( float ) $item->get_total() === $itemTotal ) {
140 157 return;
141 158 }
142 -
159 +
143 160 // translators: %1$s: item name, %2$s: original price, %3$s: new price
144 161 $note = sprintf( __( 'Tiered pricing recalculations for %1$s: %2$s → %3$s', 'tier-pricing-table' ),
145 - $item->get_name(), wc_price( $item->get_total() / $item->get_quantity() ), wc_price( $itemPrice ) );
146 -
162 + $item->get_name(), wc_price( $item->get_total() / $item->get_quantity() ),
163 + wc_price( $itemPrice ) );
164 +
147 165 $item->set_subtotal( $itemTotal );
148 166 $item->set_total( $itemTotal );
149 -
167 +
150 168 $item->calculate_taxes();
151 -
169 +
152 170 $item->get_order()->add_order_note( $note );
153 171 }
154 -
172 +
155 173 }, 10, 2 );
156 -
174 +
157 175 add_action( 'woocommerce_admin_order_items_after_line_items', function () {
158 176 ?>
159 177 <tr style="display: none;">
160 178 <td>
@@ -162,9 +180,9 @@
162 180 </td>
163 181 </tr>
164 182 <?php
165 183 } );
166 -
184 +
167 185 add_action( 'admin_head', function () {
168 186 ?>
169 187 <script>
170 188 jQuery(document).ready(function ($) {
@@ -180,9 +198,9 @@
180 198 });
181 199 </script>
182 200 <?php
183 201 } );
184 -
202 +
185 203 add_action( 'woocommerce_order_item_add_action_buttons', function ( WC_Order $order ) {
186 204 if ( $order->is_editable() ) {
187 205 ?>
188 206 <button type="button" class="button button-primary calculate-tiered-pricing">