Skip to content

ikoncept/product-manager-adapter

Repository files navigation

Product manager adapter

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status

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