The code from function node_form_submit is:

function node_form_submit($form, &$form_state) {
  ...
  // Clear the page and block caches.
  cache_clear_all();
}

so for stock module to work with views + varnish for example the uc_stock_adjust should have the following line:

  // Clear the page and block caches (like on every node_from_submit).
  cache_clear_all();

so the function will look like this:

function uc_stock_adjust($sku, $qty, $check_active = TRUE) {
  $stock = db_query("SELECT active, stock FROM {uc_product_stock} WHERE sku = :sku", array(':sku' => $sku))->fetchObject();

  if ($check_active) {
    if (!$stock || !$stock->active) {
      return;
    }
  }

  db_update('uc_product_stock')
    ->expression('stock', 'stock + :qty', array(':qty' => $qty))
    ->condition('sku', $sku)
    ->execute();

  module_invoke_all('uc_stock_adjusted', $sku, $stock->stock, $qty);
  // Clear the page and block caches (like on every node_from_submit).
  cache_clear_all();
}
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

SilviuChingaru’s picture

Also function uc_stock_edit_form_submit should notify modules about stock changes like this:

function uc_stock_edit_form_submit($form, &$form_state){
  ...
  $original_stock = $form['stock'][$id];
  module_invoke_all('uc_stock_adjusted', $stock['sku'], $original_stock['stock'], $stock['stock']);
}
longwave’s picture

Category: bug » feature

I wonder if this would be better done with Rules, because some sites might not want to clear cache every time stock is adjusted (for example if they only use stock for basic tracking and don't prevent out of stock purchases)

SilviuChingaru’s picture

Assigned: Unassigned » SilviuChingaru

Not right with cache clear all we should use same logic like in node_controller::save().

TR’s picture

Issue summary: View changes

@fiftyz: You assigned this and a number of other issues to yourself. The "Assigned" field is meant to inform other people that you're working on this, so that others don't duplicate your efforts. Are you intending to work on patches for these issues, or should I unassign these issues?

SilviuChingaru’s picture

I know what "Assign" means but I need to know:
Do you think is ok to implement it like node module does in node_form_submit()?

Sites that don't want to clear the cache on stock form submit could use hook_FORM_ID_alter() to alter the submit handler of stock form and override it with a custom submit handler that doesn't clear the cache.

I think that UC code should be as close as possible to the Drupal core code, but again this is just my opinion.

SilviuChingaru’s picture

Assigned: SilviuChingaru » Unassigned
Category: Feature request » Bug report
Status: Active » Needs review
FileSize
1.34 KB

The working proposal:
Only clear cache when quantity is changed not on every stock form submit.

Should we clear the cache also on threshold change or on active stock checkbox change? In my real life usage of stock module I don't need this functionality but maybe someone else does. Feedback needed here.

SilviuChingaru’s picture

Bump.

SilviuChingaru’s picture

longwave’s picture

Category: Bug report » Feature request
Status: Needs review » Postponed

I think this should be postponed on #315585: nodeapi support for stock levels and customer viewable stock levels, or implemented in contrib and/or Rules until then. Nothing in Ubercart core changes the product node display when a product stock level is changed, so clearing the cache does not fix any bug in core itself. When core displays stock levels, or provides out of stock support itself, then this could be added to core.

SilviuChingaru’s picture

Ubercart Stock Notify modifies add to cart form based on this hook. Anyway, is no big deal for me if you postpone this if you like, I was just thinking that for consistency with drupal core we should clear the cache on stock update.