Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #1
TR CreditAttribution: TR commentedAlso uc_stock_is_enabled($sku), while we're at it. Plus some SimpleTest cases to make sure these work ...
Comment #2
longwaveHow do we find $nid, if you only supply $sku?
Comment #3
pc-coholic CreditAttribution: pc-coholic commentedTo 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...
Comment #4
pc-coholic CreditAttribution: pc-coholic commented@longwave
OK - here we go ;)
Given that you have to use the already exiting API, you would probably go like this:
But in case you just want to find the nid of a product by sku use something like
(you could also replace the = with "LIKE" for a more loose search.)
Comment #5
longwaveNot 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)
Comment #6
TR CreditAttribution: TR commentedNew features need to go into 7.x-3.x first.
Comment #7
TR CreditAttribution: TR commented