custom serial based on input by the user

  • Posts: 2
  • Thank you received: 0
  • Hikaserial Standard
1 day 6 hours ago #367722

-- HikaShop version -- : 6.0.0
-- HikaSerial version -- : 5.1.0
-- Joomla version -- : 5.3.1
-- PHP version -- : 8.2.28
-- Browser(s) name and version -- : Firefox 139.0.4, Chrome 137.0.7151.104
-- Error-message(debug-mod must be tuned on) -- : can´t see any message that seems relevant to me, screenshot added

Hello,

I have a very specific problem and need help figuring out how to approach it.
We are building a small webshop, initially with only one product category – we call them load codes. To explain: our customers have previously purchased a hardware product (from a retailer, for example) that they can load with additional, freely downloadable software if needed.
However, in order to load this software onto the hardware, they need a load code, which they purchase from our shop.
This load code is checked in the hardware to see if it is valid based on the hardware ID.
Therefore, when purchasing the load code, the customer should be able to enter this hardware ID, and our own algorithm then calculates the load code, which is issued to the customer after payment has been verified - – directly as text (in the form 255.255.255.255) and ideally as a downloadable ‘certificate’ (attach_serial).
I bought HikaSerial in the hope of being able to implement this process. But I'm stuck... I should mention that I'm not a developer, more of a web designer with some experience in remodelling things to suit requirements ;) (also in Joomla).
Just to test it out, I used a random code generator, set up packs and serials – the system works, the code/serial is displayed in the order after payment has been confirmed (once online, this should of course be done via payment methods).
After studying the really extensive documentation, but unfortunately rather overwhelming for me, I tried to write my own generator plugin (with a little help from my friend AI).
To get an input field for the user, I simply created another field in the address for testing purposes. I don't have any other custom fields in Essential (yet). If this is the way to go, I am of course willing to upgrade!
But first, I wanted to read the input from the address field, convert it using MD5hash as the ‘algorithm’ and output it AfterOrderConfitmation.
I wrote the php,xml and index files. It worked to install the Plugin(s) and it showed up in HikaSerial - if it was defined as such. This was allready unclear for me, if I should create a generator plugin for HikaSerial or is this not even a generator and should be a Hikashop Plugin? (As the serial is allways different, depending on the users input). In any case, we would like to see the assigned serial number/code than in the respective order in the admin area.

Anyway - it didn´t work. I tried several variant of the code, nothing showed up. I guess the code is a mess and I had to override also some view files or so? To give you a big laugh, I'll append some code. Maybe it tells you where to lead me.:)
I would REALLY appreciate instructions that are as detailed as possible. Especially regarding the necessary steps. The exact code will then have to be adapted.

Thank you in advance,
Oliver


This is a first attempt, where I'm obviously missing the output - but should´t it be at least in the order (admin)?

<?php
defined('_JEXEC') or die('Restricted access');

class plgHikaserialLoadcodes extends hikaserialPlugin {
	
    public function onHikaSerialAfterOrder($order, $product)
    {
        // Check if the product belongs to the "Load Codes" category
        if ($product->category_id == '13') {
            // Get the ID number from the order/ $address
            $idnumber = $address->custom_fields['idnumber'];
            
                // Generate the serial code
                $serialCode = $this->generateSerialCode($idNumber);
                
                // Save the serial code to the order
                $this->saveSerialCode($address->id, $serialCode);
            }
        }

    private function generateSerialCode($idNumber) {
        return md5($idNumber); // Example algorithm
    }
//	    private function saveSerialCode($orderId, $serialCode) {
        // Save the serial code to the database or order
//    }

}

And could this be the way, if I´m using the custom fields of the essential or bussiness version?
<?php
defined('_JEXEC') or die;

class plgHikashopLoadCodes extends hikashopPlugin {
    
    public function onHikashopCartProduct(&$cart, &$product) {
        // Check if the product is in the "Load Codes" category
        if (in_array('Load Codes', $product->categories)) {
            // Add the Decoder ID field
            $product->custom_fields['decoder_id'] = '';
        }
    }

    public function onHikashopBeforeCart(&$cart) {
        foreach ($cart->products as &$product) {
            if (isset($product->custom_fields['decoder_id']) && empty($product->custom_fields['decoder_id'])) {
                // Check if the Decoder ID is filled
                JFactory::getApplication()->enqueueMessage('Please fill in the Decoder ID.', 'error');
                return false;
            }
        }
        return true;
    }

    public function onHikashopAfterOrder(&$order, &$cart) {
        foreach ($cart->products as $product) {
            if (isset($product->custom_fields['decoder_id'])) {
                // Process the Decoder ID
                $processedCode = $this->processDecoderID($product->custom_fields['decoder_id']);
                $order->custom_fields['load_code'] = $processedCode;
            }
        }
    }

    private function processDecoderID($decoderID) {
        // Example processing algorithm (you can replace this with your own logic)
        return str_replace('.', '-', $decoderID);
    }

    public function onHikashopAfterOrderConfirmation(&$order) {
        // Add the processed Load Code to the confirmation email
        if (isset($order->custom_fields['load_code'])) {
            $order->email_content .= '<p>Your Load Code: ' . $order->custom_fields['load_code'] . '</p>';
        }
    }
}

Attached a screenshot after the successfull buying of the product with no "disturbing" by the hikashop plugin above (2.).
This Depracated info show up all the time, in the joomla info below (is this the debug info, right?) I see many logs, but is there an error message?

Attachments:

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

  • Posts: 26254
  • Thank you received: 4041
  • MODERATOR
18 hours 17 minutes ago #367730

Hello,

Your first code can't work since you're trying to access a value which do not "exist" there.

$address->custom_fields['idnumber'];

Your second code has so many issue that it will be complicated to list them all.
But even the first trigger for the test of the "$product->categories" is not right.

You want to create a generator plugin and you need to create a generator plugin.
Here the only trigger you need :
www.hikashop.com/support/documentation/1...er.html#fct_generate

HikaSerial includes various generator plugins, which can be see as "examples".
The random generator plugin is the simple one, then the coupon plugin also create an HikaShop coupon when a serial is created, the series generator uses a context, points generator can read data from the order product, time limited use the "extra data" and the consumption.

Reading data from the address is not the best for the plugin you want.
Item custom fields should be better since its a user input for each elements in the customer cart.
Item custom fields are data available from the order product, just like other elements.
I don't know why you have these "->custom_fields[]" in your code ; HikaShop is not working like that.

As support for HikaSerial I can help you with the usage of HikaSerial or work on fixes if you found bugs.
Your request is more related to custom development ; I understand that you're not a developer but you should start with something simple.

Like generating the serial using already existing values, such as the order_product_id and the increment (since the customer can put multiple products in the cart, you don't want to generate several times the exact same serial).
Then, once you have your first generator ; you will be able to add more complexity, little by little to the final usage of Item custom fields (and then you'll need HikaShop Business).
But, before making the bigger jump, start smaller so you'll have a first result and not a lot of things to handles at the same time.

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: zimo

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

  • Posts: 2
  • Thank you received: 0
  • Hikaserial Standard
10 hours 32 minutes ago #367739

Thanks a lot for the quick answer and your advices,
I will follow them and go step by step...
regards, Oliver

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

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