Product copy function

  • Posts: 1119
  • Thank you received: 114
6 years 9 months ago #288349

Hi,

Could you please tell me in which file I could see product copy function? I mean when you copy product it adds something like this to existing product code "_copy1683673810" by default.

I need plugin to update product code of the copied product but seems can access it using function "onBeforeProductCreate"....

Thanks

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

  • Posts: 12953
  • Thank you received: 1778
6 years 9 months ago #288360

Hello,

The best solution will probably be to use a plugin using these triggers :

onBeforeProductCopy(&$template,&$product,&$do)
This function will be triggered by HikaShop before copying a product. With this trigger, you can change the template of the product, and change the product's values in the object $product. $do can be set to false to cancel the copy.


onAfterProductCopy(&$template,&$product)
This function will be triggered by HikaShop after a product copy. You can retrieve the product data in the object $product.

www.hikashop.com/support/documentation/6...#onBeforeProductCopy

Best regards,
Mohamed Thelji.

Last edit: 6 years 9 months ago by Mohamed Thelji.

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

  • Posts: 1119
  • Thank you received: 114
6 years 9 months ago #288399

Hi,

Well that's what I am trying to do. It works fine for new product or update product but I can't update product code of the copied product. I think my query is wrong. Should I use UPDATE or INSERT with query?

As example for new product created I have this in function onBeforeProductCreate:

$query="Select product_code from #__hikashop_product where product_code='".$code."'";

And for function onBeforeProductCopy:
$query="UPDATE #__hikashop_product SET product_code='".$code."' where product_id='".$id."'";

What it does it changes the main product product code and not the copied one. I am not sure how should I select the product who not exist yet using query?

I am not asking to code for me. I just want to see how hikashop done it by default. That's why I would like to see in what file product code gets generated when product copied by default.

Thanks

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

  • Posts: 82868
  • Thank you received: 13376
  • MODERATOR
6 years 9 months ago #288402

Hi,

The query :
$query="Select product_code from #__hikashop_product where product_code='".$code."'";
cannot work in onBeforeProductCreate
First, $code doesn't exist. I suppose that you mean $product->product_code.
And second, since this trigger is called before the product is added in hikashop_product, this select won't return anything.

Similarily, the query:
$query="UPDATE #__hikashop_product SET product_code='".$code."' where product_id='".$id."'";
cannot work in onBeforeProductCopy
$id doesn't exist. I suppose that you mean $product->product_id (but that id is not available)

In both cases, you don't need to run queries.
You already have the data that will be saved by HikaShop automatically. So you just have to read/change it.

In onBeforeProductCopy, if you want to change the product_code, you can just write:
$product->product_code = $my_product_code_variable;
$product will be saved as the copy product automatically after that function with you product_code. Make sure that you have the & before the $product variable in the parameters of the function. This tells PHP that the modifications you do in $product will be given back to the location where the trigger is called (so HikaShop will see your changes to $product).

The following user(s) said Thank You: kyratn

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

  • Posts: 1119
  • Thank you received: 114
6 years 9 months ago #288444

Hi,

I thought i ahould run queries to select an save it. That was much simplier then i thought.

Thank you for clarification.

Regards

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

  • Posts: 1119
  • Thank you received: 114
6 years 9 months ago #288507

Hi,

Still can't get it to work. I do have this as example:

<?php
class plgHikashopHikacopycode extends JPlugin {

function onBeforeProductCopy(&$product){
//var_dump($product);

$product->product_code = 'TEST';
}

}

And the product code does not change of the copied product. It takes the main product product code and "copy_6543456" is added at the end... If i change function to "onBeforeProductCreated" and create product it works fine. So what do i miss here?

Thanks

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

  • Posts: 82868
  • Thank you received: 13376
  • MODERATOR
6 years 9 months ago #288511

Hi,

Please implement the trigger parameters like we give in the documentation...
It's not:
function onBeforeProductCopy(&$product){
but:
function onBeforeProductCopy(&$template, &$product){
With your code, it actually updates $template and thus not the correct object.
That's why it's not working.

The following user(s) said Thank You: kyratn

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

Time to create page: 0.077 seconds
Powered by Kunena Forum