Magento 2 how to add product dimensions using rest apiInvalid Signature - add a product using Magento REST APIMagento soap api delete product using skuRest API - how to get custom attributesGet multiple product images using REST APIHow to add product in Magento using REST API?Magento 2 Update a product name using Rest APIHow to add product using REST api?How to create product using SOAP API magento 2export products Magento 2.1.9How to add product with category name instead of category id in Magento using Rest API
What is the evidence for the "tyranny of the majority problem" in a direct democracy context?
What is this called? Old film camera viewer?
How does a computer interpret real numbers?
How could a planet have erratic days?
What if a revenant (monster) gains fire resistance?
Does an advisor owe his/her student anything? Will an advisor keep a PhD student only out of pity?
Why did the EU agree to delay the Brexit deadline?
Rising and falling intonation
Does a 'pending' US visa application constitute a denial?
Biological Blimps: Propulsion
Terse Method to Swap Lowest for Highest?
Is it improper etiquette to ask your opponent what his/her rating is before the game?
Open a doc from terminal, but not by its name
On a tidally locked planet, would time be quantized?
What changes for testers when they are testing in agile environments?
Is Witten's Proof of the Positive Mass Theorem Rigorous?
Non-trope happy ending?
How can "mimic phobia" be cured or prevented?
Are Captain Marvel's powers affected by Thanos' actions in Infinity War
Is aluminum electrical wire used on aircraft?
What will be next at the bottom row and why?
How can I block email signup overlays or javascript popups in Safari?
What does chmod -u do?
Did arcade monitors have same pixel aspect ratio as TV sets?
Magento 2 how to add product dimensions using rest api
Invalid Signature - add a product using Magento REST APIMagento soap api delete product using skuRest API - how to get custom attributesGet multiple product images using REST APIHow to add product in Magento using REST API?Magento 2 Update a product name using Rest APIHow to add product using REST api?How to create product using SOAP API magento 2export products Magento 2.1.9How to add product with category name instead of category id in Magento using Rest API
I can add simple products to Magento 2 just fine and adding the weight is no problem at all I do the following:
$data = '{
"product": {
"sku": "' . $row["item_number"] . '",
"name": "' . $row["name"] .'",
"attributeSetId": 4,
"price": ' . $row["price"] . ',
"status": 1,
"visibility": 4,
"typeId": "simple",
"createdAt": "' . date('Y-m-d H:i:s') . '",
"updatedAt": "' . date('Y-m-d H:i:s') . '",
"weight": ' . $row["weight"] . ',
"custom_attributes":
"description": "' . $row["condition_description"] . '<br>' . 'Brand: ' . $row["brand"] . '<br>' . 'MPN: ' . $row["mpn"] . '<br>' . 'Fitment: ' . '<br>' . $fitment . '",
"short_description": "' . $row["condition_description"] . '",
"MPN": "' . $row["mpn"] . '",
"brand": "' . $row["brand"] . '"
,
Adding "length": "10" below the weight attribute results in an error. Magento 2 allows the adding of height, width and length when adding a product manually so it must be possible. I would like to avoid storing these in a custom attribute to keep the shipping calculations easier.
Is length not called length but something else? I have tried ts_dimensions_length, packagedimensions and dimensionslength.
magento2 api soap
New contributor
add a comment |
I can add simple products to Magento 2 just fine and adding the weight is no problem at all I do the following:
$data = '{
"product": {
"sku": "' . $row["item_number"] . '",
"name": "' . $row["name"] .'",
"attributeSetId": 4,
"price": ' . $row["price"] . ',
"status": 1,
"visibility": 4,
"typeId": "simple",
"createdAt": "' . date('Y-m-d H:i:s') . '",
"updatedAt": "' . date('Y-m-d H:i:s') . '",
"weight": ' . $row["weight"] . ',
"custom_attributes":
"description": "' . $row["condition_description"] . '<br>' . 'Brand: ' . $row["brand"] . '<br>' . 'MPN: ' . $row["mpn"] . '<br>' . 'Fitment: ' . '<br>' . $fitment . '",
"short_description": "' . $row["condition_description"] . '",
"MPN": "' . $row["mpn"] . '",
"brand": "' . $row["brand"] . '"
,
Adding "length": "10" below the weight attribute results in an error. Magento 2 allows the adding of height, width and length when adding a product manually so it must be possible. I would like to avoid storing these in a custom attribute to keep the shipping calculations easier.
Is length not called length but something else? I have tried ts_dimensions_length, packagedimensions and dimensionslength.
magento2 api soap
New contributor
add a comment |
I can add simple products to Magento 2 just fine and adding the weight is no problem at all I do the following:
$data = '{
"product": {
"sku": "' . $row["item_number"] . '",
"name": "' . $row["name"] .'",
"attributeSetId": 4,
"price": ' . $row["price"] . ',
"status": 1,
"visibility": 4,
"typeId": "simple",
"createdAt": "' . date('Y-m-d H:i:s') . '",
"updatedAt": "' . date('Y-m-d H:i:s') . '",
"weight": ' . $row["weight"] . ',
"custom_attributes":
"description": "' . $row["condition_description"] . '<br>' . 'Brand: ' . $row["brand"] . '<br>' . 'MPN: ' . $row["mpn"] . '<br>' . 'Fitment: ' . '<br>' . $fitment . '",
"short_description": "' . $row["condition_description"] . '",
"MPN": "' . $row["mpn"] . '",
"brand": "' . $row["brand"] . '"
,
Adding "length": "10" below the weight attribute results in an error. Magento 2 allows the adding of height, width and length when adding a product manually so it must be possible. I would like to avoid storing these in a custom attribute to keep the shipping calculations easier.
Is length not called length but something else? I have tried ts_dimensions_length, packagedimensions and dimensionslength.
magento2 api soap
New contributor
I can add simple products to Magento 2 just fine and adding the weight is no problem at all I do the following:
$data = '{
"product": {
"sku": "' . $row["item_number"] . '",
"name": "' . $row["name"] .'",
"attributeSetId": 4,
"price": ' . $row["price"] . ',
"status": 1,
"visibility": 4,
"typeId": "simple",
"createdAt": "' . date('Y-m-d H:i:s') . '",
"updatedAt": "' . date('Y-m-d H:i:s') . '",
"weight": ' . $row["weight"] . ',
"custom_attributes":
"description": "' . $row["condition_description"] . '<br>' . 'Brand: ' . $row["brand"] . '<br>' . 'MPN: ' . $row["mpn"] . '<br>' . 'Fitment: ' . '<br>' . $fitment . '",
"short_description": "' . $row["condition_description"] . '",
"MPN": "' . $row["mpn"] . '",
"brand": "' . $row["brand"] . '"
,
Adding "length": "10" below the weight attribute results in an error. Magento 2 allows the adding of height, width and length when adding a product manually so it must be possible. I would like to avoid storing these in a custom attribute to keep the shipping calculations easier.
Is length not called length but something else? I have tried ts_dimensions_length, packagedimensions and dimensionslength.
magento2 api soap
magento2 api soap
New contributor
New contributor
edited Mar 18 at 4:03
ABHISHEK TRIPATHI
1,9741726
1,9741726
New contributor
asked Mar 17 at 21:57
Dan SDan S
61
61
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I am assuming these are your custom attributes , not magento OOTB attributes . For them to be available for your API , you need to create extension attributes for them. Please follow these steps to add them to your API
Define our configuration in extension_attributes.xml and di.xml
The first step is to create new custom Module and register our extension attribute with Magento 2. We do this like with extension_attributes.xml which lives within the etc directory of your module.
etc/extension_attributes.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="MagentoCatalogApiDataProductInterface">
<attribute code="length" type="string" />
</extension_attributes>
</config>
Note: ? This tells Magento 2 which class we are defining an extension attribute for. As we’re doing this for a product, we’ll need to use the MagentoCatalogApiDataProductInterface interface.
The next line defines our attributes. We give them a name, and a type. The type in this case can reference either a PHP type or a class type.
Next up is our di.xml again, living in etc directory. This is so we can register a plugin to set data to our extension attribute
etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoCatalogApiProductRepositoryInterface">
<plugin name="get_product_features" type="YourPluginProductAttributesPluginProductGet"/>
</type>
</config>
In our di.xml we have defined a plugin for the ProductRepositoryInterface. Our plugin class is: YourPluginProductAttributesPluginProductGet we’ll need to create this in the next step.
Step 5) Create our plugin
Now, when creating extensionattributes you can implement your own logic. Thats one of the powerful features of extensionattributes. Instead of defining a PHP type you could instead specify a class that implements a bunch of logic. However, the drawback is if you’re implementing something “simple” like an attribute in our case, we still need to do all the hard work. No magic here folks.
The way we achieve this is by defining a plugin around the getById method on the ProductRepositoryInterface interface. At this point we can handle fetching data directly from the product model, and set it on the extension attribute class.
<?php namespace YourPluginProductAttributesPlugin;
use MagentoCatalogApiDataProductInterface;
class ProductGet
protected $productExtensionFactory;
protected $productFactory;
public function __construct(
MagentoCatalogApiDataProductExtensionFactory $productExtensionFactory,
MagentoCatalogModelProductFactory $productFactory
)
$this->productFactory = $productFactory;
$this->productExtensionFactory = $productExtensionFactory;
public function aroundGetById(
MagentoCatalogApiProductRepositoryInterface $subject,
Closure $proceed,
$customerId
)
/** @var ProductInterface $product */
$product = $proceed($customerId);
// If extension attribute is already set, return early.
if ($product->getExtensionAttributes() && $product->getExtensionAttributes()->getFeatures())
return $product;
// In the event that extension attribute class has not be instansiated yet.
// in this event, we create it ourselves.
if (!$product->getExtensionAttributes())
$productExtension = $this->productExtensionFactory->create();
$product->setExtensionAttributes($productExtension);
// Fetch the raw product model (I have not found a better way), and set the data onto our attribute.
$productModel = $this->productFactory->create()->load($product->getId());
$product->getExtensionAttributes()
->setFeatures($productModel->getData('features'));
return $product;
and you might have to pass it you a
REFERENCE 1 REFERENCE 2 REFERENCE 3
Please try to use after plugin(as used in REF 3) rather than around plugin as in above example
This will work but I am trying to use Magento OOTB attributes so they show up under the product page in the admin panel.
– Dan S
yesterday
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Dan S is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f266227%2fmagento-2-how-to-add-product-dimensions-using-rest-api%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I am assuming these are your custom attributes , not magento OOTB attributes . For them to be available for your API , you need to create extension attributes for them. Please follow these steps to add them to your API
Define our configuration in extension_attributes.xml and di.xml
The first step is to create new custom Module and register our extension attribute with Magento 2. We do this like with extension_attributes.xml which lives within the etc directory of your module.
etc/extension_attributes.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="MagentoCatalogApiDataProductInterface">
<attribute code="length" type="string" />
</extension_attributes>
</config>
Note: ? This tells Magento 2 which class we are defining an extension attribute for. As we’re doing this for a product, we’ll need to use the MagentoCatalogApiDataProductInterface interface.
The next line defines our attributes. We give them a name, and a type. The type in this case can reference either a PHP type or a class type.
Next up is our di.xml again, living in etc directory. This is so we can register a plugin to set data to our extension attribute
etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoCatalogApiProductRepositoryInterface">
<plugin name="get_product_features" type="YourPluginProductAttributesPluginProductGet"/>
</type>
</config>
In our di.xml we have defined a plugin for the ProductRepositoryInterface. Our plugin class is: YourPluginProductAttributesPluginProductGet we’ll need to create this in the next step.
Step 5) Create our plugin
Now, when creating extensionattributes you can implement your own logic. Thats one of the powerful features of extensionattributes. Instead of defining a PHP type you could instead specify a class that implements a bunch of logic. However, the drawback is if you’re implementing something “simple” like an attribute in our case, we still need to do all the hard work. No magic here folks.
The way we achieve this is by defining a plugin around the getById method on the ProductRepositoryInterface interface. At this point we can handle fetching data directly from the product model, and set it on the extension attribute class.
<?php namespace YourPluginProductAttributesPlugin;
use MagentoCatalogApiDataProductInterface;
class ProductGet
protected $productExtensionFactory;
protected $productFactory;
public function __construct(
MagentoCatalogApiDataProductExtensionFactory $productExtensionFactory,
MagentoCatalogModelProductFactory $productFactory
)
$this->productFactory = $productFactory;
$this->productExtensionFactory = $productExtensionFactory;
public function aroundGetById(
MagentoCatalogApiProductRepositoryInterface $subject,
Closure $proceed,
$customerId
)
/** @var ProductInterface $product */
$product = $proceed($customerId);
// If extension attribute is already set, return early.
if ($product->getExtensionAttributes() && $product->getExtensionAttributes()->getFeatures())
return $product;
// In the event that extension attribute class has not be instansiated yet.
// in this event, we create it ourselves.
if (!$product->getExtensionAttributes())
$productExtension = $this->productExtensionFactory->create();
$product->setExtensionAttributes($productExtension);
// Fetch the raw product model (I have not found a better way), and set the data onto our attribute.
$productModel = $this->productFactory->create()->load($product->getId());
$product->getExtensionAttributes()
->setFeatures($productModel->getData('features'));
return $product;
and you might have to pass it you a
REFERENCE 1 REFERENCE 2 REFERENCE 3
Please try to use after plugin(as used in REF 3) rather than around plugin as in above example
This will work but I am trying to use Magento OOTB attributes so they show up under the product page in the admin panel.
– Dan S
yesterday
add a comment |
I am assuming these are your custom attributes , not magento OOTB attributes . For them to be available for your API , you need to create extension attributes for them. Please follow these steps to add them to your API
Define our configuration in extension_attributes.xml and di.xml
The first step is to create new custom Module and register our extension attribute with Magento 2. We do this like with extension_attributes.xml which lives within the etc directory of your module.
etc/extension_attributes.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="MagentoCatalogApiDataProductInterface">
<attribute code="length" type="string" />
</extension_attributes>
</config>
Note: ? This tells Magento 2 which class we are defining an extension attribute for. As we’re doing this for a product, we’ll need to use the MagentoCatalogApiDataProductInterface interface.
The next line defines our attributes. We give them a name, and a type. The type in this case can reference either a PHP type or a class type.
Next up is our di.xml again, living in etc directory. This is so we can register a plugin to set data to our extension attribute
etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoCatalogApiProductRepositoryInterface">
<plugin name="get_product_features" type="YourPluginProductAttributesPluginProductGet"/>
</type>
</config>
In our di.xml we have defined a plugin for the ProductRepositoryInterface. Our plugin class is: YourPluginProductAttributesPluginProductGet we’ll need to create this in the next step.
Step 5) Create our plugin
Now, when creating extensionattributes you can implement your own logic. Thats one of the powerful features of extensionattributes. Instead of defining a PHP type you could instead specify a class that implements a bunch of logic. However, the drawback is if you’re implementing something “simple” like an attribute in our case, we still need to do all the hard work. No magic here folks.
The way we achieve this is by defining a plugin around the getById method on the ProductRepositoryInterface interface. At this point we can handle fetching data directly from the product model, and set it on the extension attribute class.
<?php namespace YourPluginProductAttributesPlugin;
use MagentoCatalogApiDataProductInterface;
class ProductGet
protected $productExtensionFactory;
protected $productFactory;
public function __construct(
MagentoCatalogApiDataProductExtensionFactory $productExtensionFactory,
MagentoCatalogModelProductFactory $productFactory
)
$this->productFactory = $productFactory;
$this->productExtensionFactory = $productExtensionFactory;
public function aroundGetById(
MagentoCatalogApiProductRepositoryInterface $subject,
Closure $proceed,
$customerId
)
/** @var ProductInterface $product */
$product = $proceed($customerId);
// If extension attribute is already set, return early.
if ($product->getExtensionAttributes() && $product->getExtensionAttributes()->getFeatures())
return $product;
// In the event that extension attribute class has not be instansiated yet.
// in this event, we create it ourselves.
if (!$product->getExtensionAttributes())
$productExtension = $this->productExtensionFactory->create();
$product->setExtensionAttributes($productExtension);
// Fetch the raw product model (I have not found a better way), and set the data onto our attribute.
$productModel = $this->productFactory->create()->load($product->getId());
$product->getExtensionAttributes()
->setFeatures($productModel->getData('features'));
return $product;
and you might have to pass it you a
REFERENCE 1 REFERENCE 2 REFERENCE 3
Please try to use after plugin(as used in REF 3) rather than around plugin as in above example
This will work but I am trying to use Magento OOTB attributes so they show up under the product page in the admin panel.
– Dan S
yesterday
add a comment |
I am assuming these are your custom attributes , not magento OOTB attributes . For them to be available for your API , you need to create extension attributes for them. Please follow these steps to add them to your API
Define our configuration in extension_attributes.xml and di.xml
The first step is to create new custom Module and register our extension attribute with Magento 2. We do this like with extension_attributes.xml which lives within the etc directory of your module.
etc/extension_attributes.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="MagentoCatalogApiDataProductInterface">
<attribute code="length" type="string" />
</extension_attributes>
</config>
Note: ? This tells Magento 2 which class we are defining an extension attribute for. As we’re doing this for a product, we’ll need to use the MagentoCatalogApiDataProductInterface interface.
The next line defines our attributes. We give them a name, and a type. The type in this case can reference either a PHP type or a class type.
Next up is our di.xml again, living in etc directory. This is so we can register a plugin to set data to our extension attribute
etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoCatalogApiProductRepositoryInterface">
<plugin name="get_product_features" type="YourPluginProductAttributesPluginProductGet"/>
</type>
</config>
In our di.xml we have defined a plugin for the ProductRepositoryInterface. Our plugin class is: YourPluginProductAttributesPluginProductGet we’ll need to create this in the next step.
Step 5) Create our plugin
Now, when creating extensionattributes you can implement your own logic. Thats one of the powerful features of extensionattributes. Instead of defining a PHP type you could instead specify a class that implements a bunch of logic. However, the drawback is if you’re implementing something “simple” like an attribute in our case, we still need to do all the hard work. No magic here folks.
The way we achieve this is by defining a plugin around the getById method on the ProductRepositoryInterface interface. At this point we can handle fetching data directly from the product model, and set it on the extension attribute class.
<?php namespace YourPluginProductAttributesPlugin;
use MagentoCatalogApiDataProductInterface;
class ProductGet
protected $productExtensionFactory;
protected $productFactory;
public function __construct(
MagentoCatalogApiDataProductExtensionFactory $productExtensionFactory,
MagentoCatalogModelProductFactory $productFactory
)
$this->productFactory = $productFactory;
$this->productExtensionFactory = $productExtensionFactory;
public function aroundGetById(
MagentoCatalogApiProductRepositoryInterface $subject,
Closure $proceed,
$customerId
)
/** @var ProductInterface $product */
$product = $proceed($customerId);
// If extension attribute is already set, return early.
if ($product->getExtensionAttributes() && $product->getExtensionAttributes()->getFeatures())
return $product;
// In the event that extension attribute class has not be instansiated yet.
// in this event, we create it ourselves.
if (!$product->getExtensionAttributes())
$productExtension = $this->productExtensionFactory->create();
$product->setExtensionAttributes($productExtension);
// Fetch the raw product model (I have not found a better way), and set the data onto our attribute.
$productModel = $this->productFactory->create()->load($product->getId());
$product->getExtensionAttributes()
->setFeatures($productModel->getData('features'));
return $product;
and you might have to pass it you a
REFERENCE 1 REFERENCE 2 REFERENCE 3
Please try to use after plugin(as used in REF 3) rather than around plugin as in above example
I am assuming these are your custom attributes , not magento OOTB attributes . For them to be available for your API , you need to create extension attributes for them. Please follow these steps to add them to your API
Define our configuration in extension_attributes.xml and di.xml
The first step is to create new custom Module and register our extension attribute with Magento 2. We do this like with extension_attributes.xml which lives within the etc directory of your module.
etc/extension_attributes.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="MagentoCatalogApiDataProductInterface">
<attribute code="length" type="string" />
</extension_attributes>
</config>
Note: ? This tells Magento 2 which class we are defining an extension attribute for. As we’re doing this for a product, we’ll need to use the MagentoCatalogApiDataProductInterface interface.
The next line defines our attributes. We give them a name, and a type. The type in this case can reference either a PHP type or a class type.
Next up is our di.xml again, living in etc directory. This is so we can register a plugin to set data to our extension attribute
etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoCatalogApiProductRepositoryInterface">
<plugin name="get_product_features" type="YourPluginProductAttributesPluginProductGet"/>
</type>
</config>
In our di.xml we have defined a plugin for the ProductRepositoryInterface. Our plugin class is: YourPluginProductAttributesPluginProductGet we’ll need to create this in the next step.
Step 5) Create our plugin
Now, when creating extensionattributes you can implement your own logic. Thats one of the powerful features of extensionattributes. Instead of defining a PHP type you could instead specify a class that implements a bunch of logic. However, the drawback is if you’re implementing something “simple” like an attribute in our case, we still need to do all the hard work. No magic here folks.
The way we achieve this is by defining a plugin around the getById method on the ProductRepositoryInterface interface. At this point we can handle fetching data directly from the product model, and set it on the extension attribute class.
<?php namespace YourPluginProductAttributesPlugin;
use MagentoCatalogApiDataProductInterface;
class ProductGet
protected $productExtensionFactory;
protected $productFactory;
public function __construct(
MagentoCatalogApiDataProductExtensionFactory $productExtensionFactory,
MagentoCatalogModelProductFactory $productFactory
)
$this->productFactory = $productFactory;
$this->productExtensionFactory = $productExtensionFactory;
public function aroundGetById(
MagentoCatalogApiProductRepositoryInterface $subject,
Closure $proceed,
$customerId
)
/** @var ProductInterface $product */
$product = $proceed($customerId);
// If extension attribute is already set, return early.
if ($product->getExtensionAttributes() && $product->getExtensionAttributes()->getFeatures())
return $product;
// In the event that extension attribute class has not be instansiated yet.
// in this event, we create it ourselves.
if (!$product->getExtensionAttributes())
$productExtension = $this->productExtensionFactory->create();
$product->setExtensionAttributes($productExtension);
// Fetch the raw product model (I have not found a better way), and set the data onto our attribute.
$productModel = $this->productFactory->create()->load($product->getId());
$product->getExtensionAttributes()
->setFeatures($productModel->getData('features'));
return $product;
and you might have to pass it you a
REFERENCE 1 REFERENCE 2 REFERENCE 3
Please try to use after plugin(as used in REF 3) rather than around plugin as in above example
edited Mar 18 at 4:59
answered Mar 18 at 4:54
Vishwas BhatnagarVishwas Bhatnagar
2,92822046
2,92822046
This will work but I am trying to use Magento OOTB attributes so they show up under the product page in the admin panel.
– Dan S
yesterday
add a comment |
This will work but I am trying to use Magento OOTB attributes so they show up under the product page in the admin panel.
– Dan S
yesterday
This will work but I am trying to use Magento OOTB attributes so they show up under the product page in the admin panel.
– Dan S
yesterday
This will work but I am trying to use Magento OOTB attributes so they show up under the product page in the admin panel.
– Dan S
yesterday
add a comment |
Dan S is a new contributor. Be nice, and check out our Code of Conduct.
Dan S is a new contributor. Be nice, and check out our Code of Conduct.
Dan S is a new contributor. Be nice, and check out our Code of Conduct.
Dan S is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f266227%2fmagento-2-how-to-add-product-dimensions-using-rest-api%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown