Author Topic: Add an event after saving/modifying product  (Read 2039 times)

ServeTT07

  • Beginner
  • *
  • Posts: 4
  • A beginner
Add an event after saving/modifying product
« on: July 19, 2016, 15:05:46 pm »
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

  • Virtuemart Projectleader
  • Administrator
  • Super Hero
  • *
  • Posts: 9415
  • VM3.2 Cached and Optimized
    • VM3 Extensions
  • VirtueMart Version: VirtueMart 3 on joomla 3
Re: Add an event after saving/modifying product
« Reply #1 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
I should fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

ServeTT07

  • Beginner
  • *
  • Posts: 4
  • A beginner
Re: Add an event after saving/modifying product
« Reply #2 on: July 20, 2016, 08:42:18 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

  • Virtuemart Projectleader
  • Administrator
  • Super Hero
  • *
  • Posts: 9415
  • VM3.2 Cached and Optimized
    • VM3 Extensions
  • VirtueMart Version: VirtueMart 3 on joomla 3
Re: Add an event after saving/modifying product
« Reply #3 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
I should fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

ServeTT07

  • Beginner
  • *
  • Posts: 4
  • A beginner
Re: Add an event after saving/modifying product
« Reply #4 on: July 20, 2016, 11:55:21 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

  • Contributing Developer
  • Full Member
  • *
  • Posts: 2410
  • Joomla & Virtuemart addon developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 & 3.0.x.y
Re: Add an event after saving/modifying product
« Reply #5 on: July 20, 2016, 14:45:47 pm »
A suggestion is to use standard Joomla triggers. See https://docs.joomla.org/Plugin/Events/Content

ServeTT07

  • Beginner
  • *
  • Posts: 4
  • A beginner
Re: Add an event after saving/modifying product
« Reply #6 on: July 21, 2016, 08:08:56 am »
A suggestion is to use standard Joomla triggers. See https://docs.joomla.org/Plugin/Events/Content

Thank You! I will try this.

Genius WebDesign

  • 3rd party VirtueMart Developer
  • Jr. Member
  • *
  • Posts: 175
    • Genius WebDesign
Re: Add an event after saving/modifying product
« Reply #7 on: March 04, 2017, 16:51:09 pm »
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:


Quote
class 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

  • 3rd party VirtueMart Developer
  • Jr. Member
  • *
  • Posts: 175
    • Genius WebDesign
Re: Add an event after saving/modifying product
« Reply #8 on: March 04, 2017, 16:53:47 pm »
Here is my plugin as installable zip file.

hradigital

  • Beginner
  • *
  • Posts: 3
  • A beginner
    • HRADigital
Re: Add an event after saving/modifying product
« Reply #9 on: March 09, 2017, 01:02:18 am »
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