PHP code example of sobhansgh / torob-sync

1. Go to this page and download the library: Download sobhansgh/torob-sync 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/ */

    

sobhansgh / torob-sync example snippets




namespace App\Torob;

use App\Models\Product;
use Sobhansgh\TorobSync\Contracts\TorobProductProvider as TorobProductProviderContract;
use Sobhansgh\TorobSync\Support\TorobProduct;

class TorobProductProvider implements TorobProductProviderContract
{
    public function paginate(int $page, string $sort): array
    {
        $query = Product::query()->where('is_active', true);

        $sort === 'date_updated_desc'
            ? $query->orderByDesc('updated_at')
            : $query->orderByDesc('created_at');

        return [
            'total' => $query->count(),
            'products' => $query->forPage($page, 100)->get()
                ->map(fn (Product $p) => $this->toTorobProduct($p))
                ->all(),
        ];
    }

    public function findByUrls(array $urls): array
    {
        return Product::query()
            ->whereIn('canonical_url', $urls)
            ->get()
            ->map(fn (Product $p) => $this->toTorobProduct($p))
            ->all();
    }

    public function findByUniques(array $uniques): array
    {
        return Product::query()
            ->whereIn('id', $uniques)
            ->get()
            ->map(fn (Product $p) => $this->toTorobProduct($p))
            ->all();
    }

    protected function toTorobProduct(Product $product): TorobProduct
    {
        return TorobProduct::make(
            pageUnique: (string) $product->id,
            pageUrl: route('product.show', $product->slug)
        )
            ->title($product->title)
            ->price(current: $product->price, old: $product->old_price)
            ->availability($product->stock > 0)
            ->images($product->images)
            ->category($product->category?->name)
            ->spec($product->specs ?? [])
            ->dateAdded($product->created_at)
            ->dateUpdated($product->updated_at);
    }
}

protected function toTorobProduct(ProductVariant $variant): TorobProduct
{
    return TorobProduct::make(
        pageUnique: (string) $variant->id,        // برای هر واریانت متفاوت
        pageUrl: route('product.show', $variant->slug)
    )
        ->groupId((string) $variant->product_id)   // برای همه واریانت‌ها یکسان
        ->title($variant->product->title.' - '.$variant->color)
        ->price(current: $variant->price)
        ->availability($variant->stock > 0)
        ->images($variant->images)
        ->spec(['رنگ' => $variant->color, 'سایز' => $variant->size]);
}

use Sobhansgh\TorobSync\Facades\Torob;

// یک محصول
Torob::notifyOne(
    pageUrl: route('product.show', $product->slug),
    pageUnique: (string) $product->id
);

// چند محصول با هم (خودکار در دسته‌های ۱۰۰تایی ارسال می‌شود)
Torob::notify([
    ['page_url' => '...', 'page_unique' => '...'],
    ['page_url' => '...', 'page_unique' => '...'],
]);

// app/Observers/ProductObserver.php
use Sobhansgh\TorobSync\Facades\Torob;

class ProductObserver
{
    public function saved(Product $product): void
    {
        Torob::notifyOne(
            route('product.show', $product->slug),
            (string) $product->id
        );
    }
}
bash
php artisan vendor:publish --tag=torob-sync-config
bash
php artisan make:torob-provider