PluginProbe
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More / 2.2.0
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More v2.2.0
2.3.0 2.2.0 2.1.1 2.1.0 2.0.0 1.10.0 1.9.1 1.9.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 All 59 releases
storeengine / includes / schedules / order.php

order.php in StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More 2.2.0, at includes/schedules/order.php

220 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace StoreEngine\Schedules;
4
5 use StoreEngine;
6 use StoreEngine\Classes\Order\OrderItemProduct;
7 use StoreEngine\Utils\Formatting;
8 use StoreEngine\Utils\Helper;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13
14 class Order {
15
16 public static function init() {
17 // Schedule lookup table update.
18 add_action( 'storeengine/checkout/after_place_order', [ __CLASS__, 'set_product_lookup_schedule' ] );
19 add_action( 'storeengine/api/after_create_order', [ __CLASS__, 'set_product_lookup_schedule' ] );
20 add_action( 'storeengine/api/after_update_order', [ __CLASS__, 'set_product_lookup_schedule' ] );
21 add_action( 'storeengine/api/order/add_order_item', [ __CLASS__, 'set_product_lookup_schedule' ] );
22 add_action( 'storeengine/api/order/update_order_item', [ __CLASS__, 'set_product_lookup_schedule' ] );
23 add_action( 'storeengine/api/order/delete_order_item', [ __CLASS__, 'set_remove_lookup_schedule' ], 10, 3 );
24
25 // Update lookup table.
26 add_action( 'storeengine/store_product_lookup', [ __CLASS__, 'store_product_lookup' ] );
27 add_action( 'storeengine/remove_product_lookup_data', [ __CLASS__, 'remove_product_lookup_data' ], 10, 2 );
28 }
29
30 public static function set_product_lookup_schedule( $order ) {
31 if ( ! StoreEngine::init()->queue()->get_next( 'storeengine/store_product_lookup', [ $order->get_id() ] ) ) {
32 StoreEngine::init()->queue()->schedule_single( time() + 1, 'storeengine/store_product_lookup', [ $order->get_id() ] );
33 }
34 }
35
36 public static function set_remove_lookup_schedule( $order, $line_item, $line_item_id ) {
37 if ( 'line_item' !== $line_item->get_type() ) {
38 return;
39 }
40
41 if ( ! $line_item instanceof OrderItemProduct ) {
42 return;
43 }
44
45 $args = [ $order->get_id(), $line_item_id ];
46
47 if ( ! StoreEngine::init()->queue()->get_next( 'storeengine/remove_product_lookup_data', $args ) ) {
48 StoreEngine::init()->queue()->schedule_single( time() + 1, 'storeengine/remove_product_lookup_data', $args );
49 }
50 }
51
52 public static function store_product_lookup( $order_id ) {
53 global $wpdb;
54
55 $order = Helper::get_order( $order_id );
56
57 if ( ! $order ) {
58 return;
59 }
60
61 $order_items = $order->get_line_product_items();
62
63 if ( empty( $order_items ) ) {
64 return;
65 }
66
67 $defaults = [
68 'order_item_id' => 0,
69 'order_id' => $order->get_id(),
70 'product_id' => 0,
71 'variation_id' => 0,
72 'price_id' => 0,
73 'price' => 0.00,
74 'customer_id' => 0,
75 'vendor_id' => null,
76 'date_created' => null,
77 'product_qty' => 1,
78 'product_net_revenue' => 0.00,
79 'product_gross_revenue' => 0.00,
80 'coupon_amount' => 0.00,
81 'tax_amount' => 0.00,
82 'shipping_amount' => 0.00,
83 'shipping_tax_amount' => 0.00,
84 ];
85 $formats = [
86 '%d', // order_item_id
87 '%d', // order_id
88 '%d', // product_id
89 '%d', // variation_id
90 '%d', // price_id
91 '%f', // price
92 '%d', // customer_id
93 '%d', // vendor_id
94 '%s', // date_created
95 '%d', // product_qty
96 '%f', // product_net_revenue
97 '%f', // product_gross_revenue
98 '%f', // coupon_amount
99 '%f', // tax_amount
100 '%f', // shipping_amount
101 '%f', // shipping_tax_amount
102 ];
103 $values = [];
104 $placeholders = [];
105
106 $total_qty = array_sum( array_map( fn( $item ) => $item->get_quantity(), $order_items ) );
107
108 foreach ( $order_items as $order_item ) {
109 $item_coupon_amount = Formatting::format_decimal( ( $order_item->get_quantity( 'edit' ) / $total_qty ) * $order->get_discount_total( 'edit' ) );
110 $product_net_revenue = Formatting::format_decimal( $order_item->get_total( 'edit' ) );
111 $product_gross_revenue = Formatting::format_decimal( $product_net_revenue + $order_item->get_total_tax( 'edit' ) );
112
113 /**
114 * Resolve a vendor id for this lookup row. Default null. The multi-vendor
115 * addon resolves it from product post_author.
116 *
117 * @param int|null $vendor_id
118 * @param int $product_id
119 * @param object $order_item
120 * @param object $order
121 */
122 $vendor_id = apply_filters(
123 'storeengine/order/product_lookup_vendor_id',
124 null,
125 $order_item->get_product_id(),
126 $order_item,
127 $order
128 );
129
130 $value = array_merge( $defaults, [
131 'order_id' => $order->get_id(),
132 'order_item_id' => $order_item->get_id(),
133 'product_id' => $order_item->get_product_id(),
134 'price_id' => $order_item->get_price_id(),
135 'variation_id' => $order_item->get_variation_id(),
136 'customer_id' => $order->get_customer_id(),
137 'vendor_id' => null !== $vendor_id ? (int) $vendor_id : 0,
138 'price' => $order_item->get_price(),
139 'product_gross_revenue' => $product_gross_revenue,
140 'product_net_revenue' => $product_net_revenue,
141 'coupon_amount' => $item_coupon_amount,
142 'tax_amount' => $order_item->get_total_tax( 'edit' ),
143 'product_qty' => $order_item->get_quantity( 'edit' ),
144 'date_created' => current_time( 'mysql', 1 ),
145 ] );
146 $values = array_merge( $values, array_values( $value ) );
147 $placeholders[] = '(' . implode( ',', $formats ) . ')';
148 }
149
150 $placeholders = implode( ', ', $placeholders );
151
152 // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery -- Placeholders prepared above.
153 $wpdb->query(
154 $wpdb->prepare(
155 "
156 INSERT INTO {$wpdb->prefix}storeengine_order_product_lookup
157 (
158 `order_item_id`,
159 `order_id`,
160 `product_id`,
161 `variation_id`,
162 `price_id`,
163 `price`,
164 `customer_id`,
165 `vendor_id`,
166 `date_created`,
167 `product_qty`,
168 `product_net_revenue`,
169 `product_gross_revenue`,
170 `coupon_amount`,
171 `tax_amount`,
172 `shipping_amount`,
173 `shipping_tax_amount`
174 )
175 VALUES {$placeholders}
176 ON DUPLICATE KEY UPDATE
177 `order_id` = VALUES(`order_id`),
178 `product_id` = VALUES(`product_id`),
179 `variation_id` = VALUES(`variation_id`),
180 `price_id` = VALUES(`price_id`),
181 `price` = VALUES(`price`),
182 `customer_id` = VALUES(`customer_id`),
183 `vendor_id` = VALUES(`vendor_id`),
184 `date_created` = VALUES(`date_created`),
185 `product_qty` = VALUES(`product_qty`),
186 `product_net_revenue` = VALUES(`product_net_revenue`),
187 `product_gross_revenue` = VALUES(`product_gross_revenue`),
188 `coupon_amount` = VALUES(`coupon_amount`),
189 `tax_amount` = VALUES(`tax_amount`),
190 `shipping_amount` = VALUES(`shipping_amount`),
191 `shipping_tax_amount` = VALUES(`shipping_tax_amount`);
192 ",
193 $values
194 )
195 );
196 // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery
197 }
198
199 public static function remove_product_lookup_data( $order_id, $line_item_id ) {
200 global $wpdb;
201
202 // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
203 $wpdb->delete(
204 $wpdb->prefix . 'storeengine_order_product_lookup',
205 [
206 'order_item_id' => $line_item_id,
207 'order_id' => $order_id,
208 ],
209 [ '%d', '%d' ]
210 );
211 // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
212
213 $order = Helper::get_order( $order_id );
214
215 if ( $order && ! is_wp_error( $order ) ) {
216 self::set_product_lookup_schedule( $order );
217 }
218 }
219 }
220