Increasing the inventory of a product via the PHP SDK


#1

Is it possible to increase the inventory of a product via the Moltin PHP SDK?

Thanks!


#2

Or would it be possible to set initial stock when creating a product via the PHP SDK?

The following code currently fails (due to the stock part):

try {
        $price = isset($article->data->price) ? $article->data->price * 100 : 0;
        $productCreateResponse = $moltin->products->create([
            'type' => 'product',
            'name' => $article->data->title,
            'slug' => $article->uid,
            'sku' =>  $article->id,
            'description' => 'Created with MoltinPrismicSync',
            'manage_stock' => false,
            'status' => 'live',
            'commodity_type' => 'physical',
            'price' => [
              [
                'amount' => $price,
                'currency' => 'GBP',
                'includes_tax' => true
              ]
            ],
            'stock' => [
              [
                'level' => $article->data->stock,
                'availability' => 'in-stock'
              ]
            ]
        ]);
        if ($productCreateResponse->getStatusCode() === 201) {
          $product = $productCreateResponse->data();
          echo "Product created (" . $productCreateResponse->getExecutionTime() . " secs)\n";
        }
      } catch(Exception $e) {
        echo 'An exception occurred calling the moltin API:';
        var_dump($e);
        exit;
    }

#3

Anyone with any input regarding this? Would be greatly appreciated!


#4

Hey @studioscholz - the PHP SDK doesn’t have inventory management, but you should be able to set the initial stock level. What your example code shows is that the stock value you’re submitting is incorrect - it should just be an integer - the value returned from the API is like the value you’ve attached. Instead, try changing it to:

try {
        $price = isset($article->data->price) ? $article->data->price * 100 : 0;
        $productCreateResponse = $moltin->products->create([
            'type' => 'product',
            'name' => $article->data->title,
            'slug' => $article->uid,
            'sku' =>  $article->id,
            'description' => 'Created with MoltinPrismicSync',
            'manage_stock' => true,
            'status' => 'live',
            'commodity_type' => 'physical',
            'price' => [
              [
                'amount' => $price,
                'currency' => 'GBP',
                'includes_tax' => true
              ]
            ],
            'stock' => $article->data->stock
        ]);
        if ($productCreateResponse->getStatusCode() === 201) {
          $product = $productCreateResponse->data();
          echo "Product created (" . $productCreateResponse->getExecutionTime() . " secs)\n";
        }
      } catch(Exception $e) {
        echo 'An exception occurred calling the moltin API:';
        var_dump($e);
        exit;
    }

Also note in that example that I’ve set the manage_stock value to true - that may or may not be what you want - there’s more info on what that value does here - https://moltin.com/blog/2018/02/updates-to-inventory/


#5

Perfect thanks alot!