-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Closed
Labels
Issue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentGate 4. Acknowledged. Issue is added to backlog and ready for development
Description
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
- The custom attributes are stored
- Load a shipment and call
getCustomAttributes()
should get the custom attributes - The api get call returns the custom attributes
{
"increment_id": "000000001",
"custom_attributes": [ {
"attribute_code": "url_key",
"value": "12345678"
}],
...
}
Actual result
$newShipment = $shipment->save()
includes the custom attributes- If I load the shipment object and call
getCustomAttributes()
I get an empty array
{
"increment_id": "000000001",
...
}
Metadata
Metadata
Assignees
Labels
Issue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentGate 4. Acknowledged. Issue is added to backlog and ready for development