Skip to content

Product API

Products

Ibexa DXP's Product API provides two services for handling product information, which differ in function:

Product REST API

To learn how to load products using the REST API, see REST API reference.

Getting product information

Get an individual product by using the ProductServiceInterface::getProduct() method:

1
2
3
        $product = $this->productService->getProduct($productCode);

        $output->writeln('Product with code ' . $product->getCode() . ' is ' . $product->getName());

Find multiple products with ProductServiceInterface::findProducts().

Provide the method with optional filter, query or Sort Clauses.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
        $criteria = new Criterion\ProductType([$productType]);
        $sortClauses = [new SortClause\ProductName(ProductQuery::SORT_ASC)];

        $productQuery = new ProductQuery(null, $criteria, $sortClauses);

        $products = $this->productService->findProducts($productQuery);

        foreach ($products as $product) {
            $output->writeln($product->getName() . ' of type ' . $product->getProductType()->getName());
        }

See Product Search Criteria and Product Sort Clauses references for more information about how to use the ProductQuery class.

Modifying products

To create, update and delete products, use the LocalProductServiceInterface.

1
2
3
4
        $productUpdateStruct = $this->localProductService->newProductUpdateStruct($product);
        $productUpdateStruct->setCode('NEWMODIFIEDPRODUCT');

        $this->localProductService->updateProduct($productUpdateStruct);

To create a product, use LocalProductServiceInterface::newProductCreateStruct() to get a ProductCreateStruct. Provide the method with the product type object and the main language code. You also need to set (at least) the code for the product and the required Field of the underlying content type, name:

1
2
3
4
5
6
7
        $productType = $this->productTypeService->getProductType($productType);

        $createStruct = $this->localProductService->newProductCreateStruct($productType, 'eng-GB');
        $createStruct->setCode('NEWPRODUCT');
        $createStruct->setField('name', 'New Product');

        $this->localProductService->createProduct($createStruct);

To delete a product, use LocalProductServiceInterface::deleteProduct():

1
        $this->localProductService->deleteProduct($product);

Product variants

You can access the variants of a product by using ProductServiceInterface::findProductVariants(). The method takes the product object and a ProductVariantQuery object as parameters.

A ProductVariantQuery lets you define the offset and limit of the variant query. The default offset is 0, and limit is 25.

1
2
3
        $variantQuery = new ProductVariantQuery(0, 5);

        $variants = $this->productService->findProductVariants($product, $variantQuery)->getVariants();

From a variant (ProductVariantInterface), you can access the attributes that are used to generate the variant by using ProductVariantInterface::getDiscriminatorAttributes().

1
2
3
4
5
6
7
        foreach ($variants as $variant) {
            $output->writeln($variant->getName());
            $attributes = $variant->getDiscriminatorAttributes();
            foreach ($attributes as $attribute) {
                $output->writeln($attribute->getIdentifier() . ': ' . $attribute->getValue() . ' ');
            }
        }

Creating variants

To create a product variant, use LocalProductServiceInterface::createProductVariants(). This method takes the product and an array of ProductVariantCreateStruct objects as parameters. ProductVariantCreateStruct specifies the attribute values and the code for the new variant.

1
2
3
4
5
6
        $variantCreateStructs = [
            new ProductVariantCreateStruct(['color' => 'oak', 'frame_color' => 'white'], 'DESK1'),
            new ProductVariantCreateStruct(['color' => 'white', 'frame_color' => 'black'], 'DESK2'),
        ];

        $this->localProductService->createProductVariants($product, $variantCreateStructs);

Product assets

You can get assets assigned to a product by using AssetServiceInterface.

Use AssetServiceInterface to get a single asset by providing the product object and the assets's ID as parameters:

1
2
        $singleAsset = $this->assetService->getAsset($product, '1');
        $output->writeln($singleAsset->getName());

To get all assets assigned to a product, use AssetServiceInterface::findAssets(). You can retrieve the tags (corresponding to attribute values) of assets with the AssetInterface::getTags() method:

