2

I try to create a subscription with a specific expiration date.

In the function below, $product_args is an array with details about the product. This array contain product_id, regular_price, etc. The product_id refers to a subscription variable product.

$start_date_timestamp is the subscription start date. This timestamp can be some month before.

By example, I need to create a subscription starting 2 month before (the April 1st). The subscription length is 3 months, so the subscription expiration must be July 1st.

But the problem right now is that the subscription expiration is set to September 27. (Because we are currently June 27)

 static function create_subscription($customer_id,$product_args,$billing_args,$shipping_args = false,$order_notes = false,$client_notes = false,$order_status = false,$start_date_timestamp = false) {

        $start_date = false;
        if($start_date_timestamp==false)
        {
            $start_date = date( 'Y-m-d H:i:s', strtotime( 'now' ) );
        }
        else
        {
            $start_date = date( 'Y-m-d H:i:s', $start_date_timestamp);
        }

        $order_id = \perfo\woocommerce\order::create_order( $customer_id, $product_args, $billing_args,$shipping_args,$order_notes,$client_notes,$order_status);

        if ( ! is_wp_error($order_id) )
        {
            $wc_product = wc_get_product($product_args['product_id']);

            $period = \WC_Subscriptions_Product::get_period( $wc_product );
            $interval = \WC_Subscriptions_Product::get_interval( $wc_product );
            $length = \WC_Subscriptions_Product::get_length( $wc_product );
            $expiration = \WC_Subscriptions_Product::get_expiration_date( $product_args['product_id'],$start_date );

            $subscription = wcs_create_subscription(array('order_id' => $order_id, 'billing_period' => $period, 'billing_interval' => $interval, 'start_date' => $start_date,'end'=>$expiration));

            if(!is_wp_error($subscription))
            {
                $wc_product->set_regular_price($product_args['regular_price']);
                $wc_product->set_price($product_args['regular_price']);
                $subscription->add_product( $wc_product, $product_args['quantity']);

                $subscription->set_address( $billing_args, 'billing' );

                if($shipping_args)
                    $subscription->set_address( $shipping_args, 'shipping' );

                $subscription->update_dates( array(
                    'end'          => $expiration,
                ) );

                $subscription->calculate_totals();
                $subscription->save();

            }
            else
            {
                wp_delete_post($order_id,true);
            }

            return $subscription;
        }

        return $order_id;
    }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Sebastien
  • 240
  • 2
  • 7
  • 16
  • Please post the code you're using to call this function and all the parameters that are being passed to it. Thx! – Spartacus Jun 29 '17 at 20:34

3 Answers3

1

Your end date must be valid date and it must be in format 'Y-m-d H:i:s'

Please try this code. This is working fine at my end

$current_date = date("Y-m-d H:i:s") ;

// Setting start date to 1 day before the current date : you can set as require
$start_date = date('Y-m-d H:i:s',strtotime($current_date . "-1 days"));           

// end date : setting 12 month after the current date : you can set as require
$end_date = date('Y-m-d H:i:s',strtotime("+12 months", strtotime($start_date)));

$dates_to_update = array();
$dates_to_update['end'] = $end_date;
// $dates_to_update['next_payment'] = $end_date;
// $dates_to_update['trial_end'] = $end_date;
$subscription->update_dates($dates_to_update);

https://hotexamples.com/examples/-/WC_Subscriptions_Product/get_expiration_date/php-wc_subscriptions_product-get_expiration_date-method-examples.html

https://hotexamples.com/examples/-/-/wcs_is_datetime_mysql_format/php-wcs_is_datetime_mysql_format-function-examples.html

Rakesh Soni
  • 10,135
  • 5
  • 44
  • 51
1

by default, WooCommerce Subscriptions will calculate the next payment date for a subscription from the time of the last payment. This hook changes it to calculate the next payment date from the scheduled payment date, not the time the payment was actually processed.

To resolve, add this hook in your function.php theme file :

add_filter( 'wcs_calculate_next_payment_from_last_payment', '__return_false' );
davidebr90
  • 43
  • 7
0

If you need to create inside an existing database of existing products some new orders with expiration dates, then you can make another function which handle some parameters like $dateStart, $dateEnd, and as a bonus gift you will be free of calculating date ranges.

Your first function will apply to new orders. OK. The new one to already registered orders to handle the expiration mecanism newly implemented in your application.

BendaThierry.com
  • 2,080
  • 1
  • 15
  • 17