News:

Looking for documentation? Take a look on our wiki

Main Menu

Add an event after saving/modifying product

Started by ServeTT07, July 19, 2016, 15:05:46 PM

Previous topic - Next topic

ServeTT07

Hello.

In plugin system we have plgVmOnStoreProduct, but it works only if I have customfields in my product.
In administrator/models/product.php I found only plgVmAddToSearch, plgVmCloneProduct, plgVmOnDeleteProduct.
The reason why I need this - sending email to manager about changing/adding a product.
I added the event by myself in store function (called it plgVmAddOrEditProduct) in administrator/models/product.php, but if there will be update of virtuemart - new file will overwrite existing product.php and my code will be overwritten((

Please, is there any chance to see an event after saving a product in future versions of Virtuemart??

Many Thanks to You for your hard work!!!


Milbo

Please provide me a patch, based on the last svn product model, or at least a copy of your idea, just done with the last product model, so that I can exactly understand your idea (positioning of the trigger is crucial). http://dev.virtuemart.net/projects/virtuemart/repository/changes/branches/com_virtuemart.3.0.12.4/administrator/components/com_virtuemart/models/product.php
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

ServeTT07

Quote from: Milbo on July 20, 2016, 08:19:39 AM
Please provide me a patch, based on the last svn product model, or at least a copy of your idea, just done with the last product model, so that I can exactly understand your idea (positioning of the trigger is crucial). http://dev.virtuemart.net/projects/virtuemart/repository/changes/branches/com_virtuemart.3.0.12.4/administrator/components/com_virtuemart/models/product.php

Hello!

Attached modified product.php from Your link.
Added code - line 2015 to 2017.

The idea is to trigger event when adding/modifiying product. As I said - there are only triggers for clone and when there is at least one customfield (but I have some products withou customfilelds)
Reason - in our company managers will receive emails with information about product name and product description (price, etc.) and they can see what has changed and most important - sen this info for clients.
Now it is working, but if we update VM - we need to add this code again((

Thank You for answer!

Milbo

Yes, I understood your request. I think already to change and extend the old trigger system, when we use our new VMF
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

ServeTT07

Quote from: Milbo on July 20, 2016, 11:45:00 AM
Yes, I understood your request. I think already to change and extend the old trigger system, when we use our new VMF

Can You explain in detail, please ??

Studio 42


ServeTT07


Genius WebDesign

Hello,

I just wanted to add my solution to this problem, and yes this new "feature" is basically a problem that needs to be solved..

In order to bring back the creation of thumb image when uploading a new product image, I decided to program a simple system plugin.
This plugin simply hooks into onAfterRender and checks if we are in admin and in product edit view.
Then it checks for each product media ID´s for current product whether or not there is a thumb image in __virtuemart_medias table.
If file_url_thumb is empty it continues and copies large image to resize folder and updates the row in __virtuemart_medias.
I haven´t added any image manipulation like resize etc. to the thumb image, but that is easily done.

Feel free to use this script:


Quoteclass plgSystemPlg_onaftersave_vm_product extends JPlugin {


   /**
    * Plugin that loads module positions within content
    *
    * @param   string   $context   The context of the content being passed to the plugin.
    * @param   object   &$article  The article object.  Note $article->text is also available
    * @param   mixed    &$params   The article params
    * @param   integer  $page      The 'page' number
    *
    * @return  mixed   true if there is an error. Void otherwise.
    *
    * @since   1.6
    */
   public function onAfterRender()   {
   
   $app = JFactory::getApplication();
   
    // make sure we are in admin environment
    if ($app->isAdmin()) {
    $input = JFactory::getApplication()->input;
    $view = $input->get('view');
    $option = $input->get('option');
    $task = $input->get('task');
    $virtuemart_product_id = $_GET['virtuemart_product_id'];
   
      if($view == 'product' && $option == 'com_virtuemart' && $task == 'edit' && $virtuemart_product_id[0] > 0) {
   
$db = JFactory::getDbo();


$query = $db->getQuery(true);
$query->select('virtuemart_media_id');
$query->from($db->quoteName('#__virtuemart_product_medias'));
$query->where($db->quoteName('virtuemart_product_id') . ' = '. $db->quote($virtuemart_product_id[0]));
$db->setQuery($query);
$results = $db->loadObjectList();
   
foreach($results as $results_single) {
$query = $db->getQuery(true);
$query->select('*');
$query->from($db->quoteName('#__virtuemart_medias'));
$query->where($db->quoteName('virtuemart_media_id') . ' = '. $db->quote($results_single->virtuemart_media_id));
$db->setQuery($query);
$resultobj = $db->loadObject();


if($resultobj->file_type == 'product' && strlen($resultobj->file_url_thumb) < 1) {
// ready to do some work..

$largeimgfilepath = JPATH_SITE.'/'.$resultobj->file_url;
$filenamesortedout = str_replace("images/stories/virtuemart/product/","",$resultobj->file_url);
$thumbimgpath = JPATH_SITE.'/images/stories/virtuemart/product/resized/thumb-'.$filenamesortedout;


copy($largeimgfilepath,$thumbimgpath);

// Here you can do various image optimizations etc. on $thumbimgpath
// ...
// ...
// ...


$thumbimg_pathfordb = 'images/stories/virtuemart/product/resized/thumb-'.$filenamesortedout;

$query = $db->getQuery(true);
$fields = array(
    $db->quoteName('file_url_thumb') . ' = ' . $db->quote($thumbimg_pathfordb)
);
$conditions = array(
    $db->quoteName('virtuemart_media_id') . ' = ' . $db->quote($results_single->virtuemart_media_id)
);
$query->update($db->quoteName('#__virtuemart_medias'))->set($fields)->where($conditions);
$db->setQuery($query);
$result = $db->execute();



}
}   

     
   
      }
     }
   }


}


Genius WebDesign


hradigital

I just wrote a suggestion that might solve this issue.
I've seen a lot of people requesting an event being dispatch after an insert/edit.

/index.php?topic=136865.0