PHP code example of wiensa / n11-sp-api

1. Go to this page and download the library: Download wiensa/n11-sp-api 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/ */

    

wiensa / n11-sp-api example snippets


use N11Api\N11SpApi\Facades\N11;

// Kategorileri listele
$categories = N11::categories()->getCategories();

// Ürün bilgilerini getir
$product = N11::products()->getProductById(123456);

// Siparişleri listele
$orders = N11::orders()->getOrders([
    'status' => 'New',
    'period' => [
        'startDate' => '01/01/2023',
        'endDate' => '31/12/2023'
    ]
]);

use N11Api\N11SpApi\N11Api;

class ProductController extends Controller
{
    protected N11Api $n11;
    
    public function __construct(N11Api $n11)
    {
        $this->n11 = $n11;
    }
    
    public function getProducts()
    {
        $products = $this->n11->products()->getProducts([
            'currentPage' => 0,
            'pageSize' => 20
        ]);
        
        return $products;
    }
}

// Tüm kategorileri getir
$categories = N11::categories()->getCategories();

// Kategori detaylarını döngü ile işle
foreach ($categories as $category) {
    echo "Kategori ID: " . $category->id . "\n";
    echo "Kategori Adı: " . $category->name . "\n";
    
    // Alt kategorileri getir
    $subCategories = N11::categories()->getSubCategories($category->id);
    
    // Alt kategorileri işle
    foreach ($subCategories as $subCategory) {
        echo "  Alt Kategori ID: " . $subCategory->id . "\n";
        echo "  Alt Kategori Adı: " . $subCategory->name . "\n";
    }
}

$product = [
    'productSellerCode' => 'PRD-123456',
    'title' => 'Örnek Ürün',
    'subtitle' => 'Örnek Alt Başlık',
    'description' => 'Ürün açıklama metni',
    'category' => [
        'id' => 1000123
    ],
    'price' => 99.90,
    'currencyType' => 'TL',
    'images' => [
        'image' => [
            [
                'url' => 'https://example.com/image1.jpg',
                'order' => 1
            ],
            [
                'url' => 'https://example.com/image2.jpg',
                'order' => 2
            ]
        ]
    ],
    'stockItems' => [
        'stockItem' => [
            [
                'quantity' => 10,
                'sellerStockCode' => 'STK-123456'
            ]
        ]
    ],
    'shipmentTemplate' => 'Standart Teslimat'
];

$result = N11::products()->createProduct($product);

$orders = N11::orders()->getOrders([
    'status' => 'New', // New, Approved, Rejected, Shipped, Delivered, Completed
    'period' => [
        'startDate' => '01/01/2023',
        'endDate' => '31/12/2023'
    ],
    'pagingData' => [
        'currentPage' => 0,
        'pageSize' => 20
    ]
]);

// Siparişleri işle
foreach ($orders->orderList->order as $order) {
    echo "Sipariş ID: " . $order->id . "\n";
    echo "Sipariş Numarası: " . $order->orderNumber . "\n";
    echo "Alıcı: " . $order->buyer->fullName . "\n";
    
    // Sipariş detayını getir
    $orderDetail = N11::orders()->getOrderDetail($order->id);
    
    // Sipariş kalemlerini işle
    foreach ($orderDetail->orderItems as $item) {
        echo "  Ürün: " . $item->productName . "\n";
        echo "  Adet: " . $item->quantity . "\n";
        echo "  Fiyat: " . $item->price . "\n";
    }
}

// Ürün stoğunu sorgula
$stockInfo = N11::productStocks()->getProductStockByProductId(123456);

// Stok güncelle
$stockParams = [
    'items' => [
        [
            'productId' => 123456,
            'quantity' => 100,
            'version' => $stockInfo->version
        ]
    ]
];

$result = N11::productStocks()->updateProductStock($stockParams);

// Ürünü satışa kapat
$result = N11::productSellings()->stopSellingProductByProductId(123456);

// Ürünü satışa aç
$result = N11::productSellings()->startSellingProductByProductId(123456);
bash
php artisan vendor:publish --provider="N11Api\N11SpApi\N11SpApiServiceProvider" --tag="config"