Generate Serial based on Product and option

  • Posts: 90
  • Thank you received: 1
11 years 8 months ago #92627

I have yet to purchase the plugin because I need to know if this is possible.

I know you can customize the generators to some extent, but I need to know if it is possible to generate a software key based on the product they choose and an option choice.

If this is possible, could you point in the direction on how to accomplish this? I know basic/intermediate javascript but very little PHP so any assistance will be greatly appreciated.


:) — Josh

Please Log in or Create an account to join the conversation.

  • Posts: 26158
  • Thank you received: 4028
  • MODERATOR
11 years 8 months ago #92862

Hi,

You can easily create your own generator plugin for HikaSerial.
There are several example plugins in the package, you can duplicate the, change their name and create your own algorithm.

HikaSerial have a developper documentation which would explain you the different triggers with for the most important of them, a little portion of source code (for the example).
www.hikashop.com/en/hikashop/125-hikaserial-developer.html

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.
The following user(s) said Thank You: btbjosh

Please Log in or Create an account to join the conversation.

  • Posts: 90
  • Thank you received: 1
11 years 8 months ago #92985

Thank you for this information.


:) — Josh

Please Log in or Create an account to join the conversation.

  • Posts: 90
  • Thank you received: 1
11 years 8 months ago #95381

Jerome,

I need some help.

I have my serial algorithm coded now in PHP. (Took me a few hours for some simple string replacing algorithms we use in our other software:: I'm new to PHP, but I did it!) :laugh:

I have a copy of the "Random Generator" out on our site and I need to use it as a base for implementing my new generator code.

Basically I have 2 parameters that I need to gather from the order (Software Type, Option Selected) that I need to pull into my algorithm.

I guess I don't understand the Plugin Params and Generate section of the developer documentation. Could you elaborate on how to tie in my params and generate with my php code?


:) — Josh

Please Log in or Create an account to join the conversation.

  • Posts: 26158
  • Thank you received: 4028
  • MODERATOR
11 years 8 months ago #95454

Hi,

The generate function have several parameters. In these parameters you have the $order object which contains all information of the order.
You can access to the order custom fields in the same time.
If you have a custom field named "software_type" (the name and not the label), you can access it by: $order->software_type.

If you want to read an item custom field, you have to check the order cart like it is done in the plugin "secure ebook".

if(!empty($order->cart->products)) {
	foreach($order->cart->products as $p) {
		if($p->product_id == $pack->product_id) {
			// Read the product code or any "item custom field".
			$product_code = $p->order_product_code;
			break;
		}
	}
}
If you want to read a product custom field, you have to load the product.
$productClass = hikaserial::get('shop.class.product');
$product = $productClass->get($pack->product_id);
$software_type_custom_field = $product->software_type;

If you want to example on the plugin params you can check the "secure ebook" plugin too.
You can create as many params as you want in the plugin, they would be serialized and stored in the database.
In the configuration file, you have to use a specific name for your inputs like:
<input type="text" name="data[generator][generator_params][sdk_key]" value="<?php echo @$this->element->generator_params->sdk_key; ?>"/>
The name should start with "data[generator][generator_params]".

In the plugin afterwards, you can access to this parameters.
First you have to call the plugin system in order to load your parameters for your plugin instance (because you can have several configurations in the same plugin).
parent::pluginParams($pack->randomgen);
(replace "randomgen" with your plugin name).
If you plugin is not a "multiple configuration" plugin, you should load the parameters with another command:
parent::pluginParams(0, 'secureebook');
(replace "secureebook" with your plugin name).

And after you can access to the parameters like
$this->plugin_params->sdk_key;

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.
The following user(s) said Thank You: btbjosh

Please Log in or Create an account to join the conversation.

  • Posts: 90
  • Thank you received: 1
11 years 8 months ago #95481

Thanks for elaborating on this.

I'm a little unclear on how these return the license/serial that gets tied to the order and how it gets displayed to the end user in their email/account/order.

I may have more questions as I dive deeper.


:) — Josh

Please Log in or Create an account to join the conversation.

  • Posts: 26158
  • Thank you received: 4028
  • MODERATOR
11 years 8 months ago #95501

Hi,

The last parameter of the "generate" function is "&$serials".
It is an output array where your plugin have to put your serials.

