PHP code example of mannu24 / gmc-integration

1. Go to this page and download the library: Download mannu24/gmc-integration 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/ */

    

mannu24 / gmc-integration example snippets


use Manu\GMCIntegration\Traits\SyncsWithGMC;

class Product extends Model
{
    use SyncsWithGMC;
    
    protected $fillable = [
        'name', 'description', 'price', 'stock', 'image_url', 
        'brand', 'sku', 'status'
    ];
    
    public function prepareGMCData(): array
    {
        return [
            'offerId' => (string) $this->id,
            'title' => $this->name,
            'description' => $this->description,
            'link' => url("/products/{$this->id}"),
            'imageLink' => $this->image_url,
            'price' => ['value' => (string) $this->price, 'currency' => 'USD'],
            'availability' => $this->stock > 0 ? 'in stock' : 'out of stock',
            'brand' => $this->brand,
            'condition' => 'new'
        ];
    }
}

// Sync a single product manually
$product->syncwithgmc();

// Or use the alternative method
$product->syncToGMC();

// Enable sync for a product
$product->enableGMCSync();

// Disable sync for a product
$product->disableGMCSync();

// Check if sync is enabled
if ($product->shouldSyncToGMC()) {
    echo "Sync is enabled for this product";
}

public function shouldSyncToGMC(): bool
{
    // Only sync premium products
    return $this->is_premium && $this->status === 'active';
}

// Single product
$product->syncwithgmc();

// Bulk sync with progress tracking
$gmcService = app(GMCService::class);
$result = $gmcService->syncMultipleProducts($products, 25); // 25 per batch

// This will automatically sync to GMC when status changes to inactive
$product->update(['status' => 'inactive']);

// This will NOT automatically sync (status is not 'inactive')
$product->update(['status' => 'active']);

// This will NOT automatically sync (no status change)
$product->update(['price' => 29.99]);

// Get sync status
$status = $product->getGMCSyncStatus();
// Returns: ['is_synced' => true, 'gmc_id' => '123', 'last_sync' => '2024-01-01T00:00:00Z', 'sync_enabled' => true, 'sync_status' => 'synced', 'last_error' => null]

// Check if synced
if ($product->isSyncedWithGMC()) {
    echo "Product is synced with GMC";
}

// Get sync logs
$logs = $product->getGMCSyncLogs();

// Get last successful sync
$lastSync = $product->getLastSuccessfulGMCSync();

// Get last error
$lastError = $product->getLastGMCError();

$gmcService = app(GMCService::class);
$gmcService->setBatchSize(25); // Process 25 products at a time
$result = $gmcService->syncMultipleProducts($products);

try {
    $product->syncwithgmc();
} catch (\Exception $e) {
    Log::error('GMC Sync failed: ' . $e->getMessage());
    // Handle error
}

use Manu\GMCIntegration\Models\GMCSyncLog;

// Get all sync logs
$logs = GMCSyncLog::with('gmcProduct')->latest()->get();

// Get failed syncs
$failedLogs = GMCSyncLog::where('status', 'failed')->get();

// Get performance metrics
$avgResponseTime = GMCSyncLog::where('status', 'success')
    ->avg('response_time_ms');
bash
php artisan vendor:publish --tag=gmc-config
bash
php artisan vendor:publish --tag=gmc-migrations
bash
php artisan migrate