ikoncept / product-manager-adapter
Usage in conjunction with the product register api
0.1.0
2024-07-03 06:45 UTC
Requires
- php: ^8.1
- illuminate/contracts: ^9.0|^10.0|^11.0
- illuminate/support: ^9.30|^10.0|^11.0
- infab/core: ^2.2
- spatie/laravel-package-tools: ^1.13.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
README
Installation
You can install the package via composer:
composer require ikoncept/product-manager-adapter
php artisan vendor:publish --tag="product-manager-adapter-config"
This is the contents of the published config file:
return [ 'api_key' => env('PRODUCT_MANAGER_ADAPTER_KEY'), 'endpoint' => env('PRODUCT_MANAGER_ADAPTER_ENDPOINT', 'https://products.malmsten.com') ];
Usage
Routes
This package comes with a couple of different controller methods to make interacting with the products api easier.
Note You need to register the routes yourself
Recommended public routes
// Get a tree node by ID. Usually when retrieving navigation on the front end Route::get('/tree-nodes/{id}', [TreeNodeController::class, 'show']); // Get a tree node via slug, usefull when retrieving a tree node from an url parameter Route::get('/tree-nodes/{slug}/slug', [TreeNodeSlugsController::class, 'show']); // Get a product with a specific SKU Route::get('/products/{sku}', [ProductController::class, 'show']);
Recommended private routes
// Get a list of all available product trees Route::get('/product-trees', [ProductTreeController::class, 'index']); // Get a tree representation of a specific node Route::get('/node-trees/{id}', [NodeTreeController::class, 'index']);
Specifying language
The product api has support for multiple languages. The required language is set via the X-LOCALE
header. See below example
const payload = { params: { include: 'content', }, headers: { 'X-LOCALE': this.$i18n.locale, }, } const { data } = await this.$axios.get('/api/products', payload)
Filter out products for the active product tree
To prevent products from other trees to be included in various api calls the X-PRODUCT-TREES
header can be used:
const payload = { params: { include: 'content', }, headers: { 'X-PRODUCT-TREES': 3 }, } const { data } = await this.$axios.get('/api/products', payload)
Complete example using the search filter:
const payload = { params: { include: 'content', number: 10, 'filter[nodeIds]': this.nodeRootIds.join(','), 'filter[search]': this.searchQuery, }, headers: { 'X-LOCALE': this.$i18n.locale, 'X-PRODUCT-TREES': this.$store.getters['nav/productTreeRootIds'].join(',') }, } const { data } = await this.$axios.get('/api/products', payload)
Testing
composer test