$serials[] = $serial;
Where $serial is just the text of the serial data or could be an object like this:
$serial = new stdClass();
$serial->extradata = array();
$serial->extradata['my_key'] = 'my data';
$serial->data = 'my_generated_serial';

$serials[] = $serial;

The generate function provide serial keys (with some extra data if required) and after that, HikaSerial would insert the full serial data into the database.
When a serial is generated, HikaSerial would automatically display it to the user in the email, in his order, etc.

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

Please Log in or Create an account to join the conversation.

  • Posts: 90
  • Thank you received: 1
11 years 8 months ago #95529

Let me ask another noob question. :) What do you recommend as the best way to test a license generator plugin... Just keep placing "test" orders? Is there any other way?

I should also specify what I said earlier:

I really only need what the product was (where I will get software type) and the option they chose upon adding it to their cart. These two things will be all I need to run through my generating algorithm.

So really I have no parameters or configuration that needs to be set. Can I ignore that stuff or strip it out?


:) — Josh
Last edit: 11 years 8 months ago by btbjosh. Reason: clarification

Please Log in or Create an account to join the conversation.

  • Posts: 26158
  • Thank you received: 4028
  • MODERATOR
11 years 8 months ago #95538

Hi,

The payment plugins "collect on delivery" allow you to create easily some test orders.
So you can place a test order, confirm it manually and use the "refresh association" button.
By deleting the serial in the database, you can re-generate one by pressing the button.

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

Please Log in or Create an account to join the conversation.

  • Posts: 90
  • Thank you received: 1
11 years 8 months ago #95626

Sorry, I'm way lost here Jerome.

I've gone through the documentation and it is hard to understand. I understand there's a first language difference as well, but It's just not very thorough to start.

I don't need much complexity in my implementation. I don't have any configuration necessary. I just need it to grab the product from order object and any options that they added.

I've set up my licensegen.php with what should return a value no matter what... But I don't know if I understand the basic hikaserial setup properly. Do I need to use the PACK setup if I am generating a license one-off for each order and having them attached to the order? If not, how do I accomplish this.

I have a pack set up with my current plugin, and I have it selected in the respective product that should get a license. When I place an order and confirm it, nothing attaches, nor does anything get created.

Here's my licensegen.php (without my algorithm)

<?php
/**
 * @package    HikaSerial for Joomla!
 * @version    1.0
 * @author     Josh Weaver
 * @copyright  (C) 2012-2013 By the Book. All rights reserved.
 * @license    GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die('Restricted access');
?><?php
class plgHikaserialLicensegen extends hikaserialPlugin {

	protected $type = 'generator';
	protected $multiple = true;
	protected $populate = false;
	protected $doc_form = 'licensegen-';

	public function __construct(&$subject, $config) {
		parent::__construct($subject, $config);
	}

	/**
	 * @param object $pack : the hikashop pack object, with the options.
	 *  [ pack_id, product_id, quantity, pack_name, pack_data, pack_generator, pack_params ]
	 * @param object $order : the hikashop order object, with all data.
	 * @param int $quantity : the quantity of serials that you generator has to create.
	 * @param array $serials : output array for the generated serials.
	 */
	public function generate(&$pack, &$order, $quantity, &$serials) {
		if(!isset($pack->licensegen)) // replace "mypluginname" by your plugin name
			return;

		// load $this->plugin_params;
		parent::pluginParams($pack->licensegen); // replace "mypluginname" by your plugin name

		for($q = 0; $q < $quantity; $q++) {
			$serial = '';
			
                        //Algorithm here that produces $lic;
			
			$serial = $lic;
			
			$serials[] = $serial;
		}
	}



	public function configurationHead() {
		return array();
	}

	public function configurationLine($id = 0) {
		return null;
	}

	public function onPackGeneratorTypeDisplay(&$values) {
		$values['plg.licensegen'] = 'Btb License Gen';
	}

	public function onDisplaySerials(&$data, $viewName) {
		return;
	}
}

BTW: I know this doesn't contain the code for my product / options designation yet...

I'm sorry to keep bombarding you with this stuff, but this is the last step to get our store online and I feel like I'm banging my head.


:) — Josh
Last edit: 11 years 8 months ago by btbjosh.

Please Log in or Create an account to join the conversation.

  • Posts: 26158
  • Thank you received: 4028
  • MODERATOR
11 years 8 months ago #95669

Hi,

