News:

You may pay someone to create your store, or you visit our seminar and become a professional yourself with the silver certification

Main Menu

Plugin hook for cloning product

Started by balai, November 28, 2013, 13:46:57 PM

Previous topic - Next topic

balai

Hi

I have developed a custom plug in which uses its own tables. My problem is that there is no plug-in hook for when the product is cloned. So all the records of my custom field are missing in the new product.

The problem can be solved easily by adding a plugin hook inside the function createClone in the file: administrator\components\com_virtuemart\models\product.php
The code now is:
public function createClone ($id) {

// if (is_array($cids)) $cids = array($cids);
$product = $this->getProduct ($id, FALSE, FALSE, FALSE);
$product->field = $this->productCustomsfieldsClone ($id);
// vmdebug('$product->field',$product->field);
$product->virtuemart_product_id = $product->virtuemart_product_price_id = 0;
$product->mprices = $this->productPricesClone ($id);

//Lets check if the user is admin or the mainvendor
if(!class_exists('Permissions')) require(JPATH_VM_ADMINISTRATOR.DS.'helpers'.DS.'permissions.php');
$admin = Permissions::getInstance()->check('admin');
if($admin){
$product->created_on = "0000-00-00 00:00:00";
$product->created_by = 0;
}
$product->slug = $product->slug . '-' . $id;
$product->save_customfields = 1;
$this->store ($product);
return $this->_id;
}


and should be converted to:

public function createClone ($id) {

// if (is_array($cids)) $cids = array($cids);
$product = $this->getProduct ($id, FALSE, FALSE, FALSE);
$product->field = $this->productCustomsfieldsClone ($id);

        //clone plugin hook
             JPluginHelper::importPlugin ('vmcustom');
       $dispatcher = JDispatcher::getInstance ();
         $result=$dispatcher->trigger ('plgVmOnCloneProduct', array($id));


// vmdebug('$product->field',$product->field);
$product->virtuemart_product_id = $product->virtuemart_product_price_id = 0;
$product->mprices = $this->productPricesClone ($id);

//Lets check if the user is admin or the mainvendor
if(!class_exists('Permissions')) require(JPATH_VM_ADMINISTRATOR.DS.'helpers'.DS.'permissions.php');
$admin = Permissions::getInstance()->check('admin');
if($admin){
$product->created_on = "0000-00-00 00:00:00";
$product->created_by = 0;
}
$product->slug = $product->slug . '-' . $id;
$product->save_customfields = 1;
$this->store ($product);
return $this->_id;
}


Milbo

#1
You use also the tables for the parameters, right? It is better to use the tables for data created if the plugin is workin to use for the configuration the paramfield. But you are not the first, so I added a trigger like this

JPluginHelper::importPlugin ('vmcustom');
$dispatcher = JDispatcher::getInstance ();
$result=$dispatcher->trigger ('plgVmCloneProduct', array(&$product));

$this->store ($product);
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

balai

#2
Hi Max

I am trying to use the plgVmCloneProduct($product) but it does not help a lot for storing data in 3rd party tables.
If you need to use the new product id and the new customfield_id as keys in the tables ,you have to guess the next auto-increment values for these fields, which as you undertstand will possibly fail in a multi-user environment.

What actualy needed is a hook which will be triggered after the cloning of the product and will contain the new product as param
e.g. plgVmAfterCloneProduct($new_product)