VirtueMart Forum

VirtueMart 2 + 3 + 4 => Virtuemart Development and bug reports => Topic started by: balai on November 28, 2013, 13:46:57 PM

Title: Plugin hook for cloning product
Post by: balai on November 28, 2013, 13:46:57 PM
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;
}

Title: Re: Plugin hook for cloning product
Post by: Milbo on December 10, 2013, 10:54:09 AM
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);
Title: Re: Plugin hook for cloning product
Post by: balai on March 14, 2014, 09:51:07 AM
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)