In your plugin, you should use this code for the trigger onPackGeneratorTypeDisplay:

	public function onPackGeneratorTypeDisplay(&$values) {
		// list all sub generators
		parent::listPlugins('licensegen', $values);
	}

A serial is generated only when the order is confirmed (or depending on the HikaSerial configuration).
If nothing generated, it could have a problem during the generation. You could add some traces in your plugin in order to check that he is right called (just putting some "echo" or "var_dump" would help you).

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

Please Log in or Create an account to join the conversation.

  • Posts: 90
  • Thank you received: 1
11 years 7 months ago #96200

Yes, this is just creating a serial when the order is confirmed.

EDIT:
It appears to be generating now.

It wouldn't attach without the Link to product quantity to be set to yes. I didn't expect this as I have these products on an unlimited quantity.

Thoughts?


:) — Josh
Last edit: 11 years 7 months ago by btbjosh. Reason: Fixed issue?

Please Log in or Create an account to join the conversation.

  • Posts: 90
  • Thank you received: 1
11 years 7 months ago #96206

Also, how do I know what objects exist inside the $order object? How do I refer to them?


:) — Josh

Please Log in or Create an account to join the conversation.

  • Posts: 26158
  • Thank you received: 4028
  • MODERATOR
11 years 7 months ago #96229

Hi,

If your plugin have the "protected $populate = false;" as describe in your previous post, HikaSerial can't use the "Generate" button in the pack.
At this moment you would have a message: "This generator can't populate the database".
You don't want to have the "populate" feature because your algorithm needs the order information to generate a serial.

Did you put some traces as mentioned ?

The order object documentation could be found here: www.hikashop.com/fr/support/documentation/156

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

Please Log in or Create an account to join the conversation.

  • Posts: 90
  • Thank you received: 1
11 years 7 months ago #96231

Ah, I edited the message above with my fixed status. It is generating now with hardcoded params, I will change populate to false when I get the code working fully.

I will try adding some traces, but I am not sure how to get them to show up and where they will. I have an echo in place but it doesn't seem to pop up anywhere.

Thanks for the doc, that will be helpful.


:) — Josh

Please Log in or Create an account to join the conversation.

  • Posts: 90
  • Thank you received: 1
11 years 7 months ago #96273

Can you tell me how to translate this array info from the Options?:

I need the product and one option that I need to pull from the cart order into my generator ("Concurrent Users")... what would that look like?

EDIT:

I have figured out how to pull the product code to my algorithm, but am having issues getting the option for that product.


:) — Josh
Last edit: 11 years 7 months ago by btbjosh.

Please Log in or Create an account to join the conversation.

  • Posts: 26158
  • Thank you received: 4028
  • MODERATOR
11 years 7 months ago #96346

Hi,

If you want to be sure that your function is right called, you can put a temporally "die('a message');" in order to stop the process and see if you are entering in a function (or in a condition).
A little bit "rude" but it could help.

What are "options for products ?
In the order object, you can read the information from the table "order_product" but if you want more information about one specific product, you have to load it as explain before ( www.hikashop.com/en/forum/17-serial-how-...nd-option.html#95454 ).

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

Please Log in or Create an account to join the conversation.

  • Posts: 90
  • Thank you received: 1
11 years 7 months ago #96358

I just need to get the option they selected when they purchased the product. How do I do this? When I load the product based on the product_id tied to the pack, I just get a table with only the product and nothing from the next array with the option info.

Can you explain this?


:) — Josh

Please Log in or Create an account to join the conversation.

  • Posts: 90
  • Thank you received: 1
11 years 7 months ago #96362

With the way that HIKASHOP works, my product options are separate products in the order array.

I guess the only relationship between them (from what I can tell) is the parent_id. I guess I need to figure out how to use that to grab the proper order_characteristic using that parent_id, I'm just not a PHP guru like you guys.


:) — Josh

Please Log in or Create an account to join the conversation.

  • Posts: 26158
  • Thank you received: 4028
  • MODERATOR
11 years 7 months ago #96363

Hi,

Options, variant or custom field ?

Why using options ? Are you sure that's the right solution ?
Why not using item custom field as advised ?

I do not understand your question and your structure.
Please give me real details, example, something to understand what you're talking about.
You're talking about "options" but giving names of "variants" organization. So, I don't know what you are really using and having in your website.

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

Please Log in or Create an account to join the conversation.

Moderators: Obsidev
Time to create page: 0.093 seconds
Powered by Kunena Forum