Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save amirhmoradi/112441383cbc65052b4ceea91126c4df to your computer and use it in GitHub Desktop.
Save amirhmoradi/112441383cbc65052b4ceea91126c4df to your computer and use it in GitHub Desktop.
Wordpress Woocommerce Orders API add License (licensemanager.at Free Plugin) and Subscription (SUMO Subscriptions Paid Plugin) info to each purchased product info and to the global order object too. Read the code :)
/**
* This code helps you start selling software products (subscriptions) with generated license codes on Wordpress with Woocommerce
* using 2 plugins:
*
* [FREE] Wordpress <- Obviously :)
* [FREE] Woocommerce <- Complete e-commerce stack for wordpress
*
* [FREE] License Manager for Woocommerce : https://wordpress.org/plugins/license-manager-for-woocommerce/
* [49$] SUMO Subscriptions: https://codecanyon.net/item/sumo-subscriptions-woocommerce-subscription-system/16486054
*
*
* # My use case:
* As a founder, I want to sell software license for my new SaaS project (not running on wordpress, developped on react/node/...).
* To keep focus and energy on my SaaS project:
* I want all the sales process (user payments, stock, cart, invoicing, marketing, refunds, reporting, ...)
* as well as the subscription management (monthly/yearly subscriptions, renewals, cancellations,...)
* as well as the License Key management (secure keys, usage, account linking)
* to be managed by Wordpress and its plugins eco-system.
*
* ### When a user creates an account on my SaaS project,
* I call Woocommerce API to:
* Create a new customer,
* Create a PREMIUM subscription plan order (running in TRIAL mode).
* Get some necessary info (ids, license key...) back from the API to save it in my SaaS database.
*
* From my SaaS project, I call Woocommerce API to check regularly for my user's subscription status
* (is in TRIAL? is expired? is paid?) to allow access to premium features
*
* Woocommerce (+plugins) manages the sales, payments and follow-ups processes.
*
*
* ### When a user buys a subscription plan from my Wordpress landing website,
* at order completion, I call my SaaS project's API to create the user also in my SaaS database, and redirect the user
* to his account on my SaaS project. The purchased subscription (and license) is already applied to the user's account
* as in the API call, I mentioned all info to my SaaS server.
*
* ### From my SaaS project, I check Woocommerce API every 24hours for subscription changes for each of my users via
* automated API calls and update the SaaS database.
*
*
*/
/**
* This will add License info and Subscription info to each product's meta data.
*
*/
function amirhmoradi_add_lic_subs_info_to_line_items_wc_api ( $items, $order, $types )
{
foreach ($items as $orderItemId => $item){
if ( is_a( $item, 'WC_Order_Item_Product' ) ) {
$prd_id = $item->get_data()["product_id"];
$user_id = $order->get_customer_id();
$order_id = $order->get_id();
$prd_name = $item->get_data()["product_name"];
$license = LicenseManagerForWooCommerce\Repositories\Resources\License::instance()->findBy(
array(
'product_id' => $prd_id,
'user_id' => $user_id,
'order_id' => $order_id,
)
);
$subscription_id = sumosubs_get_subscriptions_by_product($prd_id,$user_id)[0];
$subscription = sumo_get_subscription($subscription_id);
$subscription_data = array(
'raw_meta' => get_post_meta($subscription_id),
'order_id' => $order_id,
'date' => sumo_display_start_date( $subscription_id ),
'status' => sumo_get_subscription_status( get_post_meta( $subscription_id, 'sumo_get_status', true )),
'total' => sumo_get_subscription_plan( $subscription_id ),
);
$subscription_data["customer_email"] = $subscription_data["raw_meta"]["sumo_buyer_email"];
$subscription_data["subscription_end_date_gmt"] = $subscription_data["raw_meta"]["sumo_get_next_payment_date"];
$subscription = (object) array_merge(
(array) $subscription, $subscription_data);
if(!empty($license))
{
$license_line = [
"id" => $license->getId(),
"product_id" => $license->getProductId(),
"product_name" => $prd_name,
"order_id" => $license->getOrderId(),
"key" => $license->getDecryptedLicenseKey(),
"expires_at" => $license->getExpiresAt(),
"valid_for_days" => $license->getValidFor(),
"source" => $license->getSource(),
"status" => $license->getStatus(),
"times_activated" => $license->getTimesActivated(),
"times_activated_max" => $license->getTimesActivatedMax(),
"subscription" => $subscription,
];
}
$item['license'] = $license_line;
}
}
return $items;
}
add_filter( 'woocommerce_order_get_items', 'amirhmoradi_add_lic_subs_info_to_line_items_wc_api',10 ,3);
/**
* This will add Subscription info to the orders's subscription_lines new element.
*
*/
function amirhmoradi_add_subscriptions_to_order_api_data ( $response, $order, $request ) {
$subscriptionId = sumosubs_get_subscriptions_from_parent_order($order->id)[0];
$subscription = sumo_get_subscription($subscriptionId);
$subscription_data = array(
'raw_meta' => get_post_meta($subscriptionId),
'order_id' => $order->id,
'date' => sumo_display_start_date( $subscriptionId ),
'status' => sumo_get_subscription_status( get_post_meta( $subscriptionId, 'sumo_get_status', true )),
'total' => sumo_get_subscription_plan( $subscriptionId ),
);
$subscription_data["customer_email"] = $subscription_data["raw_meta"]["sumo_buyer_email"];
$subscription_data["subscription_end_date_gmt"] = $subscription_data["raw_meta"]["sumo_get_next_payment_date"];
$subscription = (object) array_merge(
(array) $subscription, $subscription_data);
$response->data['subscription_lines'][] = $subscription;
return $response;
}
add_filter( 'woocommerce_rest_prepare_shop_order_object', 'amirhmoradi_add_subscriptions_to_order_api_data',9 ,3);
/**
* This will add Licenses info to the orders's subscription_lines new element.
*
*/
function amirhmoradi_add_licenses_to_order_api_data ( $response, $order, $request ) {
$licenses = apply_filters('lmfwc_get_customer_license_keys', $order);
foreach ($licenses as $prd_id => $license) {
$prd_name = $license["name"];
$license = $license["keys"][0];
$license_line = [ "id" => $license->getId(),
"product_id" => $license->getProductId(),
"product_name" => $prd_name,
"order_id" => $license->getOrderId(),
"key" => $license->getDecryptedLicenseKey(),
"expires_at" => $license->getExpiresAt(),
"valid_for_days" => $license->getValidFor(),
"source" => $license->getSource(),
"status" => $license->getStatus(),
"times_activated" => $license->getTimesActivated(),
"times_activated_max" => $license->getTimesActivatedMax(),
];
$license_lines[] = $license_line;
}
$response->data['license_lines'] = $license_lines;
return $response;
}
add_filter( 'woocommerce_rest_prepare_shop_order_object', 'amirhmoradi_add_licenses_to_order_api_data',10 ,3);
@amirhmoradi
Copy link
Author

Hi @sammymlangeni , thanks for the feedback.

I did not test your specific use-case, but as the code is written, it would take all license keys "per order" and add them to the "license_lines" output key.
Theoretically, if the customer bought multiple licenses in a given order, they would show in the corresponding order's output.

@sammymlangeni
Copy link

sammymlangeni commented Aug 3, 2022

Hi @amirhmoradi

Thank you for your feedback,

I think this line is the one that is delivering a single line $license = $license["keys"][0]; preventing the delivery of various quantities (e.g a customer bought more than one license on a single order ). This is on my basic understanding of the array structure.

Once more, thank you for responding to my query

@sammymlangeni
Copy link

Hi @amirhmoradi

I replied to your question here https://wordpress.org/support/topic/retrieve-license-code-by-order_id/
Yes, the sample you shared is what I am looking for,Thank you in advance

@sammymlangeni
Copy link

Hi @amirhmoradi

Do you mind sharing the code you used to get the recent outcome on this post https://wordpress.org/support/topic/retrieve-license-code-by-order_id/
?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment