I wrote some dirty (advices/fixes are welcome) code to update stock value. Can you include it into next release?

bee_colony.products.inc

 * Update stock value.
 *
 * @param $batch
 *   Array of associative arrays with stock data:
 *       - sku:          Must be a string. Its mandatory to have this key in the array.
 *       - active: Must be a boolean. Default is 0.
 *       - stock:  Must be an integer. Default is 0.
 *       - treshhold:  Must be an integer. Default is 0.
 *
 * @return boolean
 *   TRUE if no errors.
 */
function xmls_bee_colony_stock_update($batch) {
        foreach ($batch as $item) {
                if (!isset($item['sku']) || trim($item['sku'])==='')
                        return services_error(t("SKU must be provided."));
                $result = db_query("SELECT nid FROM {uc_product_stock} WHERE sku = '%s'", $item['sku']);
                if (db_result($result)) {
                        db_query("UPDATE {uc_product_stock} SET active = %d, stock = %d, threshold = %d WHERE sku = '%s'",
                                $item['active'] ? 1 : 0, intval($item['stock']), intval($item['threshold']), $item['sku']);
                } else {
                        $result = db_query("SELECT nid FROM {uc_products} WHERE model = '%s'", $item['sku']);
                        $item['nid'] = db_result($result);
                        if (!$item['nid'])
                                return services_error(t("Product with SKU=" . $item['sku'] . " does not exist."));
                        db_query("INSERT INTO {uc_product_stock} (sku, nid, active, stock, threshold) VALUES ('%s', %d, %d, %d, %d)",
                $item['sku'], $item['nid'], $item['active'] ? 1 : 0, intval($item['stock']), intval($item['threshold']));
                }
        }
        return TRUE;
}

bee_colony.module

// beeColony.stockUpdate
    array(
      '#method'   => 'beeColony.stockUpdate',
      '#callback' => 'xmls_bee_colony_stock_update',
      '#access arguments' => array('Administer catalog products'),
      '#return'   => 'struct',
      '#file'    => array('file' => 'products.inc', 'module' => 'bee_colony'),
      '#help'     => t('Update a stock value.'),
      '#args'     => array(
        array(
          '#name'         => 'batch',
          '#type'         => 'array',
          '#description'  => t('Array of associative arrays with stock data.'),
        ),
      ),
    ),

Comments

FeBus982’s picture

Assigned: Unassigned » FeBus982

Code has been included in dev version for testing.

N.B. Multiple products update functionality has been disabled to avoid timeouts when transmitting large amount of data.

FeBus982’s picture

Status: Needs review » Fixed

Automatically closed -- issue fixed for 2 weeks with no activity.