Skip to content

Commit 73a44db

Browse files
authored
Merge pull request #343 from skyverge/ch7127-add-standard-subscription-data-to-orders
[ch7127] Add standard subscription data to orders
2 parents a4edbed + 2c2680e commit 73a44db

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

woocommerce/changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*** SkyVerge WooCommerce Plugin Framework Changelog ***
22

33
2019.nn.nn - version 5.4.2-dev.1
4+
* Tweak - Add a standard set of subscription details to orders payment data set by a gateway
45
* Tweak - Add replacement helper methods to get the current screen in WordPress and check the screen ID
56
* Misc - Change SV_WC_Payment_Gateway::is_configured() from protected to public
67
* Misc - Add admin notice when a gateway is enabled but is not configured and is unable to take payments

woocommerce/payment-gateway/integrations/class-sv-wc-payment-gateway-integration-subscriptions.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ public function add_support() {
8181
// save token/customer ID to subscription objects
8282
add_action( 'wc_payment_gateway_' . $this->get_gateway()->get_id() . '_add_transaction_data', array( $this, 'save_payment_meta' ), 10, 2 );
8383

84+
// add additional subscription details to orders along with payment data
85+
add_action( 'wc_payment_gateway_' . $this->get_gateway()->get_id() . '_get_order_base', [ $this, 'add_subscriptions_details_to_order' ], 10, 2 );
86+
8487
// process renewal payments
8588
add_action( 'woocommerce_scheduled_subscription_payment_' . $this->get_gateway()->get_id(), array( $this, 'process_renewal_payment' ), 10, 2 );
8689

@@ -193,6 +196,115 @@ public function save_payment_meta( $order ) {
193196
}
194197

195198

199+
/**
200+
* Adds subscription details to order base data.
201+
*
202+
* Details are added to a \WC_Order::$payment->subscriptions[] property.
203+
* @see SV_WC_Payment_Gateway_Integration_Subscriptions::add_subscription_details_to_order()
204+
* @internal
205+
*
206+
* @since 5.4.2-dev.1
207+
*
208+
* @param \WC_Order $order order object
209+
* @param SV_WC_Payment_Gateway $gateway payment gateway
210+
* @return \WC_Order
211+
*/
212+
public function add_subscriptions_details_to_order( $order, $gateway ) {
213+
214+
if ( isset( $order->payment ) ) {
215+
216+
// defaults
217+
$order->payment->subscriptions = [];
218+
$order->payment->recurring = ! empty( $order->payment->recurring ) ?: false;
219+
220+
// if the order contains a subscription (but is not a renewal)
221+
if ( wcs_order_contains_subscription( $order ) ) {
222+
223+
$order->payment->recurring = true;
224+
225+
$subscriptions = wcs_get_subscriptions_for_order( $order );
226+
227+
if ( ! empty( $subscriptions ) ) {
228+
229+
foreach ( $subscriptions as $subscription ) {
230+
231+
if ( $subscription instanceof \WC_Subscription ) {
232+
233+
$order->payment->subscriptions[] = $this->add_subscription_details_to_order( $subscription, false );
234+
}
235+
}
236+
}
237+
238+
// order is for a subscription renewal
239+
} elseif ( wcs_order_contains_renewal( $order ) ) {
240+
241+
$order->payment->recurring = true;
242+
243+
$subscriptions = wcs_get_subscriptions_for_renewal_order( $order );
244+
245+
if ( ! empty( $subscriptions ) ) {
246+
247+
foreach ( $subscriptions as $subscription ) {
248+
249+
if ( $subscription instanceof \WC_Subscription ) {
250+
251+
$order->payment->subscriptions[] = $this->add_subscription_details_to_order( $subscription, true );
252+
}
253+
}
254+
}
255+
}
256+
}
257+
258+
return $order;
259+
}
260+
261+
262+
/**
263+
* Builds and returns a subscription's details data.
264+
*
265+
* @see SV_WC_Payment_Gateway_Integration_Subscriptions::add_subscriptions_details_to_order()
266+
*
267+
* \stdClass {
268+
* int $id the subscription's ID
269+
* bool $is_renewal whether the order is for a subscription renewal
270+
* bool $is_installment whether the subscription is for an installment
271+
* bool $is_first whether it is the first payment for an installment series
272+
* bool $is_last whether it is the last payment for an installment series
273+
*}
274+
*
275+
* @since 5.4.2-dev.1
276+
*
277+
* @param \WC_Subscription $subscription subscription object
278+
* @param bool $renewal whether the subscription is a renewal
279+
* @return \stdClass subscription details data
280+
*/
281+
protected function add_subscription_details_to_order( $subscription, $renewal ) {
282+
283+
$details = new \stdClass;
284+
285+
$details->id = max( 0, (int) SV_WC_Order_Compatibility::get_prop( $subscription, 'id' ) );
286+
$details->is_renewal = (bool) $renewal;
287+
$details->is_installment = (bool) $subscription->get_date( 'end' );
288+
289+
$details->is_first = $details->is_last = false;
290+
291+
if ( $details->is_installment ) {
292+
293+
// if this is not a renewal, but the subscription has an end date, then this must be the first installment
294+
if ( ! $details->is_renewal ) {
295+
$details->is_first = true;
296+
}
297+
298+
// if the subscription has an end date, but there is no next payment date set, this must be the last installment
299+
if ( ! (bool) $subscription->get_date( 'next_payment' ) ) {
300+
$details->is_last = true;
301+
}
302+
}
303+
304+
return $details;
305+
}
306+
307+
196308
/**
197309
* Process a subscription renewal payment
198310
*

0 commit comments

Comments
 (0)