本文整理汇总了PHP中WC_Order::get_user_id方法的典型用法代码示例。如果您正苦于以下问题:PHP WC_Order::get_user_id方法的具体用法?PHP WC_Order::get_user_id怎么用?PHP WC_Order::get_user_id使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WC_Order
的用法示例。
在下文中一共展示了WC_Order::get_user_id方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: get_role_price
public static function get_role_price($product)
{
global $post, $woocommerce;
if (is_admin()) {
//admin screen
if (!empty($_POST['selected_user_id'])) {
$user = get_user_by('id', $_POST['selected_user_id']);
} else {
//!empty($_POST['order_id'])
$order = new WC_Order($_POST['order_id']);
$user_id = $order->get_user_id();
$user = get_user_by('id', $user_id);
}
} else {
// user screen
$user = wp_get_current_user();
}
$user_roles = $user->roles;
if ($user_roles == null) {
return null;
}
$user_role = array_shift($user_roles);
if ($user_role !== null) {
$role_price = get_post_meta($product->variation_id, $user_role, true);
if (!empty($role_price)) {
return $role_price;
}
}
return null;
}
示例2: get_order_user_id
/**
* Get the user ID for an order
*
* @since 3.0.0
* @param \WC_Order $order
* @return int
*/
public static function get_order_user_id(WC_Order $order)
{
if (self::is_wc_version_gte_2_2()) {
return $order->get_user_id();
} else {
return $order->customer_user ? $order->customer_user : 0;
}
}
示例3: get_profile_data
/**
* Helper method to return required profile data when working with customer profiles
*
* @since 2.0.0
* @param WC_Order $order the order object
* @param array $additional_data optional additional data to be set, like payment profile data
* @return array profile data
*/
protected function get_profile_data(WC_Order $order, $additional_data = array())
{
$data = array('merchantCustomerId' => $order->get_user_id(), 'email' => is_email($this->order->billing_email) ? $this->order->billing_email : '');
if ($additional_data) {
$data = array_merge($data, $additional_data);
}
return $data;
}
开发者ID:DustinHartzler,项目名称:TheCLEFT,代码行数:16,代码来源:class-wc-authorize-net-cim-api-customer-profile-request.php
示例4: sendCustomerInvoice
function sendCustomerInvoice($order_id)
{
PlexLog::addLog(' Info =>@sendCustomerInvoice order_id=' . $order_id);
$wooCommerceOrderObject = new WC_Order($order_id);
$user_id = $wooCommerceOrderObject->get_user_id();
$customerMailId = $wooCommerceOrderObject->billing_email;
$invoice_mail_content = '';
$invoice_mail_content .= '
<table border="0" cellpadding="0" cellspacing="0" width="600" id="template_container">
<tbody>
<tr>
<td align="center" valign="top">
<font face="Arial" style="font-weight:bold;background-color:#8fd1c8;color:#202020;"></font>
<table border="0" cellpadding="0" cellspacing="0" width="600" id="template_header" bgcolor="#8fd1c8">
<tbody>
<tr >
<td width="20" height="20" bgcolor="#8fd1c8"> </td>
<td bgcolor="#8fd1c8"> </td>
<td width="20" bgcolor="#8fd1c8"> </td>
</tr>
<tr >
<td width="20" bgcolor="#8fd1c8"> </td>
<td bgcolor="#8fd1c8" style="font-weight:bold;font-size:24px;color:#ffffff;" ><h2 style="margin:0;">Welcome to ' . get_option('blogname') . '! <br>Thank you for your order. We\'ll be in touch shortly with additional order and shipping information.</h2></td>
<td width="20" bgcolor="#8fd1c8"> </td>
</tr>
<tr >
<td width="20" height="20" bgcolor="#8fd1c8"> </td>
<td bgcolor="#8fd1c8"> </td>
<td width="20" bgcolor="#8fd1c8"> </td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td align="center" valign="top">
<table border="0" cellpadding="0" cellspacing="0" width="600" id="template_body">
<tbody>
<tr>
<td valign="top">
<font style="background-color:#f9f9f9">
<table border="0" cellpadding="20" cellspacing="0" width="100%">
<tbody>
<tr>
<td valign="top">
<div>
<font face="Arial" align="left" style="font-size:18px;color:#8a8a8a">
<p>Your order #' . get_post_meta($order_id, "plexOrderId", true) . ' has been received and is now being processed. Your order details are shown below for your reference:</p>
<table cellspacing="0" cellpadding="6" border="1" style="width:100%;">
<thead>
<tr>
<th scope="col">
<font align="left">Product</font>
</th>
<th scope="col">
<font align="left">Quantity</font>
</th>
<th scope="col">
<font align="left">Price</font>
</th>
</tr>
</thead>
<tbody>';
$allItems = $wooCommerceOrderObject->get_items();
foreach ($allItems as $key => $value) {
$invoice_mail_content .= '
<tr>
<td>
<font align="left">' . $value["name"] . '<br><small></small></font>
</td>
<td>
<font align="left">' . $value["qty"] . '</font>
</td>
<td>
<font align="left">
<span class="amount">$' . $value["line_subtotal"] . '</span>
</font>
</td>
</tr>';
}
$invoice_mail_content .= '
</tbody>
<tfoot style="text-align: left;">
<tr>
<th scope="row" colspan="2">
<font align="left">Cart Subtotal:</font>
</th>
<td>
<font align="left">
<span class="amount">$' . sprintf('%0.2f', $wooCommerceOrderObject->get_subtotal()) . '</span>
</font>
</td>
</tr>';
if ($wooCommerceOrderObject->get_total_discount() > 0.0) {
$invoice_mail_content .= '<tr>
<th scope="row" colspan="2">
<font align="left">Discount:</font>
</th>
<td>
<font align="left">
//.........这里部分代码省略.........
示例5: maybe_render_payment_method_1_5
/**
* Render the payment method used for a subscription in the "My Subscriptions" table
*
* @since 4.1.0
* @param string $payment_method_to_display the default payment method text to display
* @param array $subscription_details the subscription details
* @param WC_Order $order the order containing the subscription
* @return string the subscription payment method
*/
public function maybe_render_payment_method_1_5($payment_method_to_display, $subscription_details, $order)
{
// bail for other payment methods
if ($this->get_gateway()->get_id() !== $order->recurring_payment_method) {
return $payment_method_to_display;
}
$token = $this->get_gateway()->get_payment_token($order->get_user_id(), $this->get_gateway()->get_order_meta($order->id, 'payment_token'));
if (is_object($token)) {
$payment_method_to_display = sprintf(_x('Via %s ending in %s', 'Supports direct payment method subscriptions', $this->get_gateway()->get_text_domain()), $token->get_type_full(), $token->get_last_four());
}
return $payment_method_to_display;
}
开发者ID:shredzjc,项目名称:wc-plugin-framework,代码行数:21,代码来源:class-sv-wc-payment-gateway-integration-subscriptions.php
示例6: set_order_addresses
/**
* Helper method to set/update the billing & shipping addresses for an order.
*
* @since 2.5.0
* @param WC_Order $order
* @param array $data
*/
protected function set_order_addresses($order, $data)
{
$address_fields = array('first_name', 'last_name', 'company', 'email', 'phone', 'address_1', 'address_2', 'city', 'state', 'postcode', 'country');
$billing_address = $shipping_address = array();
// billing address.
if (isset($data['billing_address']) && is_array($data['billing_address'])) {
foreach ($address_fields as $field) {
if (isset($data['billing_address'][$field])) {
$billing_address[$field] = wc_clean($data['billing_address'][$field]);
}
}
unset($address_fields['email']);
unset($address_fields['phone']);
}
// shipping address.
if (isset($data['shipping_address']) && is_array($data['shipping_address'])) {
foreach ($address_fields as $field) {
if (isset($data['shipping_address'][$field])) {
$shipping_address[$field] = wc_clean($data['shipping_address'][$field]);
}
}
}
$order->set_address($billing_address, 'billing');
$order->set_address($shipping_address, 'shipping');
// update user meta
if ($order->get_user_id()) {
foreach ($billing_address as $key => $value) {
update_user_meta($order->get_user_id(), 'billing_' . $key, $value);
}
foreach ($shipping_address as $key => $value) {
update_user_meta($order->get_user_id(), 'shipping_' . $key, $value);
}
}
}
示例7: add_add_payment_method_customer_data
/**
* Add customer data as part of the add payment method transaction, primarily
* customer ID
*
* @since 4.0.0
* @param WC_Order $order mock order
* @param SV_WC_Payment_Gateway_API_Create_Payment_Token_Response $response
*/
protected function add_add_payment_method_customer_data($order, $response)
{
$user_id = $order->get_user_id();
// set customer ID from response if available
if ($this->supports_customer_id() && method_exists($response, 'get_customer_id') && $response->get_customer_id()) {
$order->customer_id = $customer_id = $response->get_customer_id();
} else {
// default to the customer ID on "order"
$customer_id = $order->customer_id;
}
// update the user
if (0 != $user_id) {
$this->update_customer_id($user_id, $customer_id);
}
}
示例8: maybe_render_payment_method_1_5
/**
* Render the payment method used for a subscription in the "My Subscriptions" table
*
* @since 4.1.0
* @param string $payment_method_to_display the default payment method text to display
* @param array $subscription_details the subscription details
* @param WC_Order $order the order containing the subscription
* @return string the subscription payment method
*/
public function maybe_render_payment_method_1_5($payment_method_to_display, $subscription_details, $order)
{
// bail for other payment methods
if ($this->get_gateway()->get_id() !== $order->recurring_payment_method) {
return $payment_method_to_display;
}
$token = $this->get_gateway()->get_payment_tokens_handler()->get_token($order->get_user_id(), $this->get_gateway()->get_order_meta($order->id, 'payment_token'));
if (is_object($token)) {
$payment_method_to_display = sprintf(__('Via %s ending in %s', 'woocommerce-plugin-framework'), $token->get_type_full(), $token->get_last_four());
}
return $payment_method_to_display;
}
开发者ID:DustinHartzler,项目名称:TheCLEFT,代码行数:21,代码来源:class-sv-wc-payment-gateway-integration-subscriptions.php
示例9: last_buy
/**
* @param $order_id
*/
public function last_buy($order_id)
{
$order = new \WC_Order($order_id);
$user_id = $order->get_user_id();
update_user_meta($user_id, 'hl_last_buy', Carbon::now()->toDateString());
}
示例10: complete_order
/**
* sensei_woocommerce_complete_order description
* @since 1.0.3
* @access public
* @param int $order_id WC order ID
* @return void
*/
public static function complete_order($order_id = 0)
{
$order_user = array();
// Check for WooCommerce
if (Sensei_WC::is_woocommerce_active() && 0 < $order_id) {
// Get order object
$order = new WC_Order($order_id);
$user = get_user_by('id', $order->get_user_id());
$order_user['ID'] = $user->ID;
$order_user['user_login'] = $user->user_login;
$order_user['user_email'] = $user->user_email;
$order_user['user_url'] = $user->user_url;
// Run through each product ordered
if (0 < sizeof($order->get_items())) {
foreach ($order->get_items() as $item) {
$product_type = '';
if (isset($item['variation_id']) && 0 < $item['variation_id']) {
$item_id = $item['variation_id'];
$product_type = 'variation';
} else {
$item_id = $item['product_id'];
}
// End If Statement
$_product = Sensei_WC::get_product_object($item_id, $product_type);
// Get courses that use the WC product
$courses = Sensei()->course->get_product_courses($_product->id);
// Loop and update those courses
foreach ($courses as $course_item) {
$update_course = Sensei()->woocommerce_course_update($course_item->ID, $order_user);
}
// End For Loop
}
// End For Loop
}
// End If Statement
// Add meta to indicate that payment has been completed successfully
update_post_meta($order_id, 'sensei_payment_complete', '1');
}
// End If Statement
}
示例11: process_payment
/**
* process_payment
*
* Completes the initial payment on the subscription order
*
* Although PayPal Braintree supports subscription products (they call them plans),
* they don't support the wide variety of intervals we do, nor multiple subscription
* products in a single order.
*
* So, this extension does all the subscription work itself, storing a customer
* in the vault on the first payment, and using those stored credentials for
* renewal orders
*
* @param int $order_id
* @return mixed
*/
public function process_payment($order_id)
{
// If the order contains no subscriptions, just let the parent process it
if (!$this->order_contains_subscription($order_id)) {
return parent::process_payment($order_id);
}
$order = new WC_Order($order_id);
$this->log(__FUNCTION__, "Info: Beginning processing of payment for (subscription) order {$order_id} for the amount of {$order->order_total}");
$this->log(__FUNCTION__, "Info: Merchant ID = {$this->merchant_id}");
$paypal_braintree_nonce = self::get_posted_variable('paypalbraintree_nonce');
if (empty($paypal_braintree_nonce)) {
$this->log(__FUNCTION__, 'Error: The paypal_braintree_nonce was unexpectedly empty');
wc_add_notice(__('Error: PayPal Powered by Braintree did not supply a payment nonce. Please try again later or use another means of payment.', 'woocommerce-gateway-paypal-braintree'), 'error');
return false;
}
$user_id = $order->get_user_id();
if (!is_user_logged_in()) {
$this->log(__FUNCTION__, 'Error: No user logged in / being created for the initial subscription payment');
wc_add_notice(__('Error: You must login or create an account before you can purchase a subscription.', 'woocommerce-gateway-paypal-braintree'), 'error');
return false;
}
// Create the gateway instance up front
require_once dirname(__FILE__) . '/../braintree_sdk/lib/Braintree.php';
$gateway = new Braintree_Gateway(array('accessToken' => $this->merchant_access_token));
// Check their user meta for a stored braintree customer id
$braintree_customer_id = get_user_meta($user_id, '_wc_paypal_braintree_customer_id', true);
if (empty($braintree_customer_id)) {
$this->log(__FUNCTION__, 'Info: Did not find braintree customer id on user meta. Need to create customer');
// Create a new customer id, passing the nonce so we can add the card to the vault
// ref https://developers.braintreepayments.com/reference/request/customer/create/php
$customer_args = array('firstName' => $order->billing_first_name, 'lastName' => $order->billing_last_name, 'company' => $order->billing_company, 'phone' => $order->billing_phone, 'email' => $order->billing_email, 'paymentMethodNonce' => $paypal_braintree_nonce);
try {
$result = $gateway->customer()->create($customer_args);
} catch (Exception $e) {
$this->log(__FUNCTION__, 'Error: Unable to create customer. Reason: ' . $e->getMessage());
wc_add_notice(__('Error: PayPal Powered by Braintree was unable to create a customer record for you. Please try again later or use another means of payment.', 'woocommerce-gateway-paypal-braintree'), 'error');
return false;
}
if (!$result->success) {
$this->log(__FUNCTION__, "Error: Unable to create customer: {$result->message}");
wc_add_notice(__('Error: PayPal Powered by Braintree was unable to create a customer record for you. Please try again later or use another means of payment.', 'woocommerce-gateway-paypal-braintree'), 'error');
return false;
}
$braintree_customer_id = $result->customer->id;
update_user_meta($user_id, '_wc_paypal_braintree_customer_id', $braintree_customer_id);
$this->log(__FUNCTION__, "Info: Created customer successfully - braintree customer id = {$braintree_customer_id}");
$payment_methods = $result->customer->paymentMethods;
$payment_method_token = '';
foreach ((array) $payment_methods as $payment_method) {
if ($payment_method->default) {
$payment_method_token = $payment_method->token;
}
}
$authentication = array('paymentMethodToken' => $payment_method_token);
} else {
// We found the braintree customer id in the customer's meta
$this->log(__FUNCTION__, "Info: Found a braintree customer id in the users meta - customer id = {$braintree_customer_id}");
$authentication = array('paymentMethodNonce' => $paypal_braintree_nonce);
}
$sale_args = $this->generate_sales_args($order, $braintree_customer_id);
$sale_args = array_merge($sale_args, $authentication);
$transaction_id = '';
// Process trial periods and possible coupon discounts.
if (isset($sale_args['amount']) && 0.0 === doubleval($sale_args['amount'])) {
$user_id = $order->get_user_id();
$this->log(__FUNCTION__, "Zero payment amount for trial or coupon. Order ID: {$order_id}, User ID: {$user_id}");
$customer = $gateway->customer()->find($braintree_customer_id);
$payment_method_token = '';
foreach ((array) $customer->paymentMethods as $payment_method) {
if ($payment_method->default) {
$payment_method_token = $payment_method->token;
}
}
} else {
// charges more than zero should be sent away
// We have a customer id now, so let's do the sale and store the payment method in the vault.
$result = $gateway->transaction()->sale($sale_args);
if (!$result->success) {
$notice = sprintf(__('Error: PayPal Powered by Braintree was unable to complete the transaction. Please try again later or use another means of payment. Reason: %s', 'woocommerce-gateway-paypal-braintree'), $error_message);
wc_add_notice($notice, 'error');
$this->log(__FUNCTION__, "Error: Unable to complete transaction. Reason: {$result->message}");
return false;
}
$transaction_id = $result->transaction->id;
//.........这里部分代码省略.........
示例12: sensei_woocommerce_reactivate_subscription
/**
* Runs when an subscription is re-activated after suspension.
* @since 1.3.3
* @access public
* @param integer $user_id User ID
* @param integer $subscription_key Subscription Unique Key
* @return void
*/
public function sensei_woocommerce_reactivate_subscription($user_id, $subscription_key)
{
$subscription = WC_Subscriptions_Manager::get_users_subscription($user_id, $subscription_key);
$order = new WC_Order($subscription['order_id']);
$user = get_user_by('id', $order->get_user_id());
$order_user = array();
$order_user['ID'] = $user->ID;
$order_user['user_login'] = $user->user_login;
$order_user['user_email'] = $user->user_email;
$order_user['user_url'] = $user->user_url;
$courses = $this->post_types->course->get_product_courses($subscription['product_id']);
foreach ($courses as $course_item) {
$update_course = $this->woocommerce_course_update($course_item->ID, $order_user);
}
// End For Loop
}
示例13: get_customer_id
/**
* Get customer ID.
*
* @param WC_Order $order Order data.
*
* @return string Customer ID.
*/
public function get_customer_id($order)
{
$user_id = $order->get_user_id();
// Try get a saved customer ID.
if (0 < $user_id) {
$customer_id = get_user_meta($user_id, '_iugu_customer_id', true);
if ($customer_id) {
return $customer_id;
}
}
// Create customer in Iugu.
$customer_id = $this->create_customer($order);
// Save the customer ID.
if (0 < $user_id) {
update_user_meta($user_id, '_iugu_customer_id', $customer_id);
}
return $customer_id;
}
示例14: authorize
/**
* Process an standard auth/vault transaction
*
* @param Realex_API $realex_client realex api client
* @param WC_Order $order the order
* @param array $data optional post data to use in place of $_POST
* @param boolean $increment_retry_count optional whether to increment the retry
* count to avoid order number clashes, defaults to true. It's important
* that this be true for, and only for, the first request of any transaction
*/
public function authorize($realex_client, $order, $data = null, $increment_retry_count = true)
{
// paying with a tokenized cc?
$vault_card_ref = $this->get_post('realex_card_ref', $data);
// subscription support, this is a scheduled subscription payment, and not a live payment
// TODO: this is terrible, refactor all this when possible
$subscription = false;
if (get_post_meta($order->id, '_realex_cardref')) {
$vault_card_ref = get_post_meta($order->id, '_realex_cardref', true);
$subscription = true;
}
// user in the vault?
$vault_payer_ref = null;
if ($order->get_user_id()) {
$vault_payer_ref = get_user_meta($order->get_user_id(), 'woocommerce_realex_payerref', true);
}
// create/update the customer in the vault as needed
if ($this->vault_available() && !$subscription) {
if ($vault_payer_ref) {
// update the vault payer info
$this->update_vault_payer($realex_client, $vault_payer_ref, $order);
}
// new vault payer (non-guest checkout)
if (!$vault_payer_ref && $this->get_post('realex_vault_new', $data) && $order->get_user_id()) {
$vault_payer_ref = $this->get_new_payer_ref($order->get_user_id());
$this->create_new_vault_payer($realex_client, $vault_payer_ref, $order);
}
// add a new card?
if ($vault_payer_ref && $this->get_post('realex_vault_new', $data) && !$vault_card_ref) {
$card_type = $this->get_post('realex_cardType', $data);
// generate a unique vault card ref and add the card
$vault_card_ref = $this->get_new_card_ref($order->get_user_id(), $card_type);
$vault_new_card_response = $this->create_new_vault_card($realex_client, $vault_payer_ref, $vault_card_ref, $order, $data);
if ($vault_new_card_response && $vault_new_card_response->result == '501' && stripos($vault_new_card_response->message, 'There is no such payer ref') !== false) {
// the payerref we have on file is invalid: delete it and attempt to create a new one and add this card to it
$this->delete_payer_ref($order->get_user_id());
$vault_payer_ref = $this->get_new_payer_ref($order->get_user_id());
$this->create_new_vault_payer($realex_client, $vault_payer_ref, $order);
if ($vault_payer_ref) {
// if vault payer was added try one last time to add the card
$vault_card_ref = $this->get_new_card_ref($order->get_user_id(), $card_type);
$this->create_new_vault_card($realex_client, $vault_payer_ref, $vault_card_ref, $order, $data);
}
}
}
}
// with subscriptions, the payment total can be zero for the initial setup
if ($order->payment_total == 0) {
if ($vault_card_ref) {
// record the card token used for this order
update_post_meta($order->id, '_realex_cardref', (string) $vault_card_ref);
}
return;
}
$cards = null;
if ($vault_card_ref) {
// paying using an existing token
$cards = get_user_meta($order->get_user_id(), 'woocommerce_realex_cc', true);
// validate the input
if (!isset($cards[$vault_card_ref])) {
SV_WC_Helper::wc_add_notice(__("An error occurred, try an alternate form of payment", 'woocommerce-gateway-realex'), 'error');
return;
}
// perform the auth transaction using the supplied token (never 3DSecure)
$response = $this->vault_auth_request($realex_client, $vault_payer_ref, $cards[$vault_card_ref]['type'], $vault_card_ref, $order);
} else {
// perform the regular auth transaction
$response = $this->auth_request($realex_client, $order, $data, $increment_retry_count);
}
if (!$response) {
SV_WC_Helper::wc_add_notice(__('Connection error', 'woocommerce-gateway-realex'), 'error');
return;
}
if ('00' == $response->result) {
// Successful payment
if (!$realex_client->verify_transaction_signature($response)) {
// response was not properly signed by realex
SV_WC_Helper::wc_add_notice(__('Error - invalid transaction signature, check your Realex settings.', 'woocommerce-gateway-realex'), 'error');
// if debug mode load the response into the messages object
if ($this->is_debug_mode()) {
$this->response_debug_message($response, $vault_card_ref ? "Response: receipt-in" : "Response: auth", 'error');
}
return;
}
// if debug mode load the response into the messages object
if ($this->is_debug_mode()) {
$this->response_debug_message($response, $vault_card_ref ? "Response: receipt-in" : "Response: auth", 'message', true);
}
// collect the credit card information used for this transaction, whether it was a vaulted transaction or a regular
if ($vault_card_ref) {
//.........这里部分代码省略.........
示例15: create_pending_subscription_for_order
/**
* Uses the details of an order to create a pending subscription on the customers account
* for a subscription product, as specified with $product_id.
*
* @param int|WC_Order $order The order ID or WC_Order object to create the subscription from.
* @param int $product_id The ID of the subscription product on the order, if a variation, it must be the variation's ID.
* @param array $args An array of name => value pairs to customise the details of the subscription, including:
* 'start_date' A MySQL formatted date/time string on which the subscription should start, in UTC timezone
* 'expiry_date' A MySQL formatted date/time string on which the subscription should expire, in UTC timezone
* @since 1.1
*/
public static function create_pending_subscription_for_order($order, $product_id, $args = array())
{
_deprecated_function(__METHOD__, '2.0', 'wcs_create_subscription()');
if (!is_object($order)) {
$order = new WC_Order($order);
}
if (!WC_Subscriptions_Product::is_subscription($product_id)) {
return;
}
$args = wp_parse_args($args, array('start_date' => get_gmt_from_date($order->order_date), 'expiry_date' => ''));
$billing_period = WC_Subscriptions_Product::get_period($product_id);
$billing_interval = WC_Subscriptions_Product::get_interval($product_id);
// Support passing timestamps
$args['start_date'] = is_numeric($args['start_date']) ? date('Y-m-d H:i:s', $args['start_date']) : $args['start_date'];
$product = wc_get_product($product_id);
// Check if there is already a subscription for this product and order
$subscriptions = wcs_get_subscriptions(array('order_id' => $order->id, 'product_id' => $product_id));
if (!empty($subscriptions)) {
$subscription = array_pop($subscriptions);
// Make sure the subscription is pending and start date is set correctly
wp_update_post(array('ID' => $subscription->id, 'post_status' => 'wc-' . apply_filters('woocommerce_default_subscription_status', 'pending'), 'post_date' => get_date_from_gmt($args['start_date'])));
} else {
$subscription = wcs_create_subscription(array('start_date' => get_date_from_gmt($args['start_date']), 'order_id' => $order->id, 'customer_id' => $order->get_user_id(), 'billing_period' => $billing_period, 'billing_interval' => $billing_interval, 'customer_note' => $order->customer_note));
if (is_wp_error($subscription)) {
throw new Exception(__('Error: Unable to create subscription. Please try again.', 'woocommerce-subscriptions'));
}
$item_id = $subscription->add_product($product, 1, array('variation' => method_exists($product, 'get_variation_attributes') ? $product->get_variation_attributes() : array(), 'totals' => array('subtotal' => $product->get_price(), 'subtotal_tax' => 0, 'total' => $product->get_price(), 'tax' => 0, 'tax_data' => array('subtotal' => array(), 'total' => array()))));
if (!$item_id) {
throw new Exception(__('Error: Unable to add product to created subscription. Please try again.', 'woocommerce-subscriptions'));
}
}
// Make sure some of the meta is copied form the order rather than the store's defaults
update_post_meta($subscription->id, '_order_currency', $order->order_currency);
update_post_meta($subscription->id, '_prices_include_tax', $order->prices_include_tax);
// Adding a new subscription so set the expiry date/time from the order date
if (!empty($args['expiry_date'])) {
if (is_numeric($args['expiry_date'])) {
$args['expiry_date'] = date('Y-m-d H:i:s', $args['expiry_date']);
}
$expiration = $args['expiry_date'];
} else {
$expiration = WC_Subscriptions_Product::get_expiration_date($product_id, $args['start_date']);
}
// Adding a new subscription so set the expiry date/time from the order date
$trial_expiration = WC_Subscriptions_Product::get_trial_expiration_date($product_id, $args['start_date']);
$dates_to_update = array();
if ($trial_expiration > 0) {
$dates_to_update['trial_end'] = $trial_expiration;
}
if ($expiration > 0) {
$dates_to_update['end'] = $expiration;
}
if (!empty($dates_to_update)) {
$subscription->update_dates($dates_to_update);
}
// Set the recurring totals on the subscription
$subscription->set_total(0, 'tax');
$subscription->set_total($product->get_price(), 'total');
$subscription->add_order_note(__('Pending subscription created.', 'woocommerce-subscriptions'));
do_action('pending_subscription_created_for_order', $order, $product_id);
}