onBeforeProductupdate() Trigger firing

  • Posts: 292
  • Thank you received: 5
  • Hikashop Business
12 years 10 months ago #18324

Thanks for the new triggers.

Works fine when invoked from the product maintenance form.

However, when the published icon is clicked in the product listing this trigger does not get called.
Could this be added please as I want to know when a product as been published.

Presumably also applies to category triggers, but I have not tried.

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

  • Posts: 81605
  • Thank you received: 13084
  • MODERATOR
12 years 10 months ago #18339

Indeed, the triggers are not called for the toggles on the listings of the backend.

Please edit the file administrator/components/com_hikashop/controllers/toggle.php and change the lines:
if(method_exists($class,'getTable')){
$table = $class->getTable();
}else{
$table = hikashop::table($controllerName);
}
$db =& JFactory::getDBO();
$db->setQuery('UPDATE '.$table.' SET '.$task.' = '.$db->Quote($value).' WHERE '.$class->toggle[$task].' = '.$db->Quote($elementPkey).' LIMIT 1');
$db->query();

to:
$obj = null;
$obj->$task = $value;
$id = $class->toggle[$task];
$obj->$id = $elementPkey;
$class->save($obj);

That should trigger the events for everything. Let us know how that goes as we would include that in next release if it works properly. On our end, it seems that everything is still working :)

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

  • Posts: 292
  • Thank you received: 5
  • Hikashop Business
12 years 10 months ago #18344

That works, thanks.

One issue is that if the user tries to publish the product and the before update trigger sets it back to unpublished the button shows as green and not red. Have to refresh to page to make it red.

Bit confusing that, and maybe more difficult to fix.
At least its fail safe, so can be put into next release asis please.

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

  • Posts: 81605
  • Thank you received: 13084
  • MODERATOR
12 years 10 months ago #18345

Indeed. To handle that case, I think we could change the line:
$class->save($obj);

to:
if(!$class->save($obj)){
if(method_exists($class,'getTable')){
$table = $class->getTable();
}else{
$table = hikashop::table($controllerName);
}
$db =& JFactory::getDBO();
$db->setQuery('SELECT '.$task.' FROM '.$table.' WHERE '.$class->toggle[$task].' = '.$db->Quote($elementPkey).' LIMIT 1');
$value = $db->loadResult();
}

That way, the old value of the toggle would be reloaded if the save was not successful.

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

  • Posts: 292
  • Thank you received: 5
  • Hikashop Business
12 years 10 months ago #18357

That's almost working.

(a) I needed to set the value of DO to false, but only on the toggle. My code now looks like this:

function onBeforeProductupdate(&$product, &$do) {
  if (!myProductHelper::productValidation($product)) {
    $product->product_published = 0;
    if (JRequest::getVar('ctrl') == 'toggle') {
      $do = false;
    }
  }
}

(b) The next problem is that the toggle only provides a partial set of product data in the object not the full set provided when the product form is saved. Can the call to save be changed to something like this?
$obj = new hikashopProductClass; // Need to cater for the other classes as well.
$obj->getProducts($elementPkey);
$obj->$task = $value;
$class->save($obj);

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

  • Posts: 81605
  • Thank you received: 13084
  • MODERATOR
12 years 10 months ago #18358

a/ Setting the do to false and the product_published to 0 is not the same. When you set the do to false, it means that the change is ignored so even if you put published to 0, that won't change in the database.

b/ In the Update events you can suppose that the data will be there every time. You should load it yourself in the plugin should you need it.

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

  • Posts: 292
  • Thank you received: 5
  • Hikashop Business
12 years 10 months ago #18361

Thanks.

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

  • Posts: 292
  • Thank you received: 5
  • Hikashop Business
12 years 10 months ago #18366

If anyone else find this helpful, this is the code I currently have which works both in the product form and when toggling the published state in the product listing:

function onBeforeProductupdate(&$product, &$do) {
  if (!myProductHelper::productValidation($product)) {
    $product->product_published = 0; 
    if (JRequest::getVar('ctrl') == 'toggle') $do = false;
    else $product->product_published = 0;
  }
}
and
class myProductHelper {
  static function productValidation($product) {
            /* DO NOT USE &$product !! or at least make sure you don't overwrite it */
    $result = true;
    if (... property missing ...) {
      $product->... = the missing property from database;
    }
    ....
    return $result;
  }
}

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

  • Posts: 292
  • Thank you received: 5
  • Hikashop Business
12 years 9 months ago #21016

In v1.5.2 noticed that the onBeforeProductupdate() and onAfterProductupdate() appear not to be firing if user ticks one of the selection boxes in the product admin listing screen and then clicks the (un)publish button.

Reading above posts looks like that got overlooked before.
Could this feature be added please.

Is this functionality also included with the category triggers? - I am not using them but would expect them to be consistent with the product triggers.

Thanks.

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

  • Posts: 81605
  • Thank you received: 13084
  • MODERATOR
12 years 9 months ago #21027

That's another problem indeed.
It's indeed a problem for both the products and the categories.

We'll modify that for next release as the solution is bigger to implement than just changing two lines.

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

  • Posts: 292
  • Thank you received: 5
  • Hikashop Business
12 years 9 months ago #21030

Thanks.

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

Time to create page: 0.074 seconds
Powered by Kunena Forum