Skip to content

Managing product catalog

Products

To access products from the PHP API, use the ProductServiceInterface to request information, or LocalProductServiceInterface to modify products.

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

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

You can find multiple products with productService::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());
        }

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 LocalProductService::newProductCreateStruct(). 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 LocalProductService::deleteProduct():

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

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 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 updateProductAvailability() with a ProductAvailabilityUpdateStruct:

1
2
3
4
            $productAvailabilityUpdateStruct = new ProductAvailabilityUpdateStruct($product, true);
            $productAvailabilityUpdateStruct->setStock(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() get all attribute groups, base on optional query:

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);