我尝试创建具有特定到期日期的订阅。
在下面的功能中,
$product_args
是一个包含产品详细信息的数组。此数组包含product_id,regular_price等.product_id指的是订阅变量product。
$start_date_timestamp
是订阅开始日期。这个时间戳可能在几个月之前。
例如,我需要在2个月(4月1日)之前创建订阅。订阅期限为3个月,因此订阅期限必须是7月1日。
但现在问题是订阅到期时间设定为9月27日。(因为我们目前是6月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 :(得分:0)
如果您需要在现有产品的现有数据库中创建一些具有到期日期的新订单,那么您可以创建另一个处理某些参数的函数,例如$ dateStart,$ dateEnd,作为奖励礼品,您将无需计算日期范围。
您的第一个功能将适用于新订单。好。 新的已经注册的订单来处理您的应用程序中新实现的到期机制。
答案 1 :(得分: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);
答案 2 :(得分:0)
默认情况下,WooCommerce订阅将从上次付款的时间开始计算订阅的下一个支付日期。该挂钩将其更改为从计划的付款日期计算下一个付款日期,而不是实际处理付款的时间。
要解决此问题,请将此钩子添加到您的function.php主题文件中:
add_filter( 'wcs_calculate_next_payment_from_last_payment', '__return_false' );