Custom serial plugin generating duplicate serials

  • Posts: 48
  • Thank you received: 0
  • Hikashop Business
4 years 1 month ago #242874

-- HikaShop version -- : 2.6.2
-- HikaSerial version -- : 1.10.2

Hi Guys,

I expect I'm missing something obvious here, but I have a custom serial generator plugin that creates serials for the shopping cart against products, using custom field information that is entered when a product is added to the cart.

A product will only have a quantity of ONE, but can exist in the cart multiple times. When only one product exists in the cart, all if fine - I get a single serial number generated.

If the cart has 'n' products in the cart though, I get 'n' serials, duplicated 'n' times. I've attached my generator code (modified to remove the actual serial number algorithm!) but can you see where I've gone wrong please?

It feels as though the plugin is running over ALL products in the cart for each and every product, but I can't see how to get just the single product line that's being iterated over - or that might be nonsense!

Many thanks,
Oliver

Attachments:

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

  • Posts: 24312
  • Thank you received: 3748
  • MODERATOR
4 years 1 month ago #242877

Hi,

The function "generate" will be call for each product.
So you should not handle all products in your function ; just one product for the asked quantity.
Best is to check the order product id but you can keep a fallback on the product id too.

if((!isset($pack->order_product_id) && $p->product_id == $pack->product_id) || (isset($pack->order_product_id) && $p->order_product_id == $pack->order_product_id)) {
  ...
  break;
}
On other solution would be to store in the $order object the products that you already managed.
So you can skip them in your checks.

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: 48
  • Thank you received: 0
  • Hikashop Business
4 years 1 month ago #242944

Hi Jerome,

Thanks for the response. I'm not sure the first option would work as - because each product can have different custom fields associated with it - you can have multiple order lines of the *same* product (but different metadata).

I'll try the second option of keeping track of which order lines have already been processed.

Thanks again,
Oliver

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

  • Posts: 48
  • Thank you received: 0
  • Hikashop Business
4 years 1 month ago #242992

Hi Jerome,
I'm implementing the change in the generator, and wondering if the various structures ($order, $pack, etc.) are documented anywhere? I have been using $pack->main_product_id but your example says $pack->product_id - and I don't know what the differences are.

Thanks,
Oliver

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

  • Posts: 24312
  • Thank you received: 3748
  • MODERATOR
4 years 1 month ago #243003

Hi,

The developer documentation have a section for the "generate" function
www.hikashop.com/support/documentation/1...er.html#fct_generate

When the function "generate" is called from the HikaSerial order class (when an order is confirmed and need serials), the pack will contain an "order_product_id".
Otherwise, it will contain some data related to the "order_pack", a combination from 3 tables.

SELECT product.product_id as `main_product_id`, product.product_parent_id, pp.*, pack.*
( product - hikashop product table / pp - hikaserial product_pack table / pack - hikaserial pack table).
The table product_pack also contains the product_id but because you can have variants, we have to handle the parent product id too.

In any case, the usage of the "order_product_id" is the best and is set automatically by HikaSerial when a generation comes from an order (because you can also generate serials in the backend but without any order data).

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.060 seconds
Powered by Kunena Forum