2017-06-27 28 views
3

特定の有効期限のサブスクリプションを作成しようとしています。Woocommerceサブスクリプションの変更有効期限

以下の機能では、 $product_argsは、製品の詳細な配列です。この配列には、product_id、regular_priceなどが含まれます。product_idは、サブスクリプション変数プロダクトを参照します。

$start_date_timestampは、購読開始日です。このタイムスタンプは数ヶ月前になる可能性があります。

たとえば、2か月前(4月1日)に開始するサブスクリプションを作成する必要があります。サブスクリプション期間は3ヶ月ですので、サブスクリプション期限は7月1日でなければなりません。

しかし、今の問題は(私たちは現在、6月27日なので)、サブスクリプションの有効期限が9月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; 
    } 
+0

あなたはこの関数と、それに渡されるすべてのパラメータを呼び出すために使用しているコードを投稿してくださいで正常に動作しています。どうも! – Spartacus

答えて

0

あなたは、既存の製品の既存のデータベース内のいくつかの新しいオーダーを作成する必要がある場合有効期限は$ dateStart、$ dateEndのようないくつかのパラメータを扱う別の関数を作ることができ、ボーナスギフトとして日付範囲を計算することはできません。

あなたの最初の機能は新しい注文に適用されます。 OK。 あなたのアプリケーションで新たに実装された有効期限を処理するための注文を既に登録した新しいもの。

0

あなたの終了日が有効な日付である必要があり、それは形式でなければなりません 'Y-M-DのH:I:s' が

このコードを試してみてください。これが私の最後

$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

関連する問題