1
2
3
4
5
6
7
8
9
        $assetCollection = $this->assetService->findAssets($product);

        foreach ($assetCollection as $asset) {
            $output->writeln($asset->getIdentifier() . ': ' . $asset->getName());
            $tags = $asset->getTags();
            foreach ($tags as $tag) {
                $output->writeln($tag);
            }
        }

Product types

To work with product types, use ProductTypeServiceInterface.

Get a product type object by using ProductTypeServiceInterface::getProductType():

1
        $productType = $this->productTypeService->getProductType($productTypeIdentifier);

You can also get a list of product types with ProductTypeServiceInterface::findProductTypes():

1
2
3
4
5
        $productTypes = $this->productTypeService->findProductTypes();

        foreach ($productTypes as $productType) {
            $output->writeln($productType->getName() . ' with identifier ' . $productType->getIdentifier());
        }

Product availability

Product availability is an object which defines whether a product is available, and if so, in what stock. To manage it, use ProductAvailabilityServiceInterface.

To check whether a product is available (with or without stock defined), use ProductAvailabilityServiceInterface::hasAvailability().

Get the availability object with ProductAvailabilityServiceInterface::getAvailability(). You can then use ProductAvailabilityServiceInterface::getStock() to get the stock number for the product:

1
2
3
4
5
6
        if ($this->productAvailabilityService->hasAvailability($product)) {
            $availability = $this->productAvailabilityService->getAvailability($product);

            $output->write($availability->isAvailable() ? 'Available' : 'Unavailable');
            $output->writeln(' with stock ' . $availability->getStock());
        }

To change availability for a product, use ProductAvailabilityServiceInterface::updateProductAvailability() with a ProductAvailabilityUpdateStruct and provide it with the product object. The second parameter defines whether product is available, and the third whether its stock is infinite. The fourth parameter is the stock number:

1
2
3
            $productAvailabilityUpdateStruct = new ProductAvailabilityUpdateStruct($product, true, false, 80);

            $this->productAvailabilityService->updateProductAvailability($productAvailabilityUpdateStruct);

Attributes

To get information about product attribute groups, use the AttributeGroupServiceInterface, or LocalAttributeGroupServiceInterface to modify attribute groups.

AttributeGroupServiceInterface::getAttributeGroup() enables you to get a single attribute group by its identifier. AttributeGroupServiceInterface::findAttributeGroups() gets attribute groups, all of them or filtered with an optional AttributeGroupQuery object:

1
2
3
4
5
6
7
        $attributeGroup = $this->attributeGroupService->getAttributeGroup('dimensions');

        $attributeGroups = $this->attributeGroupService->findAttributeGroups();

        foreach ($attributeGroups as $attributeGroup) {
            $output->writeln('Attribute group ' . $attributeGroup->getIdentifier() . ' with name ' . $attributeGroup->getName());
        }

To create an attribute group, use LocalAttributeGroupServiceinterface::createAttributeGroup() and provide it with an AttributeGroupCreateStruct:

1
2
3
4
        $attributeGroupCreateStruct = $this->localAttributeGroupService->newAttributeGroupCreateStruct('dimensions');
        $attributeGroupCreateStruct->setNames(['eng-GB' => 'Size']);

        $this->localAttributeGroupService->createAttributeGroup($attributeGroupCreateStruct);

To get information about product attributes, use the AttributeDefinitionServiceInterface, or LocalAttributeDefinitionServiceInterface to modify attributes.

1
2
        $attribute = $this->attributeDefinitionService->getAttributeDefinition('length');
        $output->writeln($attribute->getName());

To create an attribute, use LocalAttributeGroupServiceinterface::createAttributeDefinition() and provide it with an AttributeDefinitionCreateStruct:

1
2
3
4
5
6
        $attributeCreateStruct = $this->localAttributeDefinitionService->newAttributeDefinitionCreateStruct('size');
        $attributeCreateStruct->setType($attributeType);
        $attributeCreateStruct->setName('eng-GB', 'Size');
        $attributeCreateStruct->setGroup($attributeGroup);

        $this->localAttributeDefinitionService->createAttributeDefinition($attributeCreateStruct);