type ) {
$start_date = time();
$next_date = sdevs_wp_strtotime( $recurr_timing, $start_date );
if ( $trial && ! empty( $trial ) ) {
$trial_started = get_post_meta( $subscription_id, '_subscrpt_trial_started', true );
$trial_ended = get_post_meta( $subscription_id, '_subscrpt_trial_ended', true );
if ( empty( $trial_started ) && empty( $trial_ended ) ) {
$start_date = sdevs_wp_strtotime( $trial );
update_post_meta( $subscription_id, '_subscrpt_trial_started', time() );
update_post_meta( $subscription_id, '_subscrpt_trial_ended', $start_date );
update_post_meta( $subscription_id, '_subscrpt_trial_mode', 'on' );
$next_date = $start_date;
}
}
update_post_meta( $subscription_id, '_subscrpt_start_date', $start_date );
} elseif ( 'renew' === $subscription_history->type ) {
if ( $trial ) {
delete_post_meta( $subscription_id, '_subscrpt_trial' );
delete_post_meta( $subscription_id, '_subscrpt_trial_mode' );
delete_post_meta( $subscription_id, '_subscrpt_trial_started' );
delete_post_meta( $subscription_id, '_subscrpt_trial_ended' );
}
$next_date = sdevs_wp_strtotime( $recurr_timing, time() );
} elseif ( 'early-renew' === $subscription_history->type ) {
$next_date = sdevs_wp_strtotime( $recurr_timing, get_post_meta( $subscription_id, '_subscrpt_next_date', true ) );
if ( $trial ) {
$trial_mode = get_post_meta( $subscription_id, '_subscrpt_trial_mode', true );
if ( 'on' === $trial_mode ) {
update_post_meta( $subscription_id, '_subscrpt_trial_mode', 'extended' );
$next_date = sdevs_wp_strtotime( $recurr_timing, get_post_meta( $subscription_id, '_subscrpt_trial_ended', true ) );
}
}
}
update_post_meta( $subscription_id, '_subscrpt_next_date', $next_date );
}
/**
* Add custom column on order item.
*
* @return void
*/
public function register_custom_column() {
?>
|
get_id();
$subscription_id = Helper::get_subscription_from_order_item_id( $item->get_id() );
if ( ! $subscription_id ) {
echo "- | ";
return;
}
$subscription_id = $subscription_id->subscription_id;
$price = get_post_meta( $subscription_id, '_subscrpt_price', true );
$subtotal = Helper::format_price_with_order_item( $price, $item_id );
?>
|
2;
if ( $has_trial ) {
echo '
+ Got ' . $trial . ' free trial!';
}
}
/**
* Take some actions based on order status changed.
*
* @param int $order_id Order Id.
*/
public function order_status_changed( $order_id ) {
$order = wc_get_order( $order_id );
$post_status = 'active';
switch ( $order->get_status() ) {
case 'on-hold':
case 'pending':
$post_status = 'pending';
break;
case 'refunded':
case 'failed':
case 'cancelled':
$post_status = 'cancelled';
break;
default:
$post_status = 'active';
break;
}
$post_status = apply_filters( 'subscript_order_status_to_post_status', $post_status, $order );
global $wpdb;
$table_name = $wpdb->prefix . 'subscrpt_order_relation';
// @phpcs:ignore
$histories = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE order_id=%d', array( $table_name, $order_id ) ) );
foreach ( $histories as $history ) {
if ( 'new' === $history->type || 'renew' === $history->type ) {
wp_update_post(
array(
'ID' => $history->subscription_id,
'post_status' => $post_status,
)
);
Action::write_comment( $post_status, $history->subscription_id );
} else {
do_action( 'subscrpt_order_status_changed', $order, $history );
}
}
}
/**
* Delete the subscription.
*
* @param int $order_id Order ID.
*
* @return void
*/
public function delete_the_subscription( $order_id ) {
global $wpdb;
$table_name = $wpdb->prefix . 'subscrpt_order_relation';
$histories = Helper::get_subscriptions_from_order( $order_id );
foreach ( (array) $histories as $history ) {
$subscription_order_id = get_post_meta( $history->subscription_id, '_subscrpt_order_id', true );
if ( (int) $subscription_order_id === $order_id ) {
wp_delete_post( $history->subscription_id, true );
}
}
// phpcs:ignore
$wpdb->delete( $table_name, array( 'order_id' => $order_id ), array( '%d' ) );
}
}