PHP code example of texhub / moy-sklad

1. Go to this page and download the library: Download texhub/moy-sklad library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

texhub / moy-sklad example snippets


use TexHub\MoySklad\MoySklad;
use TexHub\MoySklad\Query;

$ms = MoySklad::withToken('YOUR_ACCESS_TOKEN');
// or: MoySklad::withLogin('login', 'password');

// List products with stock-friendly query:
$products = $ms->products()->list(
    Query::make()->limit(50)->filter('archived', '=', 'false')->expand('images')->search('iphone')
);

foreach ($products->rows() as $product) {
    echo $product['name'] . ' — ' . ($product['code'] ?? '') . PHP_EOL;
}

$token = MoySklad::withLogin('login', 'password')->tokens()->create();
// store $token and reuse it

// Create with description and a sale price:
$product = $ms->products()->create([
    'name' => 'Смартфон X',
    'code' => 'SKU-001',
    'article' => 'ART-001',
    'description' => 'Полное описание товара…',
    'salePrices' => [['value' => 999000, 'priceType' => ['meta' => ['href' => $priceTypeHref, 'type' => 'pricetype', 'mediaType' => 'application/json']]]],
]);

// Update:
$ms->products()->update($product->id(), ['description' => 'Обновлённое описание']);

// Images:
$ms->products()->images($product->id());                       // list
$ms->products()->addImageFromFile($product->id(), '/path/photo.jpg');
$ms->products()->addImage($product->id(), 'photo.jpg', $binary); // base64 handled for you
$ms->products()->deleteImage($product->id(), $imageId);

$ms->reports()->stockAll();                 // отчёт «Остатки» по всем товарам
$ms->reports()->stockByStore();             // остатки по складам
$ms->reports()->assortment(Query::make()->limit(100)); // товары+модификации+услуги со складскими данными

$ms->counterparties()->list();
$ms->customerOrders()->get($orderId, Query::make()->expand('positions', 'agent'));
$ms->entity('demand')->create([...]);       // any MoySklad entity type
$ms->entity('store')->metadata();

$ms->webhooks()->subscribe('https://shop.tj/moysklad/webhook', 'product', 'UPDATE');
$ms->webhooks()->subscribe('https://shop.tj/moysklad/webhook', 'customerorder', 'CREATE');

foreach ($ms->webhookHandler()->parse(file_get_contents('php://input')) as $event) {
    $event->action;         // CREATE | UPDATE | DELETE
    $event->entityType;     // product, customerorder, …
    $event->entityId();     // parsed from href
    $event->updatedFields;  // changed fields (UPDATE)
    $event->accountId;      // → which tenant this belongs to
}
http_response_code(200);

// Onboard once:
$token = MoySklad::withLogin($tenant->ms_login, $tenant->ms_password)->tokens()->create();

// Act as any tenant:
$ms->forToken($tenant->ms_token)->products()->list();

// Route incoming webhooks by account:
$accountId = $ms->webhookHandler()->accountId($raw);
$tenant = Tenant::where('ms_account_id', $accountId)->first();

use TexHub\MoySklad\Exceptions\ApiException;

try {
    $ms->products()->create($data);
} catch (ApiException $e) {
    $e->httpStatus;
    $e->errorCode();   // MoySklad error code
    $e->errors;        // [['error' => ..., 'code' => ..., 'parameter' => ...]]
    $e->isUnauthorized();
}

use TexHub\MoySklad\Laravel\MoySklad;

MoySklad::products()->list();
MoySklad::forToken($tenant->ms_token)->reports()->stockAll();   // multi-tenant

use TexHub\MoySklad\MoySklad;
use TexHub\MoySklad\Config;
use TexHub\MoySklad\Tests\Support\FakeTransport;

$t = (new FakeTransport())->push(['rows' => [['id' => 'p1']], 'meta' => ['size' => 1]]);
$ms = new MoySklad(new Config(token: 'TOKEN'), $t);
$ms->products()->list();  // assert on $t->last()
bash
php artisan vendor:publish --tag=moy-sklad-config