How to set an attribute to an order?

How to set an attribute to an order?

In order to set an attribute to an order, the following steps are required.

      1. The first step is to obtain the order object based on the order ID. This is done using Magento's OrderRepository:
1
2
3
use Magento\Sales\Model\OrderRepository;

$order = $objectManager->create(OrderRepository::class)->get($orderId);

Here, $orderId is the ID of the order for which you want to set the custom attribute.


      2.  Once you have the order object, you need to get the related entity using Amasty's EntityResolver. There are two ways to resolve the entity:
  1. By Order Object:

    1
    2
    3
    4
    use Amasty\Orderattr\Model\Entity\EntityResolver;
    
    $entityResolver = $objectManager->create(EntityResolver::class);
    $entity = $entityResolver->getEntityByOrder($order);
    
  2. By Order ID:

    $entity = $entityResolver->getEntityByOrderId($orderId);

Choose the method that best fits your scenario.


      3. With the entity in hand, you can now set the custom attribute using the setCustomAttribute method. This method updates the attribute with the specified value using the following code:
1
2
3
4
$attributeCode = 'your_custom_attribute_code'; // Replace with your actual attribute code
$attributeValue = 'your_desired_value'; // Replace with the value you want to set

$entity->setCustomAttribute($attributeCode, $attributeValue);
      • Related Articles

      • My Order Attribute relations don't work, how can I check if everything is configured correctly?

        Firstly, check which attribute is used as a parent attribute in the relation. The Order Attributes relations could be created only for parent attributes with the following types: Dropdown, Radio Button, Checkbox Group. Secondly, please, make sure the ...
      • How to add an attribute to the Search?

        In order to make an attribute which is assigned to the products searchable, please, follow the next steps: 1) Kindly navigate to Stores > Attributes > Product and choose the attribute required. 2) In the attribute settings, please, switch to the ...
      • Order Attributes | GraphQL methods

        Queries amOrderattrAvailableAttributes (cartId: String!) description: get the list of available order attributes amOrderattrAttributesRelations description: get information about order attribute relations Mutations saveAmOrderattrValues (cartId: ...
      • Order Attributes API

        1. Amasty\Orderattr\Api\EntityDataRepositoryInterface - class for managing order attributes in the backend: POST /V1/amasty_orderattr/entityData, method="save" - create an attribute; PUT /V1/amasty_orderattr/entityData/:entityId, method="save" - edit ...
      • Why don't the Order Attributes relations work while placing an order in the Backend?

        It is the expected extension behaviour. According to the Order Attributes extension logic, attribute relations don't work for orders created in the Backend since store admins are believed to know the store logic. That's why they are able to view all ...