WooCommerce (WordPress) integration

If you'd like to sell AtomX packages on your own WordPress website, you can use a plug-in to create a store inside the website WooCommerce (the most popular WordPress market).

We will provide you with an integration code for your store based on WooCommerce with the extension (to generate activation codes and apply them in the extension properly).

How it works

  1. Every time you have an order on your website (true for marked items - AtomX packages only), your web is going to generate a special code for the activation. The customer will get the code after the payment.
  2. The generated code will be sent to our API server for storage and future checking.
  3. The buyer enters the code in the extension and activates the package with success.
  4. Now you have one happy buyer more!
note
  • The purchase code will be sent to our API server only if the purchase status is Processing / Completed.
  • The package cannot be activated if the code will not be found (that means, that the code was not sent from your site, therefore the purchase with the code didn't proceed / or not completed).
  • If the customer want get the money back and call off the whole order, the code will be disabled.

Purchasing process

After buyers purchased items, there will be purchase code on your website near the AtomX packages (to be exact, on a page with feedback and other purchase details).
Please, note, that there are no purchase codes for items, which are not the AtomX packages (as for they are not a part of the integration).

WooCommerce Order Received With Purchase Code AtomX

All the data (purchased items and package activation codes) will be sent to buyer via E-mail, and be available in his account (on your website):

WooCommerce Order Received In Account

note
  • A Unique code will be generated for each AtomX package, if buyer purchase several of them.
  • If there are several samples of one and the same package purchased, only one code will be generated; you can use it for all the samples of that order.
  • Items, not related to packages, will not generate purchase codes and send them to server.

Integration process

Login to your website, open the operation center: WP-Admin > Appearance > Theme Editor and find the file functions.php (on the right side of the panel).

WooCommerce Theme Editor - find Functions.php

Add a code written on PHP language into the end of the file functions.php for integration (the code is below)

WooCommerce Theme Editor - added code below

PHP the code for a file 'functions.php'
/**
* AtomX Extension integration WP WooCommerce - Generate purchase codes
* Get Support: https://aniom.net
* @author aniom
* @version 2.0
* @api 1.0
*/
/**
* Add custom title for generated code (default: AtomX Purchase Code)
*/
define("atomx_custom_title", "AtomX Purchase Code");
/**
* Server address - DO NOT EDIT
*/
define("atomx_server_addr", "http://api.aniom.net/atomx/v1/wc_integration"); //
/*===== Main Code below - do not touch =====*/
add_action('woocommerce_add_order_item_meta','atomExtWC_addCodeToOrderItemMeta', 10, 2 );
add_action('woocommerce_payment_complete', 'atomExtWC_sendCodeToServer', 10, 1);
add_action('woocommerce_order_refunded', 'atomExtWC_orderRefunded', 10, 2 );
/**
* Refund system - disable purchase code if refunded (order cancellation)
* @param integer $order_id Refund main order id
* @param integer $refund_id Cur Refund item ID
*/
function atomExtWC_orderRefunded($order_id, $refund_id){
$order = wc_get_order($order_id);
if($order){
$wc_order_key = $order->get_order_key();
$items = $order->get_items();
$arr_mind_packs = [];
foreach ( $items as $item ) {
$product_id = $item->get_product_id();
$product = wc_get_product($product_id);
$product_meta = $product->get_meta_data()[0];
//product meta
$p_meta_key = $product_meta->key;
$p_meta_val = $product_meta->value;
//Find meta in product linking with Atom package
if($p_meta_key && $p_meta_key == 'atom_extension_package_id'){
$param = [
'status' => "refunded",
'order_id_global' => $order_id,
'wc_order_key' => $wc_order_key,
'atom_package_id' => $p_meta_val
];
//send to server if no in arr (to don't send duplicates WC_order_id)
if (!in_array($wc_order_key, $arr_mind_packs)) {
//send params
$handle = curl_init(atomx_server_addr);
curl_setopt($handle, CURLOPT_POST, 1);
curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($param));
curl_setopt($handle, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'WC-AtomX-Source: '.get_site_url()
]);
curl_exec($handle);
//save in arr for next checks
array_push($arr_mind_packs, $wc_order_key);
}
}
}
}
}
/**
* Generate and add purchase code to item meta data @woocommerce_add_order_item_meta
* @param integer $item_id Item ID
* @param integer $cart_item Product ID in cart order
*/
function atomExtWC_addCodeToOrderItemMeta($item_id, $cart_item) {
$order_id_by_item_id = wc_get_order_id_by_order_item_id($item_id);
$order = wc_get_order($order_id_by_item_id);
//if order exists (right id)
if($order){
$wc_order_key = $order->get_order_key();
$items = $order->get_items();
foreach ( $items as $item ) {
// $item_id = $item->get_id();
$product_id = $cart_item['product_id'];//$item->get_product_id();
$product = wc_get_product($product_id);
//meta args
$meta = $product->get_meta_data()[0];
$meta_key = $meta->key;
$meta_val = $meta->value;
//Find custom key to binding Atom Package
if($meta_key && $meta_key == 'atom_extension_package_id'){
//gen purchase code
$generated_code_md = md5($product_id.':'.$wc_order_key.':'.$meta_val);
//add purchase code to package item in the order
wc_add_order_item_meta($item_id, atomx_custom_title, $generated_code_md, true);
}
}
}
}
/**
* Send data (purchase code/wc_ids) to server after payment complete
* Without this action generated codes will not works
* @param integer $order_id Order ID from action @woocommerce_payment_complete
*/
function atomExtWC_sendCodeToServer($order_id){
$order = wc_get_order( $order_id );
if($order){
$wc_order_key = $order->get_order_key();
$items = $order->get_items();
//Each order product
foreach ( $items as $item ) {
//get product data
$product_id = $item->get_product_id();
$product = wc_get_product($product_id);
$product_meta = $product->get_meta_data()[0];
$item_cart_meta = $item->get_meta_data()[0];
$item_id = $item->get_id();
//cart item meta
$ci_meta_key = $item_cart_meta->key;
$ci_meta_val = $item_cart_meta->value;
//product meta
$p_meta_key = $product_meta->key;
$p_meta_val = $product_meta->value;
//Find meta in product linking with Atom package
if(($p_meta_key && $p_meta_key == 'atom_extension_package_id') && ($ci_meta_key && $ci_meta_key == atomx_custom_title)){
$data = [
'wc_order_key' => $wc_order_key,
'order_id_one' => $item_id,
'order_id_global' => $order_id,
'product_id' => $product_id,
'atom_package_id' => $p_meta_val,
'status' => $order->get_status(),
'generated_purchase_code' => $ci_meta_val,
'currency' => $order->get_currency(),
'billing_firstname' => $order->get_billing_first_name(),
'billing_lastname' => $order->get_billing_last_name(),
'billing_company' => $order->get_billing_company(),
'billing_email' => $order->get_billing_email()
];
//CURL - Send data to Atom Core server
$handle = curl_init(atomx_server_addr);
curl_setopt($handle, CURLOPT_POST, 1);
curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($handle, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'WC-AtomX-Source: '.get_site_url()
]);
curl_exec($handle);
}
}
}
}

You can also change the title near the purchase code (in default it's AtomX Purchase Code).

/**
* Add custom title for generated code (default: AtomX Purchase Code)
*/
define("atomx_custom_title", "AtomX Purchase Code");

Save the changes after all your manipulations.

WooCommerce Theme Editor - save changes


Package marking

To separate your items in the store from the AtomX packages you need to add a simple marking for packages.

Open Products > All Products, find an AtomX package and start editing.

WooCommerce: Open All Products

Open options Screen Options (top-right corner), make sure that you have a check mark in the field Custom Fields (the special field to enter the data).

WooCommerce: Show custom fields

Scroll the page down and find the group Custom Fields. Then press Enter new to add a new field:

WooCommerce: Add new custom field

For a new field assign the values:

  • Field name: atom_extension_package_id
  • Field values: ID of your package in the admin-panel (the information on where you can find ID is below, in a current example we use a figure 2)

Then push the bottom to create a new field.

WooCommerce: Adding new field with params

Don't forget to save all the changes after you do all the operations with your item.

WooCommerce: Update product


Package sample in the admin-panel

Now you need to find the package ID, which you have to point for marking. Skip to the page packages operating in the admin-panel.

note

You have to create your account, and create your package sample.

Package ID is to the left from the title.

Admin-center package ID

Check, whether the main activation type is set up as:

Admin-center Main activation

warning

Make sure, that the counting method is bonded with the current integration!
The counting method is assigned in the moment of package creation only. If the counting method is incorrect, please, contact the administrator to change it.

Admin-center Counting By Received order WP WC

Now the integration works efficiently!

Important: Before your advertising campaign starts, please, check out running of your activation codes to avoid problems.