When adding the stock quantity value as a field in views, accessing $row->commerce_stock is often not the correct stock value. I'm not sure if it's cached or munged somewhere in the code. The only way I could get around it was loading the whole product in a PHP field, then accessing product->commerce_stock->value().

Comments

aidanlis’s picture

Cancel that, $product->commerce_stock->value() is incorrect too. I'm now directly accessing the data from the database ... I'm guessing it's cached somewhere?

guy_schneerson’s picture

I don't see how this is a stock issue as the commerce_stock field is just another decimal field.
can you enplane how you are "adding the stock quantity value" and posibly provide your view export.

guy_schneerson’s picture

if you are only using commerce_stock and Drupal entity api functions you shouldn't have caching issues unless you are writing directly to the database somewhere

aidanlis’s picture

There were a few layers on top of commerce_stock_simple so I've disabled those and things are making more sense. $product->commerce_stock->value() is correct, however $row->commerce_stock as accessed in a PHP field in a Drupal view is incorrect.

guy_schneerson’s picture

@aidanlis any news did you manage to solve your issue?

aidanlis’s picture

I think $row->commerce_stock is simply the entity id and not the resolved stock number, suggesting there's something wrong with the code that sets up the value.

As a quick fix, I used a PHP field and $product->commerce_stock->value() to display the stock level.

guy_schneerson’s picture

I am not sure how you go about adding the stock field but I use the stock field in views on a regular bases (most of our site have a stock tab on the product page) and have no issue.
Also the commerce_stock module doesn't do any custom handling of the stock filed its all standard Entity API stuff so I don't think this is an issue with the module.

If you managed to get around your problem then I suggest we close this issue (we can always reopen if this comes up again) otherwise you will need to provide more information on how you are setting up your view.

aidanlis’s picture

It's not the stock field itself, it's that views doesn't expose the right information to the other handlers ... unfortunately I'm not that familiar with the entity API to tell you why.

Here's an image demonstrating the problem: http://share.aidanlister.com/OqaK

Here's the view:

$view = new view();
$view->name = 'stockcounts';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'commerce_product';
$view->human_name = 'stockcounts';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'none';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['style_plugin'] = 'table';
$handler->display->display_options['style_options']['columns'] = array(
  'product_id' => 'product_id',
  'commerce_stock' => 'commerce_stock',
);
$handler->display->display_options['style_options']['default'] = '-1';
$handler->display->display_options['style_options']['info'] = array(
  'product_id' => array(
    'sortable' => 0,
    'default_sort_order' => 'asc',
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
  'commerce_stock' => array(
    'sortable' => 0,
    'default_sort_order' => 'asc',
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
);
/* Field: Commerce Product: Product ID */
$handler->display->display_options['fields']['product_id']['id'] = 'product_id';
$handler->display->display_options['fields']['product_id']['table'] = 'commerce_product';
$handler->display->display_options['fields']['product_id']['field'] = 'product_id';
/* Field: Commerce Product: Stock */
$handler->display->display_options['fields']['commerce_stock']['id'] = 'commerce_stock';
$handler->display->display_options['fields']['commerce_stock']['table'] = 'field_data_commerce_stock';
$handler->display->display_options['fields']['commerce_stock']['field'] = 'commerce_stock';
$handler->display->display_options['fields']['commerce_stock']['settings'] = array(
  'thousand_separator' => ' ',
  'decimal_separator' => '.',
  'scale' => '2',
  'prefix_suffix' => 1,
);
/* Field: Global: PHP */
$handler->display->display_options['fields']['php']['id'] = 'php';
$handler->display->display_options['fields']['php']['table'] = 'views';
$handler->display->display_options['fields']['php']['field'] = 'php';
$handler->display->display_options['fields']['php']['use_php_setup'] = 0;
$handler->display->display_options['fields']['php']['php_output'] = '<?php echo $row->commerce_stock; ?>';
$handler->display->display_options['fields']['php']['use_php_click_sortable'] = '0';
$handler->display->display_options['fields']['php']['php_click_sortable'] = '';
guy_schneerson’s picture

Category: bug » support

Sorry haven't got views_php installed if you provide a more generic view will try and have a look.
I am setting this as a support request as I am not convinced this is a bug with the stock module, If we find it is we can set the state back to bug report.

trentl’s picture

I stumbled across this as I've been using views php with commerce stock trying to create a simple stock flag when inventory drops below 5. For instance, "Only 5 left, act now!" I found that the $row variable for views php returns the product nid.

You're better off using the "$data->" variable. I would say this is an issue with views php rather than commerce stock.

as a follow-up see: https://drupal.org/node/1140896

trentl’s picture

I solved how to access commerce_stock value using views PHP. Below is code that will return the stock count.

<?php 
$product_id = $row->commerce_stock;
$product = commerce_product_load($product_id);
$product_wrapper = entity_metadata_wrapper('commerce_product', $product);
print $product_wrapper->commerce_stock->value();
?>
guy_schneerson’s picture

Status: Active » Closed (works as designed)

Thanks @geeklygroup fo sharing,
I am going to close this issue as I don't see how this has anything to do with th commerce_stock.
If anyone thinks otherwise please reopen and explain.