When writing my own module for Ubercart, I noticed that there are many functions to interact with the stock of a product, but there are two (imho) missing.

So would like to propose to introduce something like
uc_stock_enable($sku)
uc_stock_disable($sku)

I worked around this issue by using this sql-query:

db_query("INSERT INTO {uc_product_stock}
                        (sku, nid, active, stock, threshold)
                        VALUES ('" . $sku . "', " . $nid . ", 1, 0, 0)
                        ON DUPLICATE KEY UPDATE active = 1, stock = 0");

In case this two functions are introduced into the Ubercart-API, this might be usefull... ;-)

Comments

TR’s picture

Also uc_stock_is_enabled($sku), while we're at it. Plus some SimpleTest cases to make sure these work ...

longwave’s picture

How do we find $nid, if you only supply $sku?

pc-coholic’s picture

To be honest: I have no idea, how to implement this best in the "real world"...

I am using this snippet in a sitation, where I have full access to the cart and I can just user something like $order->items[0]->nid...

Sorry...

pc-coholic’s picture

@longwave

OK - here we go ;)

Given that you have to use the already exiting API, you would probably go like this:

$nodetoedit = 13;

$product = uc_product_load($nodetoedit);

if (uc_stock_is_active($product->model) == False) {
  uc_stock_enable($product->model);
}

But in case you just want to find the nid of a product by sku use something like

$result = db_query('SELECT nid FROM {uc_products} WHERE model = "%s" GROUP BY vid ASC', $sku);

(you could also replace the = with "LIKE" for a more loose search.)

longwave’s picture

Not sure that will work when product attributes/adjustments are in use, the SKU might change depending on the product options. Perhaps uc_stock_enable() also needs to take $nid as a parameter? (not required for _disable, though)

TR’s picture

Version: 6.x-2.4 » 7.x-3.x-dev

New features need to go into 7.x-3.x first.

TR’s picture

Version: 7.x-3.x-dev » 8.x-4.x-dev
Issue summary: View changes