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.


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.


<?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="Magento\Catalog\Api\Data\ProductInterface">
        <attribute code="length" type="string" />

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 Magento\Catalog\Api\Data\ProductInterface 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="Magento\Catalog\Api\ProductRepositoryInterface">
        <plugin name="get_product_features" type="YourPlugin\ProductAttributes\Plugin\ProductGet"/>

In our di.xml we have defined a plugin for the ProductRepositoryInterface. Our plugin class is: YourPlugin\ProductAttributes\Plugin\ProductGet 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 YourPlugin\ProductAttributes\Plugin;

use Magento\Catalog\Api\Data\ProductInterface;

class ProductGet
    protected $productExtensionFactory;
    protected $productFactory;

    public function __construct(
        \Magento\Catalog\Api\Data\ProductExtensionFactory $productExtensionFactory,
        \Magento\Catalog\Model\ProductFactory $productFactory
        $this->productFactory = $productFactory;
        $this->productExtensionFactory = $productExtensionFactory;

    public function aroundGetById(
        \Magento\Catalog\Api\ProductRepositoryInterface $subject,
        \Closure $proceed,
        /** @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();

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

        return $product;

and you might have to pass it you a


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 Mar 22 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.