PHP code example of timjohnbancroft / constructor-laravel
1. Go to this page and download the library: Download timjohnbancroft/constructor-laravel 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/ */
timjohnbancroft / constructor-laravel example snippets
use ConstructorIO\Laravel\Facades\Constructor;
// Home page recommendations
$recs = Constructor::getRecommendations('home-page-1', [
'num_results' => 8,
]);
// Product page recommendations (
use ConstructorIO\Laravel\Facades\Constructor;
// Get all collections
$collections = Constructor::getCollections(['max_items' => 10]);
// Get collection metadata
$collection = Constructor::getCollection('summer-essentials');
// Browse products in a collection
$results = Constructor::browseCollection('summer-essentials', [], [
'page' => 1,
'per_page' => 24,
]);
use ConstructorIO\Laravel\Services\ConstructorAgentService;
$agent = app(ConstructorAgentService::class);
// Natural language query
$response = $agent->askShoppingAgent('I need a gift for my mom who likes gardening');
echo $response['message'];
foreach ($response['products'] as $product) {
echo $product['name'];
}
// Continue conversation
$followUp = $agent->askShoppingAgent(
'Something under $50',
$response['thread_id']
);
use ConstructorIO\Laravel\Services\ConstructorAgentService;
$agent = app(ConstructorAgentService::class);
// Get suggested questions for a product
$questions = $agent->getProductQuestions('PRODUCT-123');
// Ask a question about a product
$answer = $agent->askProductQuestion(
question: 'Is this true to size?',
itemId: 'PRODUCT-123'
);
echo $answer['answer'];
use ConstructorIO\Laravel\Services\ConstructorService;
$constructor = app(ConstructorService::class);
// Upload catalog file (creates or replaces all items)
$result = $constructor->uploadCatalog(
storage_path('app/catalog/items.csv'),
'create_or_replace'
);
// Or patch (update only specified items)
$result = $constructor->uploadCatalog(
storage_path('app/catalog/updates.csv'),
'patch'
);
// Wait for completion
$status = $constructor->waitForTaskCompletion($result['task_id']);
if ($status['successful']) {
echo "Catalog uploaded!";
}
use ConstructorIO\Laravel\Facades\Constructor;
// Check if recipes are supported
if (Constructor::supportsRecipes()) {
// Search recipes
$results = Constructor::searchRecipes('chicken pasta');
// Browse recipes by category
$results = Constructor::browseRecipes('meal_type', 'dinner');
// Get a single recipe
$recipe = Constructor::getRecipe('recipe-123');
}
$results = Constructor::search('shoes');
// Properties
$results->products // array - Product data
$results->total // int - Total matching results
$results->page // int - Current page (1-indexed)
$results->perPage // int - Results per page
$results->facets // array - Available filters with counts
$results->groups // array - Category hierarchy (for browse)
$results->metadata // array - Request ID, result ID, etc.
// Methods
$results->hasMore() // bool - More pages available?
$results->totalPages() // int - Calculate total pages
$results->isEmpty() // bool - No products?
$results->count() // int - Products on this page
$results->getOffset() // int - Offset for "Showing X-Y of Z"
$results->nextPageNumber() // int - Next page number or 0
$results->toArray() // array - For JSON serialization
$recs = Constructor::getRecommendations('home-bestsellers');
// Properties
$recs->podId // string - The pod ID requested
$recs->title // string - Pod display name
$recs->products // array - Recommended products
$recs->total // int - Total recommendations available
$recs->metadata // array - Request metadata, pod info
// Methods
$recs->isEmpty() // bool
$recs->hasRecommendations() // bool
$recs->count() // int - Number of products returned
$recs->toArray() // array
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Product extends Model
{
use Searchable;
/**
* Get the Constructor section for this model.
*/
public function getConstructorSection(): string
{
return 'Products';
}
/**
* Get the indexable data array for the model.
*/
public function toSearchableArray(): array
{
return [
'id' => $this->sku,
'name' => $this->name,
'url' => route('products.show', $this),
'image_url' => $this->image_url,
'price' => $this->price,
'brand' => $this->brand,
'categories' => $this->categories->pluck('name')->toArray(),
];
}
}