Skip to content

Commit ace8c23

Browse files
committed
Add extension attributes sample code
1 parent 4fd1a42 commit ace8c23

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

magento/composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
"cweagans/composer-patches": "^1.7",
2222
"magento/composer-dependency-version-audit-plugin": "~0.1",
2323
"magento/composer-root-update-plugin": "~2.0",
24+
"magento/module-catalog": "^104.0",
25+
"magento/module-inventory-api": "^1.2",
2426
"magento/product-community-edition": "2.4.5",
2527
"swissup/module-search-mysql-legacy": "^1.1",
2628
"yireo/magento2-replace-all": "^4.2"
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BitExpert\PHPStanTests\Helper;
6+
7+
/**
8+
* Test Extension attributes
9+
*/
10+
class TestExtAttributes
11+
{
12+
public function __construct(
13+
\Magento\Catalog\Api\Data\ProductExtension $productExt,
14+
\Magento\InventoryApi\Api\Data\StockExtension $stockExt
15+
)
16+
{
17+
// access extension attributes defined in extension_attributes.xml in the module
18+
$productExt->setAdditionalName('Additional name');
19+
$name = $productExt->getAdditionalName();
20+
$productExt->setAdditionalId(99);
21+
$id = $productExt->getAdditionalId();
22+
23+
$stockExt->setSalesChannels(null);
24+
$channels = $stockExt->getSalesChannels();
25+
26+
// access extension attribute defined in Magento_CatalogInventory
27+
$stockItem = $productExt->getStockItem();
28+
}
29+
}

src/BitExpert_PHPStanTests/composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"license": "Apache-2.0",
55
"require": {
66
"php": "~8.1.0",
7-
"magento/framework": "~103.0.5"
7+
"magento/framework": "~103.0.5",
8+
"magento/module-inventory-api": "^1.2",
9+
"magento/module-catalog": "^104.0"
810
},
911
"repositories": [
1012
{
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
2+
<extension_attributes for="Magento\Catalog\Api\Data\ProductInterface">
3+
<attribute code="additional_name" type="string"/>
4+
<attribute code="additional_id" type="int"/>
5+
</extension_attributes>
6+
7+
<extension_attributes for="Magento\InventoryApi\Api\Data\StockInterface">
8+
<attribute code="sales_channels" type="Magento\InventorySalesApi\Api\Data\SalesChannelInterface[]" />
9+
</extension_attributes>
10+
</config>

0 commit comments

Comments
 (0)