Skip to content

Add custom attributes to any model #3941

@marvincaspar

Description

@marvincaspar

I want to add custom attributes for any model, in this example I use the shipment

Steps to reproduce

  • Install Magento v2.0.2
  • Override for example the shipment class and set the available custom attributes codes with getCustomAttributesCodes()

di.xml

<preference for="Magento\Sales\Model\Order\Shipment" type="My\Namespace\Model\Order\SalesShipment"/>

SalesShipment.php

<?php

namespace My\Namespace\Model\Order;

class SalesShipment extends \Magento\Sales\Model\Order\Shipment
{
    /**
     * @param \Magento\Framework\Model\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
     * @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory
     * @param \Magento\Sales\Model\ResourceModel\Order\Shipment\Item\CollectionFactory $shipmentItemCollectionFactory
     * @param \Magento\Sales\Model\ResourceModel\Order\Shipment\Track\CollectionFactory $trackCollectionFactory
     * @param \Magento\Sales\Model\Order\Shipment\CommentFactory $commentFactory
     * @param \Magento\Sales\Model\ResourceModel\Order\Shipment\Comment\CollectionFactory $commentCollectionFactory
     * @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
     * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
     * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
     * @param array $data
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
     */
    public function __construct(
        \Magento\Framework\Model\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
        \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
        \Magento\Sales\Model\ResourceModel\Order\Shipment\Item\CollectionFactory $shipmentItemCollectionFactory,
        \Magento\Sales\Model\ResourceModel\Order\Shipment\Track\CollectionFactory $trackCollectionFactory,
        \Magento\Sales\Model\Order\Shipment\CommentFactory $commentFactory,
        \Magento\Sales\Model\ResourceModel\Order\Shipment\Comment\CollectionFactory $commentCollectionFactory,
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
        \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
        \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
        array $data = []
    )
    {
        parent::__construct(
            $context,
            $registry,
            $extensionFactory,
            $customAttributeFactory,
            $shipmentItemCollectionFactory,
            $trackCollectionFactory,
            $commentFactory,
            $commentCollectionFactory,
            $orderRepository,
            $resource,
            $resourceCollection,
            $data
        );
    }

    /**
     * {@inheritdoc}
     */
    protected function getCustomAttributesCodes()
    {
        return [
            "house_number"
        ];
    }
}
  • Call api method, e.g. to create a shipment with the following data
{  
  "shipment":{  
    "custom_attributes": [
       {
        "attribute_code": "house_number",
        "value": "123"
       }
    ],
    ...
  }
}

Expected result

  1. The custom attributes are stored
  2. Load a shipment and call getCustomAttributes() should get the custom attributes
  3. The api get call returns the custom attributes
{
   "increment_id": "000000001",
   "custom_attributes": [   {
      "attribute_code": "url_key",
      "value": "12345678"
   }],
  ...
}

Actual result

  1. $newShipment = $shipment->save() includes the custom attributes
  2. If I load the shipment object and call getCustomAttributes() I get an empty array
{
   "increment_id": "000000001",
  ...
}

Metadata

Metadata

Labels

Issue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for development

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions